Fix zsh nested subcommand option completion#22422
Merged
Merged
Conversation
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
Contributor
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
wordsspecial array and theCURRENTspecial parameter are modified to refer only to the normal arguments when the action is executed or evaluated":Also confirmed that this issue is zsh-specific. Bash and fish completions work fine.
brew lgtm(style, typechecking and tests) with your changes locally?Claude helped to locate and fix the problem.
Footnotes
https://zsh.sourceforge.io/Doc/Release/Completion-System.html ↩