Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(fish): prevent getcwd error after worktree removal in Zellij (#1787)
Maximilian Roosfd00dbd8bff0
diff --git a/src/commands/process.rs b/src/commands/process.rsindex 8d808d3ed..7e82bdff8 100644--- a/src/commands/process.rs+++ b/src/commands/process.rs@@ -442,14 +442,12 @@ pub fn build_remove_command(let worktree_path_str = worktree_path.to_string_lossy();let worktree_escaped = escape(worktree_path_str.as_ref().into());- // TODO: This delay is a timing-based workaround, not a principled fix.- // The race: after wt exits, the shell wrapper reads the directive file and- // runs `cd`. But fish (and other shells) may call getcwd() before the cd- // completes (e.g., for prompt updates), and if the background removal has- // already deleted the directory, we get "shell-init: error retrieving current- // directory". A 1s delay is very conservative (shell cd takes ~1-5ms), but- // deterministic solutions (shell-spawned background, marker file sync) add- // significant complexity for marginal benefit.+ // Delay before deleting the worktree directory. After wt exits, the shell+ // wrapper reads the directive file and runs `cd`. The 1s delay ensures the+ // shell has finished cd'ing before the directory is removed. The primary fix+ // for the "shell-init: error retrieving current directory" race is in the+ // fish wrapper (using builtins instead of subprocesses to read the directive),+ // but this delay provides defense in depth for other shells and edge cases.let delay = "sleep 1";// Stop fsmonitor daemon first (best effort - ignore errors)diff --git a/src/shell/snapshots/worktrunk__shell__tests__init_fish.snap b/src/shell/snapshots/worktrunk__shell__tests__init_fish.snapindex 646f0d2a4..8fd83aaa8 100644--- a/src/shell/snapshots/worktrunk__shell__tests__init_fish.snap+++ b/src/shell/snapshots/worktrunk__shell__tests__init_fish.snap@@ -11,10 +11,11 @@ expression: output# Creates a temp file, passes path via WORKTRUNK_DIRECTIVE_FILE, evals it after.# WORKTRUNK_BIN can override the binary path (for testing dev builds).#-# Note: We use `eval (cat ... | string collect)` instead of `source` because:-# 1. fish's `source` doesn't propagate exit codes to the parent function-# 2. `eval (cat ...)` without `string collect` splits on newlines, breaking multiline directives-# With `string collect`, we get proper exit code propagation for cd and other directives.+# Note: We use `eval (string collect < file)` instead of `source` because+# fish's `source` doesn't propagate exit codes to the parent function.+# We read the directive with `string collect` (builtin) instead of `cat`+# (external) to avoid spawning a subprocess whose CWD may have been renamed+# by worktree removal.function wtset -l use_source falseset -l args@@ -39,13 +40,16 @@ function wtset -l exit_code $statusif test -s "$directive_file"- eval (cat "$directive_file" | string collect)+ # Use fish builtin instead of cat to avoid spawning a subprocess+ # whose CWD may have been renamed by worktree removal.+ set -l directive (string collect < "$directive_file")+ eval $directiveif test $exit_code -eq 0set exit_code $statusendend- rm -f "$directive_file"+ command rm -f "$directive_file"return $exit_codeenddiff --git a/templates/fish.fish b/templates/fish.fishindex 5902431e0..e4d3e1f15 100644--- a/templates/fish.fish+++ b/templates/fish.fish@@ -7,10 +7,11 @@# Creates a temp file, passes path via WORKTRUNK_DIRECTIVE_FILE, evals it after.# WORKTRUNK_BIN can override the binary path (for testing dev builds).#-# Note: We use `eval (cat ... | string collect)` instead of `source` because:-# 1. fish's `source` doesn't propagate exit codes to the parent function-# 2. `eval (cat ...)` without `string collect` splits on newlines, breaking multiline directives-# With `string collect`, we get proper exit code propagation for cd and other directives.+# Note: We use `eval (string collect < file)` instead of `source` because+# fish's `source` doesn't propagate exit codes to the parent function.+# We read the directive with `string collect` (builtin) instead of `cat`+# (external) to avoid spawning a subprocess whose CWD may have been renamed+# by worktree removal.function {{ cmd }}set -l use_source falseset -l args@@ -35,13 +36,16 @@ function {{ cmd }}set -l exit_code $statusif test -s "$directive_file"- eval (cat "$directive_file" | string collect)+ # Use fish builtin instead of cat to avoid spawning a subprocess+ # whose CWD may have been renamed by worktree removal.+ set -l directive (string collect < "$directive_file")+ eval $directiveif test $exit_code -eq 0set exit_code $statusendend- rm -f "$directive_file"+ command rm -f "$directive_file"return $exit_codeenddiff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snapindex 9bbdc4dc4..340c1a0c2 100644--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap@@ -21,16 +21,20 @@ info:LANG: CLC_ALL: CMOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"+ NO_COLOR: ""PATH: "[PATH]"PSModulePath: ""RUST_LOG: warnSHELL: ""TERM: alacrittyUSERPROFILE: "[TEST_HOME]"+ WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+ WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"WORKTRUNK_TEST_EPOCH: "1735776000"+ WORKTRUNK_TEST_NUSHELL_ENV: "0"WORKTRUNK_TEST_POWERSHELL_ENV: "0"WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"@@ -47,10 +51,11 @@ exit_code: 0# Creates a temp file, passes path via WORKTRUNK_DIRECTIVE_FILE, evals it after.# WORKTRUNK_BIN can override the binary path (for testing dev builds).#-# Note: We use `eval (cat ... | string collect)` instead of `source` because:-# 1. fish's `source` doesn't propagate exit codes to the parent function-# 2. `eval (cat ...)` without `string collect` splits on newlines, breaking multiline directives-# With `string collect`, we get proper exit code propagation for cd and other directives.+# Note: We use `eval (string collect < file)` instead of `source` because+# fish's `source` doesn't propagate exit codes to the parent function.+# We read the directive with `string collect` (builtin) instead of `cat`+# (external) to avoid spawning a subprocess whose CWD may have been renamed+# by worktree removal.function wtset -l use_source falseset -l args@@ -75,13 +80,16 @@ function wtset -l exit_code $statusif test -s "$directive_file"- eval (cat "$directive_file" | string collect)+ # Use fish builtin instead of cat to avoid spawning a subprocess+ # whose CWD may have been renamed by worktree removal.+ set -l directive (string collect < "$directive_file")+ eval $directiveif test $exit_code -eq 0set exit_code $statusendend- rm -f "$directive_file"+ command rm -f "$directive_file"return $exit_codeend