Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(remove): prevent Nushell PWD errors during staged worktree removal (#1508)
worktrunk-bot9506ea256389
diff --git a/src/commands/process.rs b/src/commands/process.rsindex 14ef9de9f..b5428e17a 100644--- a/src/commands/process.rs+++ b/src/commands/process.rs@@ -390,19 +390,34 @@ pub fn generate_removing_path(worktree_path: &Path) -> PathBuf {////// This is used after the worktree has been renamed to a staging path,/// git metadata has been pruned, and the branch has been deleted synchronously.-/// The command just does `rm -rf` on the staged directory.///-/// No sleep is needed because:-/// 1. The shell cd happens before the rename (directive file is written first)-/// 2. The original worktree path no longer exists immediately after rename-pub fn build_remove_command_staged(staged_path: &std::path::Path) -> String {+/// The delay mirrors `build_remove_command`'s `sleep 1`. After the rename,+/// a placeholder directory is created at `original_path` so the shell's+/// working directory remains valid until the wrapper has processed the `cd`+/// directive. Without this, shells that validate `$env.PWD` (notably Nushell)+/// emit errors between binary exit and the `cd`.+///+/// The background command removes both the placeholder and the staged directory.+pub fn build_remove_command_staged(+ staged_path: &std::path::Path,+ original_path: &std::path::Path,+) -> String {use shell_escape::escape;let staged_path_str = staged_path.to_string_lossy();let staged_escaped = escape(staged_path_str.as_ref().into());+ let original_path_str = original_path.to_string_lossy();+ let original_escaped = escape(original_path_str.as_ref().into());++ // sleep 1: give the shell wrapper time to cd away before removing the placeholder.+ // rmdir: remove the empty placeholder (safe — only removes empty directories).+ // rm -rf: remove the staged worktree contents.// Use -- to prevent option parsing for paths starting with -- format!("rm -rf -- {}", staged_escaped)+ format!(+ "sleep 1 && rmdir -- {} 2>/dev/null; rm -rf -- {}",+ original_escaped, staged_escaped+ )}/// Build shell command for background worktree removal (legacy path).@@ -597,11 +612,13 @@ mod tests {#[test]fn test_build_remove_command_staged() {let staged_path = PathBuf::from("/tmp/my-project.feature.wt-removing-1234567890");- assert_snapshot!(build_remove_command_staged(&staged_path), @"rm -rf -- /tmp/my-project.feature.wt-removing-1234567890");+ let original_path = PathBuf::from("/tmp/my-project.feature");+ assert_snapshot!(build_remove_command_staged(&staged_path, &original_path), @"sleep 1 && rmdir -- /tmp/my-project.feature 2>/dev/null; rm -rf -- /tmp/my-project.feature.wt-removing-1234567890");// Shell escaping for special characters (space in path)let special_path = PathBuf::from("/tmp/test worktree.wt-removing-123");- assert_snapshot!(build_remove_command_staged(&special_path), @"rm -rf -- '/tmp/test worktree.wt-removing-123'");+ let special_original = PathBuf::from("/tmp/test worktree");+ assert_snapshot!(build_remove_command_staged(&special_path, &special_original), @"sleep 1 && rmdir -- '/tmp/test worktree' 2>/dev/null; rm -rf -- '/tmp/test worktree.wt-removing-123'");}#[test]diff --git a/src/output/handlers.rs b/src/output/handlers.rsindex 62eb5473b..d10d65a39 100644--- a/src/output/handlers.rs+++ b/src/output/handlers.rs@@ -76,7 +76,14 @@ fn execute_instant_removal_or_fallback({log::debug!("Failed to delete branch {} synchronously: {}", branch, e);}- build_remove_command_staged(&staged_path)+ // Create an empty placeholder at the original path so the shell's working+ // directory ($env.PWD) remains valid until the wrapper has cd'd away.+ // Without this, shells that validate PWD (notably Nushell) emit errors+ // between binary exit and the cd directive executing.+ // Best-effort: if create_dir fails (permissions, race), the only effect+ // is that Nushell may still emit PWD errors — not a correctness issue.+ let _ = std::fs::create_dir(worktree_path);+ build_remove_command_staged(&staged_path, worktree_path)}Err(e) => {// Fallback: cross-filesystem, permissions, Windows file locking, etc.