Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(shell): guard fish lazy-load wrapper against completion-mode recursion (#3250)
Worktrunk Botc2b7ab683edb
diff --git a/src/shell/mod.rs b/src/shell/mod.rsindex 5b077ff26..9a6f54b40 100644--- a/src/shell/mod.rs+++ b/src/shell/mod.rs@@ -408,6 +408,36 @@ mod tests {);}+ #[test]+ fn test_fish_wrapper_guards_completion_mode() {+ // Regression for #3240: the lazy-load wrapper stub must short-circuit+ // when COMPLETE is set, calling the binary directly rather than falling+ // through to `wt config shell init fish | source` and the trailing+ // `wt $argv` self-call. Without the guard, a stale third-party+ // completion that invokes the bare `wt` command (e.g. an old Homebrew+ // vendor_completions.d/wt.fish) re-enters the stub and recurses to+ // fish's call-stack limit. Mirrors the bash/zsh COMPLETE guard.+ let wrapper = ShellInit::with_prefix(Shell::Fish, "wt".to_string())+ .generate_fish_wrapper()+ .expect("Failed to generate fish wrapper");+ assert!(+ wrapper.contains("set -q COMPLETE"),+ "fish wrapper must guard completion mode to avoid recursion (#3240); got:\n{wrapper}"+ );+ // The guard must precede the re-sourcing self-call so completion never+ // reaches it.+ let guard = wrapper+ .find("set -q COMPLETE")+ .expect("guard present per assertion above");+ let resource = wrapper+ .find("config shell init fish | source")+ .expect("wrapper sources the full integration");+ assert!(+ guard < resource,+ "the COMPLETE guard must come before the re-sourcing self-call; got:\n{wrapper}"+ );+ }+#[test]fn test_shell_config_paths_returns_paths() {// All shells should return at least one config pathdiff --git a/templates/fish_wrapper.fish b/templates/fish_wrapper.fishindex a12ecc980..6b326969c 100644--- a/templates/fish_wrapper.fish+++ b/templates/fish_wrapper.fish@@ -4,6 +4,14 @@# Check: {{ cmd }} config show | Uninstall: {{ cmd }} config shell uninstallfunction {{ cmd }}+ # Completion mode: let the binary emit completions directly. A stale+ # third-party completion (e.g. an old Homebrew vendor_completions.d/wt.fish)+ # may invoke the bare `wt` command with COMPLETE set; without this guard+ # that lands back on this stub and recurses. Mirrors the bash/zsh guard.+ if set -q COMPLETE+ command {{ cmd }} $argv+ return+ endcommand {{ cmd }} config shell init fish | source# Check both command exit code ($pipestatus[1]) and source exit code ($pipestatus[2])# If source fails, the function isn't replaced and we'd infinite-loop calling ourselvesdiff --git a/tests/snapshots/integration__integration_tests__config_show__config_show_fish_with_completions.snap b/tests/snapshots/integration__integration_tests__config_show__config_show_fish_with_completions.snapindex ea850c67a..b0aca9040 100644--- a/tests/snapshots/integration__integration_tests__config_show__config_show_fish_with_completions.snap+++ b/tests/snapshots/integration__integration_tests__config_show__config_show_fish_with_completions.snap@@ -55,7 +55,7 @@ exit_code: 0[33m▲[39m [33mShell integration not active[39m[107m [0m Invoked as: [1m[PROJECT_ROOT]/target/[BUILD_MODE]/wt[22m-[2m○[22m [1mfish[22m: Already configured shell extension @ ~/.config/fish/functions/wt.fish:7+[2m○[22m [1mfish[22m: Already configured shell extension @ ~/.config/fish/functions/wt.fish:15[107m [0m [2m[0m[2m[34mcommand[0m[2m wt config shell init fish [0m[2m[36m|[0m[2m [0m[2m[34msource[0m[2m○[22m [1mfish[22m: Already configured completions @ ~/.config/fish/completions/wt.fishdiff --git a/tests/snapshots/integration__integration_tests__config_show__config_show_fish_without_completions.snap b/tests/snapshots/integration__integration_tests__config_show__config_show_fish_without_completions.snapindex e3a949f29..f310c8f28 100644--- a/tests/snapshots/integration__integration_tests__config_show__config_show_fish_without_completions.snap+++ b/tests/snapshots/integration__integration_tests__config_show__config_show_fish_without_completions.snap@@ -55,7 +55,7 @@ exit_code: 0[33m▲[39m [33mShell integration not active[39m[107m [0m Invoked as: [1m[PROJECT_ROOT]/target/[BUILD_MODE]/wt[22m-[2m○[22m [1mfish[22m: Already configured shell extension @ ~/.config/fish/functions/wt.fish:7+[2m○[22m [1mfish[22m: Already configured shell extension @ ~/.config/fish/functions/wt.fish:15[107m [0m [2m[0m[2m[34mcommand[0m[2m wt config shell init fish [0m[2m[36m|[0m[2m [0m[2m[34msource[0m[33m▲[39m [33m[1mfish[22m: Completions not configured @ [1m~/.config/fish/completions/wt.fish[22m[39m[2m↳[22m [2mTo configure completions, run [4mwt config shell install fish[24m[22mdiff --git a/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_fish_dry_run.snap b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_fish_dry_run.snapindex 2d9217a3f..0973bbc0f 100644--- a/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_fish_dry_run.snap+++ b/tests/snapshots/integration__integration_tests__configure_shell__configure_shell_fish_dry_run.snap@@ -55,15 +55,23 @@ exit_code: 0[107m [0m [2m# Check: wt config show | Uninstall: wt config shell uninstall[0m[107m [0m [0m[107m [0m [2m[0m[2m[35mfunction[0m[2m wt[0m-[107m [0m [2m command wt config shell init fish [0m[2m[36m|[0m[2m source[0m+[107m [0m [2m # Completion mode: let the binary emit completions directly. A stale[0m+[107m [0m [2m # third-party completion (e.g. an old Homebrew vendor_completions.d/wt.fish)[0m+[107m [0m [2m # may invoke the bare `wt` command with COMPLETE set; without this guard[0m+[107m [0m [2m # that lands back on this stub and recurses. Mirrors the bash/zsh guard.[0m+[107m [0m [2m [0m[2m[35mif[0m[2m [0m[2m[34mset[0m[2m [0m[2m[36m-q[0m[2m COMPLETE[0m+[107m [0m [2m [0m[2m[34mcommand[0m[2m wt [0m[2m[36m$[0m[2margv[0m+[107m [0m [2m [0m[2m[34mreturn[0m+[107m [0m [2m [0m[2m[34mend[0m+[107m [0m [2m [0m[2m[34mcommand[0m[2m wt config shell init fish [0m[2m[36m|[0m[2m [0m[2m[34msource[0m[107m [0m [2m # Check both command exit code ($pipestatus[1]) and source exit code ($pipestatus[2])[0m[107m [0m [2m # If source fails, the function isn't replaced and we'd infinite-loop calling ourselves[0m-[107m [0m [2m set -l wt_status [0m[2m[36m$[0m[2mpipestatus[1][0m+[107m [0m [2m [0m[2m[34mset[0m[2m [0m[2m[36m-l[0m[2m wt_status [0m[2m[36m$[0m[2mpipestatus[1][0m[107m [0m [2m [0m[2m[34mset[0m[2m [0m[2m[36m-l[0m[2m source_status [0m[2m[36m$[0m[2mpipestatus[2][0m[107m [0m [2m [0m[2m[34mtest[0m[2m [0m[2m[36m$[0m[2mwt_status [0m[2m[36m-eq[0m[2m 0; [0m[2m[34mor[0m[2m return [0m[2m[36m$[0m[2mwt_status[0m[107m [0m [2m [0m[2m[34mtest[0m[2m [0m[2m[36m$[0m[2msource_status [0m[2m[36m-eq[0m[2m 0; [0m[2m[34mor[0m[2m return [0m[2m[36m$[0m[2msource_status[0m[107m [0m [2m [0m[2m[34mwt[0m[2m [0m[2m[36m$[0m[2margv[0m-[107m [0m [2m[0m[2m[34mend[0m+[107m [0m [2mend[0m[2m○[22m Will create completions for [1mfish[0m @ [1m~/.config/fish/completions/wt.fish[0m[107m [0m [2m# worktrunk completions for fish[0m