fix: guard zsh compdef call to prevent error before compinit#422
fix: guard zsh compdef call to prevent error before compinit#422ilblackdragon merged 2 commits intonearai:mainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the robustness of the Zsh completion script by conditionally executing the Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly fixes an issue where the Zsh completion script could error if sourced before compinit. The change adds a standard guard to check for the existence of the compdef function. While the fix is correct, it's applied as a manual patch to a generated file, which is fragile. I've left a comment suggesting a more permanent solution by either upgrading the clap_complete dependency or modifying the completion generation logic to apply this patch automatically, preventing future regressions.
| _ironclaw "$@" | ||
| else | ||
| compdef _ironclaw ironclaw | ||
| (( $+functions[compdef] )) && compdef _ironclaw ironclaw |
There was a problem hiding this comment.
This change correctly fixes the immediate issue. However, as you've noted, this manual patch to a generated file is fragile and will be lost upon regeneration. To create a more robust and permanent solution, there are two main approaches.
-
Upgrade
clap_complete: The cleanest solution would be to upgradeclapandclap_completedependencies. Newer versions automatically include this guard for Zsh completions, resolving the issue at the source. -
Patch during generation: If upgrading is not feasible right now, you could modify the completion generation logic in
src/cli/completion.rsto apply this guard automatically. This prevents future regressions from regenerating the file.
Here's a potential implementation for the second approach:
// in src/cli/completion.rs, replace the existing `run` method
pub fn run(&self) -> anyhow::Result<()> {
let mut cmd = crate::cli::Cli::command();
let bin_name = cmd.get_name().to_string();
if self.shell == Shell::Zsh {
let mut buf = Vec::new();
generate(self.shell, &mut cmd, bin_name.clone(), &mut buf);
let script = String::from_utf8(buf)?;
let original = format!("compdef _{0} {0}", bin_name);
let guarded = format!("(( $+functions[compdef] )) && compdef _{0} {0}", bin_name);
let patched_script = script.replace(&original, &guarded);
std::io::stdout().write_all(patched_script.as_bytes())?;
} else {
generate(self.shell, &mut cmd, bin_name, &mut std::io::stdout());
}
Ok(())
}This ensures the Zsh completion script is always generated correctly.
There was a problem hiding this comment.
Good catch. Fixed in e8f9d72 -- the compdef guard is now applied during generation in src/cli/completion.rs rather than hand-patching the generated file. The Zsh output is post-processed to wrap the bare compdef call. Regenerated ironclaw.zsh from the patched code so future regenerations stay correct.
The generated ironclaw.zsh completions file calls compdef without checking if it exists. Users who source this file before compinit runs in their .zshrc get "compdef: command not found" on every terminal open. Wrap the call with the standard (( $+functions[compdef] )) guard. Closes nearai#420 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Instead of hand-patching the generated ironclaw.zsh file (which is fragile and lost on regeneration), patch the compdef call in the generation code itself. The Zsh output is post-processed to wrap `compdef _ironclaw ironclaw` with a `$+functions[compdef]` guard. Regenerated ironclaw.zsh from the patched code to stay in sync. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
e8f9d72 to
a49cf09
Compare
) * fix: guard zsh compdef call to prevent error before compinit The generated ironclaw.zsh completions file calls compdef without checking if it exists. Users who source this file before compinit runs in their .zshrc get "compdef: command not found" on every terminal open. Wrap the call with the standard (( $+functions[compdef] )) guard. Closes nearai#420 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(completions): apply compdef guard during zsh generation Instead of hand-patching the generated ironclaw.zsh file (which is fragile and lost on regeneration), patch the compdef call in the generation code itself. The Zsh output is post-processed to wrap `compdef _ironclaw ironclaw` with a `$+functions[compdef]` guard. Regenerated ironclaw.zsh from the patched code to stay in sync. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
) * fix: guard zsh compdef call to prevent error before compinit The generated ironclaw.zsh completions file calls compdef without checking if it exists. Users who source this file before compinit runs in their .zshrc get "compdef: command not found" on every terminal open. Wrap the call with the standard (( $+functions[compdef] )) guard. Closes nearai#420 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(completions): apply compdef guard during zsh generation Instead of hand-patching the generated ironclaw.zsh file (which is fragile and lost on regeneration), patch the compdef call in the generation code itself. The Zsh output is post-processed to wrap `compdef _ironclaw ironclaw` with a `$+functions[compdef]` guard. Regenerated ironclaw.zsh from the patched code to stay in sync. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Summary
compdefcall inironclaw.zshwith(( $+functions[compdef] ))guardcompinitruns in the user's.zshrcWhat changed
One line in
ironclaw.zsh:Test plan
ironclaw.zshbeforecompinit-- no errorironclaw.zshaftercompinit-- completions work normallyironclaw completion --shell zshstill generates valid output (note: regenerating will overwrite this fix; a post-generation patch step may be needed long-term)Closes #420