Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix: Guard compdef call in zsh static completions for --no-rcs compatibility

Maximilian Roos44f07e612513
diff --git a/src/commands/init.rs b/src/commands/init.rs
index bc6eb4601..9e8406390 100644
--- a/src/commands/init.rs
+++ b/src/commands/init.rs
@@ -70,7 +70,22 @@ pub fn handle_init(shell_name: &str, cmd_name: &str, cli_cmd: &mut Command) -> R
line.to_string()
};
- println!("{}", line);
+ // For zsh, wrap the final compdef call in a guard to prevent errors in --no-rcs mode
+ // The clap-generated completions end with:
+ // if [ "$funcstack[1]" = "_wt" ]; then
+ // _wt "$@"
+ // else
+ // compdef _wt wt
+ // fi
+ // We need to check if compdef exists before calling it in the else branch
+ if matches!(shell, shell::Shell::Zsh) && line.trim() == "compdef _wt wt" {
+ // Wrap the compdef call in a guard (preserve 4-space indentation from else block)
+ println!(" if (( $+functions[compdef] )); then");
+ println!(" compdef _wt wt");
+ println!(" fi");
+ } else {
+ println!("{}", line);
+ }
}
Ok(())
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__command_failure_zsh.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__command_failure_zsh.snap
index ce8b21c1b..dc69189d5 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__command_failure_zsh.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__command_failure_zsh.snap
@@ -2,7 +2,6 @@
source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized().as_ref()
---
-zsh:365: command not found: compdef
❌ Branch existing already exists
πŸ’‘ Remove --create flag to switch to it
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_zsh.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_zsh.snap
index 29eac739a..8903390cb 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_zsh.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_zsh.snap
@@ -2,7 +2,6 @@
source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized().as_ref()
---
-zsh:365: command not found: compdef
βœ“ Code formatting check passed
βœ— Test suite failed: 3 tests failing
❌ Pre-merge command failed: test
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_zsh.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_zsh.snap
index 3474951f1..6f85a617a 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_zsh.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_zsh.snap
@@ -2,7 +2,6 @@
source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized().as_ref()
---
-zsh:365: command not found: compdef
βœ“ Code formatting check passed
βœ“ Linting passed - no warnings
βœ“ All 47 tests passed in 2.3s
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_zsh.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_zsh.snap
index e96b53264..f0f2b867a 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_zsh.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_zsh.snap
@@ -2,5 +2,4 @@
source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized().as_ref()
---
-zsh:365: command not found: compdef
βœ… Already on main, nothing to merge
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove_zsh.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove_zsh.snap
index 53e9e9543..8b824a4bb 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove_zsh.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove_zsh.snap
@@ -2,6 +2,5 @@
source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized().as_ref()
---
-zsh:365: command not found: compdef
πŸ”„ Removing worktree for to-remove...
βœ… Removed worktree for to-remove
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_create_zsh.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_create_zsh.snap
index 4039cc3a7..6d6577aca 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_create_zsh.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_create_zsh.snap
@@ -2,5 +2,4 @@
source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized().as_ref()
---
-zsh:365: command not found: compdef
βœ… Created new worktree for feature at [TMPDIR]/test-repo.feature
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_execute_zsh.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_execute_zsh.snap
index aabd3606d..8bef55bb1 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_execute_zsh.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_execute_zsh.snap
@@ -2,7 +2,6 @@
source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized().as_ref()
---
-zsh:365: command not found: compdef
βœ… Created new worktree for test-exec at [TMPDIR]/test-repo.test-exec
πŸ”„ Executing (--execute):
  echo executed
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_zsh.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_zsh.snap
index 408cbd06e..351825872 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_zsh.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_zsh.snap
@@ -2,7 +2,6 @@
source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized().as_ref()
---
-zsh:365: command not found: compdef
Installing dependencies...
Building project...
πŸ”„ Executing (post-create):
diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
index 6fe1c7735..868cbe0ff 100644
--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
@@ -16,7 +16,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpelDWZF/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpw2OXOa/test-config.toml
---
success: true
exit_code: 0
@@ -382,7 +382,9 @@ _wt__switch_commands() {
if [ "$funcstack[1]" = "_wt" ]; then
_wt "$@"
else
- compdef _wt wt
+ if (( $+functions[compdef] )); then
+ compdef _wt wt
+ fi
fi
----- stderr -----