From 09604cafc54ae94b87211700dd0b42f00f4abd91 Mon Sep 17 00:00:00 2001 From: wuyangfan <1102042793@qq.com> Date: Sun, 17 May 2026 16:16:09 +0800 Subject: [PATCH] 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 --- src/app.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/app.rs b/src/app.rs index 8626aa3b..8b20c5e9 100644 --- a/src/app.rs +++ b/src/app.rs @@ -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> { + 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 = 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()