Skip to content

Fix zsh nested subcommand option completion#22422

Merged
MikeMcQuaid merged 1 commit into
mainfrom
fix-zsh-subcommand-option-completion
May 27, 2026
Merged

Fix zsh nested subcommand option completion#22422
MikeMcQuaid merged 1 commit into
mainfrom
fix-zsh-subcommand-option-completion

Conversation

@ZhongRuoyu
Copy link
Copy Markdown
Member

@ZhongRuoyu ZhongRuoyu commented May 26, 2026

In zsh completion context, the words array excludes the command name, so subcommands are not matched and per-subcommand options are not suggested.

To fix that, use $words[1] instead of $words[2] when dispatching nested subcommands in generated zsh completions.

For reference, see zshcompsys's manpage 1, specifically "the words special array and the CURRENT special parameter are modified to refer only to the normal arguments when the action is executed or evaluated":

$ man zshcompsys | col -bx | grep -F -A 13 '*::message:action'
              *::message:action
              *:::message:action
                     This describes how arguments (usually non-option
                     arguments, those not beginning with - or +) are to be
                     completed when neither of the first two forms was
                     provided.  Any number of arguments can be completed in
                     this fashion.

                     With two colons before the message, the words special
                     array and the CURRENT special parameter are modified to
                     refer only to the normal arguments when the action is
                     executed or evaluated.  With three colons before the
                     message they are modified to refer only to the normal
                     arguments covered by this description.

Also confirmed that this issue is zsh-specific. Bash and fish completions work fine.


  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same change?
  • Have you added an explanation of what your changes do and why you'd like us to include them? Performance claims (e.g. "this is faster") must include Hyperfine benchmarks.
  • Have you written new tests (excluding integration tests) for your changes? Here's an example.
  • Have you successfully run brew lgtm (style, typechecking and tests) with your changes locally?

  • AI was used to generate or assist with generating this PR.

Claude helped to locate and fix the problem.


Footnotes

  1. https://zsh.sourceforge.io/Doc/Release/Completion-System.html

In zsh completion context, the words array excludes the command name, so
subcommands are not matched and per-subcommand options are not
suggested.

To fix that, use `$words[1]` instead of `$words[2]` when dispatching
nested subcommands in generated zsh completions.

For reference, see zshcompsys's manpage [1]:

    $ man zshcompsys | col -bx | grep -F -A 13 '*::message:action'
                  *::message:action
                  *:::message:action
                         This describes how arguments (usually non-option
                         arguments, those not beginning with - or +) are to be
                         completed when neither of the first two forms was
                         provided.  Any number of arguments can be completed in
                         this fashion.

                         With two colons before the message, the words special
                         array and the CURRENT special parameter are modified to
                         refer only to the normal arguments when the action is
                         executed or evaluated.  With three colons before the
                         message they are modified to refer only to the normal
                         arguments covered by this description.

Also confirmed that this issue is zsh-specific. Bash and fish
completions work fine.

[1]: https://zsh.sourceforge.io/Doc/Release/Completion-System.html
Copilot AI review requested due to automatic review settings May 26, 2026 15:05
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes zsh completion dispatch for Homebrew commands with nested subcommands so per-subcommand options are suggested correctly.

Changes:

  • Updates generated zsh nested subcommand completion logic to inspect $words[1].
  • Regenerates affected zsh completion entries in completions/zsh/_brew.
  • Adds test coverage for the corrected zsh dispatch expression.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
Library/Homebrew/completions.rb Updates nested zsh subcommand dispatch to use the correct zsh words index.
Library/Homebrew/test/completions_spec.rb Adds an expectation covering the corrected generated zsh case statement.
completions/zsh/_brew Applies the regenerated zsh completion output for nested subcommand completions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Member

@MikeMcQuaid MikeMcQuaid left a comment

Choose a reason for hiding this comment

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

Thanks!

@MikeMcQuaid MikeMcQuaid enabled auto-merge May 26, 2026 15:34
@MikeMcQuaid MikeMcQuaid added this pull request to the merge queue May 27, 2026
Merged via the queue into main with commit 5d7251a May 27, 2026
79 of 83 checks passed
@MikeMcQuaid MikeMcQuaid deleted the fix-zsh-subcommand-option-completion branch May 27, 2026 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants