Skip to content

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.rs
index 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 path
diff --git a/templates/fish_wrapper.fish b/templates/fish_wrapper.fish
index 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 uninstall
function {{ 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
+ end
command {{ 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 ourselves
diff --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.snap
index 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
▲ Shell integration not active
  Invoked as: [PROJECT_ROOT]/target/[BUILD_MODE]/wt
-○ fish: Already configured shell extension @ ~/.config/fish/functions/wt.fish:7
+○ fish: Already configured shell extension @ ~/.config/fish/functions/wt.fish:15
  command wt config shell init fish | source
○ fish: Already configured completions @ ~/.config/fish/completions/wt.fish
diff --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.snap
index 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
▲ Shell integration not active
  Invoked as: [PROJECT_ROOT]/target/[BUILD_MODE]/wt
-○ fish: Already configured shell extension @ ~/.config/fish/functions/wt.fish:7
+○ fish: Already configured shell extension @ ~/.config/fish/functions/wt.fish:15
  command wt config shell init fish | source
▲ fish: Completions not configured @ ~/.config/fish/completions/wt.fish
↳ To configure completions, run wt config shell install fish
diff --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.snap
index 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
  # Check: wt config show | Uninstall: wt config shell uninstall
  
  function wt
-   command wt config shell init fish | source
+   # 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 wt $argv
+   return
+   end
+   command wt 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 ourselves
-   set -l wt_status $pipestatus[1]
+   set -l wt_status $pipestatus[1]
   set -l source_status $pipestatus[2]
   test $wt_status -eq 0; or return $wt_status
   test $source_status -eq 0; or return $source_status
   wt $argv
-  end
+  end
○ Will create completions for fish @ ~/.config/fish/completions/wt.fish
  # worktrunk completions for fish