fix: handle empty and single-line output in PowerShell completion - #2497
Open
jabrailkhalil wants to merge 2 commits into
Open
jabrailkhalil wants to merge 2 commits into
jabrailkhalil wants to merge 2 commits into
Conversation
The generated PowerShell completion script failed with runtime errors when the completion command produced no output, when it produced a single directive-only line, and when a completion value collided with the directive line: - $Out is now normalized into an array before the directive is read, so an empty result ($Out being $null) and a single-line result (where $Out[-1] would return a System.Char instead of a string) are handled safely. - The directive line is stripped by index with Select-Object instead of filtering out every line equal to it, so completions that match the directive value are no longer dropped. - The script returns early when there are no completions left after removing the directive, instead of indexing into $Out. Fixes spf13#2481 Signed-off-by: jabrailkhalil <jabrailkhalil@gmail.com>
Check both generated template variants for scalar normalization, guarded empty-output indexing, the directive-only return, and index-based removal that preserves completions equal to the directive. Ignore comments and whitespace, and verify the operations remain ordered without requiring PowerShell on the test host. Signed-off-by: jabrailkhalil <jabrailkhalil@gmail.com>
Author
|
The CI workflows for the current head |
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.
Summary
Fixes #2481. The generated PowerShell completion script failed with runtime errors in three edge cases:
$Out[-1].TrimStart(':')was called on a null value (Cannot index into a null array).-OutVariableunwraps single-element results, so$Out[-1]returns aSystem.Charinstead of aSystem.StringandTrimStartis not available.$Out = $Out | Where-Object { $_ -ne $Out[-1] }removed every line equal to the directive value, dropping legitimate completions (e.g. a completion whose value is:0).The template in
powershell_completions.gonow:$Outinto an array ([Array]$Directives = $Out) before reading the directive, so$null, a single string, or a multi-line result are all handled safely;Select-Object -First, instead of filtering by value;$Out.Tests
go test ./...— passes (cobra, cobra/doc)go vet ./...— passesgolangci-lint run --new-from-rev=origin/main --new ./...— 0 issuesVerification
Exercised the generated script in Windows PowerShell 5.1 against real binaries speaking the completion protocol (empty output, directive-only output, colliding directive/completion values, and a normal CLI built on cobra):
PSArgumentNullExceptionon PS 5.1; new script completes with no errors:0completion; new script returns itValidArgsFunction): behavior is unchangedFixes #2481