← Back to PRs

#17325: fix(completion): avoid zsh compdef error when compinit is not initialized

by ephelia-ai open 2026-02-15 17:03 View on GitHub →
cli stale size: XS
## 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