Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix: canonicalize paths when resolving current worktree (#696)

Maximilian Roos1384e4f61f57
diff --git a/src/git/repository/worktrees.rs b/src/git/repository/worktrees.rs
index fe016591f..bc06bd745 100644
--- a/src/git/repository/worktrees.rs
+++ b/src/git/repository/worktrees.rs
@@ -3,6 +3,7 @@
use std::path::{Path, PathBuf};
use color_print::cformat;
+use dunce::canonicalize;
use normalize_path::NormalizePath;
use super::{GitError, Repository, ResolvedWorktree, WorktreeInfo};
@@ -30,12 +31,18 @@ impl Repository {
/// Note: For worktree-specific operations, use [`current_worktree()`](Self::current_worktree)
/// to get a [`WorkingTree`](super::WorkingTree) instead.
pub fn current_worktree_info(&self) -> anyhow::Result<Option<WorktreeInfo>> {
+ // root() returns canonicalized path, so canonicalize worktree paths for comparison
+ // to handle symlinks (e.g., macOS /var -> /private/var)
let current_path = match self.current_worktree().root() {
- Ok(p) => p.to_path_buf(),
+ Ok(p) => p,
Err(_) => return Ok(None),
};
let worktrees = self.list_worktrees()?;
- Ok(worktrees.into_iter().find(|wt| wt.path == current_path))
+ Ok(worktrees.into_iter().find(|wt| {
+ canonicalize(&wt.path)
+ .map(|p| p == current_path)
+ .unwrap_or(false)
+ }))
}
/// Find the worktree path for a given branch, if one exists.
@@ -180,15 +187,14 @@ impl Repository {
.map_err(|_| GitError::NotInWorktree {
action: Some("resolve '@'".into()),
})?;
+ // root() returns canonicalized path, so canonicalize worktree paths
+ // for comparison to handle symlinks (e.g., macOS /var -> /private/var)
let worktrees = self.list_worktrees()?;
let branch = worktrees
.iter()
- .find(|wt| wt.path == path)
+ .find(|wt| canonicalize(&wt.path).map(|p| p == path).unwrap_or(false))
.and_then(|wt| wt.branch.clone());
- Ok(ResolvedWorktree::Worktree {
- path: path.to_path_buf(),
- branch,
- })
+ Ok(ResolvedWorktree::Worktree { path, branch })
}
_ => {
// Resolve to branch name first, then find its worktree
diff --git a/tests/integration_tests/remove.rs b/tests/integration_tests/remove.rs
index 76714ef78..641d4cf0f 100644
--- a/tests/integration_tests/remove.rs
+++ b/tests/integration_tests/remove.rs
@@ -1598,3 +1598,43 @@ fn test_remove_detached_worktree_in_multi(mut repo: TestRepo) {
None
));
}
+
+/// Test that resolve_worktree("@") works when the worktree is accessed via a symlink.
+///
+/// This tests the path normalization fix where:
+/// - `root()` returns a canonicalized path (symlinks resolved)
+/// - `wt.path` from git is the raw path (symlinks not resolved)
+///
+/// Without proper canonicalization, comparison fails on systems with symlinks
+/// (e.g., macOS /var -> /private/var).
+#[cfg(unix)]
+#[rstest]
+fn test_remove_at_symbol_via_symlink(mut repo: TestRepo) {
+ use std::os::unix::fs::symlink;
+
+ let worktree_path = repo.add_worktree("feature-symlink");
+
+ // Create a symlink pointing to the worktree
+ let symlink_path = repo
+ .root_path()
+ .parent()
+ .unwrap()
+ .join("symlink-to-feature");
+ symlink(&worktree_path, &symlink_path).expect("Failed to create symlink");
+
+ // Verify symlink was created
+ assert!(
+ symlink_path.is_symlink(),
+ "Symlink should exist at {:?}",
+ symlink_path
+ );
+
+ // Run `wt remove @` from the symlinked path
+ // This tests that resolve_worktree("@") properly handles symlinked paths
+ assert_cmd_snapshot!(make_snapshot_cmd(
+ &repo,
+ "remove",
+ &["@"],
+ Some(&symlink_path)
+ ));
+}
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_at_symbol_via_symlink.snap b/tests/snapshots/integration__integration_tests__remove__remove_at_symbol_via_symlink.snap
new file mode 100644
index 000000000..264b680fe
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__remove__remove_at_symbol_via_symlink.snap
@@ -0,0 +1,39 @@
+---
+source: tests/integration_tests/remove.rs
+info:
+ program: wt
+ args:
+ - remove
+ - "@"
+ env:
+ APPDATA: "[TEST_CONFIG_HOME]"
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "500"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
+ HOME: "[TEST_HOME]"
+ LANG: C
+ LC_ALL: C
+ MOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"
+ PATH: "[PATH]"
+ RUST_LOG: warn
+ SHELL: ""
+ SOURCE_DATE_EPOCH: "1735776000"
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+
+----- stderr -----
+◎ Removing feature-symlink worktree & branch in background (same commit as main, _)
+▲ Cannot change directory — shell integration not installed
+↳ To enable automatic cd, run wt config shell install