fix: resolve --file paths from any directory in the repo

Strip the repository workdir (not .git) when converting --file to a
tree-relative path, and also try paths relative to the workdir root.

Fixes #2871

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
wuyangfan 2026-05-17 16:16:09 +08:00
parent 8619c07f3f
commit 09604cafc5

View file

@ -148,6 +148,25 @@ impl Environment {
}
}
/// Resolves `--file` to a path relative to the repository workdir (as used by the file tree).
fn resolve_select_file(file: PathBuf, repo: &RepoPath) -> Result<Option<PathBuf>> {
let workdir = PathBuf::from(repo_work_dir(repo)?);
let workdir_canon = workdir.canonicalize()?;
let candidates = [
file.canonicalize().ok(),
workdir.join(&file).canonicalize().ok(),
];
for abs in candidates.into_iter().flatten() {
if let Ok(rel) = abs.strip_prefix(&workdir_canon) {
return Ok(Some(Path::new(".").join(rel)));
}
}
Ok(None)
}
// public interface
impl App {
///
@ -178,14 +197,7 @@ impl App {
let mut select_file: Option<PathBuf> = None;
let tab = if let Some(file) = cliargs.select_file {
// convert to relative git path
if let Ok(abs) = file.canonicalize() {
if let Ok(path) = abs.strip_prefix(
env.repo.borrow().gitpath().canonicalize()?,
) {
select_file = Some(Path::new(".").join(path));
}
}
select_file = resolve_select_file(file, &repo.borrow())?;
2
} else {
env.options.borrow().current_tab()