#17325: fix(completion): avoid zsh compdef error when compinit is not initialized
cli
stale
size: XS
Cluster:
Zsh and PowerShell Completion Fixes
## Summary
- update generated zsh completion script to bootstrap `compinit` when `compdef` is unavailable
- prevents `command not found: compdef` for users sourcing completion in shells without prior `compinit` setup
- add unit test coverage for the zsh script bootstrap block
## Validation
- reproduced failure before fix: `zsh -f -c 'source <(openclaw completion --shell zsh)'` -> exit 127 (compdef missing)
- verified after fix: same command exits 0
- `pnpm vitest src/cli/completion-cli.test.ts` passes
- `pnpm build` passes
Closes #14289
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Adds a `compinit` bootstrap guard to the generated zsh completion script so that `source <(openclaw completion --shell zsh)` works in shells where `compinit` has not been initialized (e.g., `zsh -f`). The guard uses `command -v compdef` to detect whether the completion system is loaded, and only runs `autoload -Uz compinit && compinit` when it is not — a well-established pattern used by other CLI tools. The `#compdef` directive is preserved for fpath-based loading, and when `compdef` is already available the guard is a no-op.
- `generateZshCompletion` is now exported (was internal) to enable direct unit testing
- A new test file (`completion-cli.test.ts`) verifies the bootstrap block is present in the generated script
- No changes to bash, fish, or PowerShell completion generators
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge — it adds a well-known defensive guard to shell script output with no risk to existing functionality.
- The change is minimal and targeted: a 5-line shell guard added to a template string, plus a visibility change (export) and a new test file. The bootstrap pattern (check for compdef, conditionally run compinit) is standard practice in zsh completion scripts. The guard is a no-op when compinit is already loaded, so it cannot break existing setups. The test follows established patterns in the codebase.
- No files require special attention
<sub>Last reviewed commit: f1f6b2d</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#20553: fix(completion): guard zsh compdef call for environments without co...
by mr-sk · 2026-02-19
91.6%
#18938: fix(completion): avoid compdef error in zsh by guarding completion ...
by gcsenyan · 2026-02-17
91.4%
#19109: fix(completion): initialize compinit before compdef
by Clawborn · 2026-02-17
90.7%
#17437: fix(completion): avoid rc=1 for optionless zsh leaf commands
by ephelia-ai · 2026-02-15
80.6%
#12308: fix(cli): redirect log output to stderr during completion script ge...
by mcaxtr · 2026-02-09
79.6%
#6382: fix(cli): silence Powerlevel10k Zsh warning from completion script
by dungngo4520 · 2026-02-01
79.4%
#22488: fix(cli): redirect plugin logs to stderr during completion
by pierreeurope · 2026-02-21
77.3%
#11165: fix(cli): filter empty flags in zsh and PowerShell nested completion
by Yida-Dev · 2026-02-07
76.6%
#9148: Fix: Speed up shell completion generation from ~4.6s to <200ms
by vishaltandale00 · 2026-02-04
76.4%
#6694: fix: Cache completion install
by s0up4200 · 2026-02-01
76.2%