← Back to PRs

#20553: fix(completion): guard zsh compdef call for environments without compinit

by mr-sk open 2026-02-19 02:46 View on GitHub →
cli size: XS
## Summary - Guard the `compdef` call in generated zsh completions with `$+functions[compdef]` so it doesn't error in non-interactive shells or environments where `compinit` hasn't run - Re-initialize the `_comps` associative array if it was lost (e.g. shell state snapshot/restore) to prevent `_comps: assignment to invalid subscript range` - Remove leading blank line so `#compdef` is on line 1 per zsh completion conventions ## Problem The generated zsh completion script calls `compdef` unconditionally. This fails in two scenarios: 1. **Non-interactive shells** (`zsh -c`, SSH command execution, remote editors like Zed) where `compinit` never runs → `command not found: compdef` 2. **Shell snapshot restore** (e.g. Claude Code, tmux-resurrect) where the `compdef` function survives but the `_comps` associative array is lost → `compdef:153: _comps: assignment to invalid subscript range` ## Test plan - [ ] Source the generated completion file in a non-interactive shell: `zsh -c "source ~/.openclaw/completions/openclaw.zsh"` — should produce no error - [ ] Source in an interactive shell with `compinit` — completions should register normally - [ ] Regenerate completions with `openclaw completion --write-state` and verify the guard is present 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- greptile_comment --> <h3>Greptile Summary</h3> Guards zsh completion's `compdef` call to prevent errors in non-interactive shells and shell snapshot/restore scenarios. The fix removes the leading blank line (putting `#compdef` on line 1 per zsh conventions), wraps the `compdef` call with a check for the function's existence, and re-initializes the `_comps` associative array if lost. <h3>Confidence Score: 5/5</h3> - This PR is safe to merge with minimal risk - The changes are surgical and targeted, fixing specific edge cases in zsh completion generation without modifying core logic. The guard pattern `$+functions[compdef]` is standard zsh practice, and the `_comps` re-initialization follows zsh completion conventions. No breaking changes or side effects expected. - No files require special attention <sub>Last reviewed commit: 0f44a24</sub> <!-- greptile_other_comments_section --> <sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub> <!-- /greptile_comment -->

Most Similar PRs