Skip to content

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

feat: Defer ZSH completion registration until compinit

Maximilian Roos1b7714e73b30
diff --git a/templates/zsh.zsh b/templates/zsh.zsh
index 9af17dea9..decb5e0a3 100644
--- a/templates/zsh.zsh
+++ b/templates/zsh.zsh
@@ -75,8 +75,21 @@ if command -v {{ cmd_prefix }} >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]];
fi
}
- # Register stub, but only if compdef is available (compinit was run)
- if (( $+functions[compdef] )); then
- compdef _wt_lazy_complete {{ cmd_prefix }}
+ # Register completion - either now or deferred until compinit runs
+ _wt_register_completion() {
+ if (( $+functions[compdef] )); then
+ compdef _wt_lazy_complete {{ cmd_prefix }}
+ # Remove hook once registered
+ precmd_functions=(${precmd_functions:#_wt_register_completion})
+ unfunction _wt_register_completion 2>/dev/null
+ return 0
+ fi
+ return 1
+ }
+
+ # Try immediate registration, otherwise defer via precmd hook
+ if ! _wt_register_completion; then
+ # Add to hook only if not already present (handles re-sourcing .zshrc)
+ (( ${precmd_functions[(I)_wt_register_completion]} )) || precmd_functions+=(_wt_register_completion)
fi
fi
diff --git a/tests/integration_tests/shell_wrapper.rs b/tests/integration_tests/shell_wrapper.rs
index 673997997..5fffcca61 100644
--- a/tests/integration_tests/shell_wrapper.rs
+++ b/tests/integration_tests/shell_wrapper.rs
@@ -203,12 +203,6 @@ fn build_shell_script(shell: &str, repo: &TestRepo, subcommand: &str, args: &[&s
script.push_str("set -x CLICOLOR_FORCE 1\n");
}
"zsh" => {
- // For zsh, initialize the completion system first
- // This allows static completions (which call compdef) to work in isolated mode
- // We run with --no-rcs to prevent user rc files from touching /dev/tty,
- // but compinit is safe since it only sets up completion functions
- script.push_str("autoload -Uz compinit && compinit -i 2>/dev/null\n");
-
script.push_str(&format!("export WORKTRUNK_BIN='{}'\n", wt_bin.display()));
script.push_str(&format!(
"export WORKTRUNK_CONFIG_PATH='{}'\n",
@@ -1399,7 +1393,6 @@ approved-commands = ["echo 'fish background task'"]
script.push_str("set -x CLICOLOR_FORCE 1\n");
}
"zsh" => {
- script.push_str("autoload -Uz compinit && compinit -i 2>/dev/null\n");
script.push_str(&format!("export WORKTRUNK_BIN='{}'\n", wt_bin.display()));
script.push_str(&format!(
"export WORKTRUNK_CONFIG_PATH='{}'\n",
@@ -1679,12 +1672,16 @@ approved-commands = ["echo 'bash background'"]
let wrapper_script = generate_wrapper(&repo, "zsh");
// Script that sources wrapper and checks if completion function exists
+ // Tests deferred registration: compinit runs AFTER wrapper (like real .zshrc)
let script = format!(
r#"
- autoload -Uz compinit && compinit -i 2>/dev/null
export WORKTRUNK_BIN='{}'
export WORKTRUNK_CONFIG_PATH='{}'
{}
+ # Run compinit after wrapper (simulates typical user .zshrc order)
+ autoload -Uz compinit && compinit -i 2>/dev/null
+ # Trigger precmd hook to complete deferred registration
+ for func in $precmd_functions; do $func; done
# Check if lazy completion stub is registered
if (( $+functions[_wt_lazy_complete] )); then
echo "__COMPLETION_REGISTERED__"
@@ -1784,7 +1781,6 @@ approved-commands = ["echo 'bash background'"]
let script = match shell {
"zsh" => format!(
r#"
- autoload -Uz compinit && compinit -i 2>/dev/null
# Clear PATH to ensure wt is not found via PATH
export PATH="/usr/bin:/bin"
export WORKTRUNK_BIN='{}'
diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
index 738fbcee8..7716cc2d8 100644
--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
@@ -187,9 +187,22 @@ wt_exec() {
fi
}
- # Register stub, but only if compdef is available (compinit was run)
- if (( $+functions[compdef] )); then
- compdef _wt_lazy_complete wt
+ # Register completion - either now or deferred until compinit runs
+ _wt_register_completion() {
+ if (( $+functions[compdef] )); then
+ compdef _wt_lazy_complete wt
+ # Remove hook once registered
+ precmd_functions=(${precmd_functions:#_wt_register_completion})
+ unfunction _wt_register_completion 2>/dev/null
+ return 0
+ fi
+ return 1
+ }
+
+ # Try immediate registration, otherwise defer via precmd hook
+ if ! _wt_register_completion; then
+ # Add to hook only if not already present (handles re-sourcing .zshrc)
+ (( ${precmd_functions[(I)_wt_register_completion]} )) || precmd_functions+=(_wt_register_completion)
fi
fi