Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: use symlink-preserved paths in display messages (#985)
Maximilian Roosb0814c52a3ca
diff --git a/src/output/global.rs b/src/output/global.rsindex 5b618ac54..0b3584766 100644--- a/src/output/global.rs+++ b/src/output/global.rs@@ -137,6 +137,22 @@ impl SymlinkMapping {}}+/// Translate a canonical path to the user's logical (symlink-preserved) path.+///+/// If the user navigated via symlink (e.g., `/workspace/project` -> `/mnt/wsl/workspace/project`),+/// this translates canonical paths back to the symlink tree. Returns the original path unchanged+/// if no symlink mapping exists or the translation doesn't round-trip correctly.+pub fn to_logical_path(path: &Path) -> PathBuf {+ let guard = get_state().lock().expect("OUTPUT_STATE lock poisoned");+ let Some(mapping) = &guard.symlink_mapping else {+ return path.to_path_buf();+ };+ mapping+ .to_logical_path(path)+ .filter(|translated| dunce::canonicalize(translated).ok() == dunce::canonicalize(path).ok())+ .unwrap_or_else(|| path.to_path_buf())+}+/// Get or lazily initialize the global output state.////// Reads `WORKTRUNK_DIRECTIVE_FILE` from environment on first access.@@ -197,26 +213,9 @@ pub fn change_directory(path: impl AsRef<Path>) -> io::Result<()> {// Write to directive file if setif guard.directive_file.is_some() {- // Clone mapping out of lock — the canonicalize check below does I/O- let symlink_mapping = guard.symlink_mapping.clone();drop(guard); // Release lock before I/O- // Translate canonical path to logical (symlink-preserved) path for the cd directive.- // The user's shell should stay in their symlink tree — e.g., if they navigated via- // /workspace/project (symlink to /mnt/wsl/workspace/project), the cd should use- // /workspace/project.feature, not /mnt/wsl/workspace/project.feature.- //- // Verify the translation round-trips correctly: canonicalize(translated) must equal- // canonicalize(original). This prevents mis-translation if the prefix mapping is broad- // and an unrelated path happens to exist at the translated location.- let directive_path = symlink_mapping- .as_ref()- .and_then(|m| m.to_logical_path(path))- .filter(|translated| {- dunce::canonicalize(translated).ok() == dunce::canonicalize(path).ok()- })- .unwrap_or_else(|| path.to_path_buf());-+ let directive_path = to_logical_path(path);let path_str = directive_path.to_string_lossy();// Escape based on shell type. Both shell families use single-quoted strings// where contents are literal, but they escape embedded quotes differently:diff --git a/src/output/handlers.rs b/src/output/handlers.rsindex fe2c61110..43ef769cf 100644--- a/src/output/handlers.rs+++ b/src/output/handlers.rs@@ -250,7 +250,8 @@ fn print_switch_message_if_changed(return Ok(());};- let path_display = format_path_for_display(main_path);+ let logical_path = super::to_logical_path(main_path);+ let path_display = format_path_for_display(&logical_path);if super::is_shell_integration_active() {// Shell integration active - cd will work@@ -358,8 +359,10 @@ pub fn handle_switch_output(super::change_directory(&cd_target)?;}- let path = result.path();- let path_display = format_path_for_display(path);+ // Translate to the user's logical (symlink-preserved) path for display messages.+ // The cd directive (above) handles its own translation internally.+ let path = super::to_logical_path(result.path());+ let path_display = format_path_for_display(&path);let branch = &branch_info.branch;// Check if shell integration is active (directive file set)@@ -433,7 +436,7 @@ pub fn handle_switch_output(eprintln!("{}",info_message(format_switch_message(- branch, path, false, // worktree_created+ branch, &path, false, // worktree_createdfalse, // created_branchNone, None,))@@ -457,7 +460,7 @@ pub fn handle_switch_output("{}",success_message(format_switch_message(branch,- path,+ &path,true, // worktree_created*created_branch,base_branch.as_deref(),diff --git a/src/output/mod.rs b/src/output/mod.rsindex 145cc4daa..4c02bd48a 100644--- a/src/output/mod.rs+++ b/src/output/mod.rs@@ -38,7 +38,7 @@ pub(crate) mod shell_integration;// Re-export the public APIpub(crate) use global::{change_directory, execute, is_shell_integration_active, post_hook_display_path,- pre_hook_display_path, set_verbosity, terminate_output,+ pre_hook_display_path, set_verbosity, terminate_output, to_logical_path,};// Re-export output handlerspub(crate) use handlers::{diff --git a/tests/integration_tests/directives.rs b/tests/integration_tests/directives.rsindex fe0f9e7a0..efee3d655 100644--- a/tests/integration_tests/directives.rs+++ b/tests/integration_tests/directives.rs@@ -472,6 +472,21 @@ fn test_switch_preserves_symlink_path(#[from(repo_with_remote)] mut repo: TestRereal_prefix,directives);++ // Display messages (stderr) should also use the logical path+ let stderr = String::from_utf8_lossy(&output.stderr);+ assert!(+ stderr.contains(&*symlink_prefix),+ "Display message should contain logical path {}, got: {}",+ symlink_prefix,+ stderr+ );+ assert!(+ !stderr.contains(&*real_prefix),+ "Display message should NOT contain canonical path {}, got: {}",+ real_prefix,+ stderr+ );}#[cfg(unix)]@@ -510,6 +525,22 @@ fn test_switch_create_preserves_symlink_path(#[from(repo_with_remote)] repo: Tessymlink_prefix,directives);++ // Display messages (stderr) should also use the logical path+ let real_prefix = real_parent.to_string_lossy();+ let stderr = String::from_utf8_lossy(&output.stderr);+ assert!(+ stderr.contains(&*symlink_prefix),+ "Display message should contain logical path {}, got: {}",+ symlink_prefix,+ stderr+ );+ assert!(+ !stderr.contains(&*real_prefix),+ "Display message should NOT contain canonical path {}, got: {}",+ real_prefix,+ stderr+ );}#[cfg(unix)]