#18938: fix(completion): avoid compdef error in zsh by guarding completion init
cli
size: XS
Cluster:
Zsh and PowerShell Completion Fixes
## Summary
- fix `command not found: compdef` for zsh users with minimal shell setup
- make generated zsh completion source block interactive-only
- ensure `compinit` is available before sourcing cached completion script
## What changed
In `src/cli/completion-cli.ts`, `formatCompletionSourceLine(...)` now emits this for zsh:
```zsh
if [[ -o interactive ]]; then
autoload -Uz compinit
(( $+functions[compdef] )) || compinit
source "<cache-path>"
fi
```
## Why
The previous install block only sourced the completion script. In shells where `compinit` had not run yet (common in minimal/clean `~/.zshrc` setups, especially over SSH), the script's `compdef` calls failed.
## Notes
- behavior for bash/fish is unchanged
- this preserves completion behavior for interactive shells while avoiding startup errors in non-interactive contexts
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Fixed `command not found: compdef` error in zsh by wrapping completion initialization in an interactive-only guard that ensures `compinit` is loaded before sourcing the cached completion script.
- Wrapped zsh completion source block with `[[ -o interactive ]]` check
- Added `autoload -Uz compinit` to load the completion system
- Added `(( $+functions[compdef] )) || compinit` to initialize only if `compdef` is missing
- Prevented startup errors in non-interactive contexts (e.g., SSH sessions, minimal `.zshrc` setups)
<h3>Confidence Score: 5/5</h3>
- Safe to merge - addresses a specific zsh initialization error with a standard solution
- The fix follows zsh best practices by checking for interactive mode, loading compinit only when needed, and preserving bash/fish behavior. The implementation is minimal, targeted, and uses standard zsh idioms (`[[ -o interactive ]]`, `autoload -Uz`, `$+functions[compdef]`). No behavior changes for other shells.
- No files require special attention
<sub>Last reviewed commit: e145af6</sub>
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#17325: fix(completion): avoid zsh compdef error when compinit is not initi...
by ephelia-ai · 2026-02-15
91.4%
#20553: fix(completion): guard zsh compdef call for environments without co...
by mr-sk · 2026-02-19
88.3%
#19109: fix(completion): initialize compinit before compdef
by Clawborn · 2026-02-17
87.7%
#17437: fix(completion): avoid rc=1 for optionless zsh leaf commands
by ephelia-ai · 2026-02-15
79.9%
#6382: fix(cli): silence Powerlevel10k Zsh warning from completion script
by dungngo4520 · 2026-02-01
79.3%
#9148: Fix: Speed up shell completion generation from ~4.6s to <200ms
by vishaltandale00 · 2026-02-04
78.0%
#22488: fix(cli): redirect plugin logs to stderr during completion
by pierreeurope · 2026-02-21
77.1%
#12308: fix(cli): redirect log output to stderr during completion script ge...
by mcaxtr · 2026-02-09
77.0%
#9158: perf(completion): add fast path for cached completions
by gavinbmoore · 2026-02-04
76.0%
#11165: fix(cli): filter empty flags in zsh and PowerShell nested completion
by Yida-Dev · 2026-02-07
75.7%