Skip to content

fix: guard zsh compdef call to prevent error before compinit#422

Merged
ilblackdragon merged 2 commits intonearai:mainfrom
zmanian:fix/zsh-compdef-guard
Mar 1, 2026
Merged

fix: guard zsh compdef call to prevent error before compinit#422
ilblackdragon merged 2 commits intonearai:mainfrom
zmanian:fix/zsh-compdef-guard

Conversation

@zmanian
Copy link
Copy Markdown
Collaborator

@zmanian zmanian commented Feb 28, 2026

Summary

  • Wraps the compdef call in ironclaw.zsh with (( $+functions[compdef] )) guard
  • Prevents "compdef: command not found" error when the file is sourced before compinit runs in the user's .zshrc
  • Standard pattern used by rustup, starship, and other clap_complete-generated completions

What changed

One line in ironclaw.zsh:

-    compdef _ironclaw ironclaw
+    (( $+functions[compdef] )) && compdef _ironclaw ironclaw

Test plan

  • Source ironclaw.zsh before compinit -- no error
  • Source ironclaw.zsh after compinit -- completions work normally
  • ironclaw completion --shell zsh still generates valid output (note: regenerating will overwrite this fix; a post-generation patch step may be needed long-term)

Closes #420

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 compdef command. This change addresses a common issue where sourcing the completion file before compinit could lead to errors, ensuring a smoother experience for users integrating the script into their shell configurations.

Highlights

  • Error Prevention: Implemented a guard around the compdef call in ironclaw.zsh to prevent a "compdef: command not found" error when the file is sourced before compinit runs.
  • Standard Practice: Adopted a common pattern used by other tools like rustup and starship for handling compdef calls in Zsh completion scripts.
  • Improved Robustness: Ensured that the Zsh completion script can be sourced reliably regardless of when compinit is executed in the user's .zshrc.
Changelog
  • ironclaw.zsh
    • Wrapped the compdef call with a conditional check to ensure the compdef function is available before execution.
Activity
  • The pull request was opened by zmanian to address an issue with Zsh completion script loading.
  • The author provided a detailed summary, explanation of changes, and a test plan.
  • No further human activity or reviews have been recorded yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions Bot added size: XS < 10 changed lines (excluding docs) risk: low Changes to docs, tests, or low-risk modules contributor: experienced 6-19 merged PRs labels Feb 28, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread ironclaw.zsh
_ironclaw "$@"
else
compdef _ironclaw ironclaw
(( $+functions[compdef] )) && compdef _ironclaw ironclaw
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

  1. Upgrade clap_complete: The cleanest solution would be to upgrade clap and clap_complete dependencies. Newer versions automatically include this guard for Zsh completions, resolving the issue at the source.

  2. Patch during generation: If upgrading is not feasible right now, you could modify the completion generation logic in src/cli/completion.rs to 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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions github-actions Bot added scope: channel/cli TUI / CLI channel size: L 200-499 changed lines and removed size: XS < 10 changed lines (excluding docs) labels Feb 28, 2026
zmanian and others added 2 commits February 28, 2026 21:38
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>
@zmanian zmanian force-pushed the fix/zsh-compdef-guard branch from e8f9d72 to a49cf09 Compare March 1, 2026 05:38
@ilblackdragon ilblackdragon merged commit c6bfd18 into nearai:main Mar 1, 2026
13 checks passed
@github-actions github-actions Bot mentioned this pull request Mar 1, 2026
zmanian added a commit to zmanian/ironclaw that referenced this pull request Mar 1, 2026
)

* 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>
bkutasi pushed a commit to bkutasi/ironclaw that referenced this pull request Mar 28, 2026
)

* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor: experienced 6-19 merged PRs risk: low Changes to docs, tests, or low-risk modules scope: channel/cli TUI / CLI channel size: L 200-499 changed lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

zsh completions file calls compdef without guard, fails if sourced before compinit

2 participants