Fix positional parameter re-binding when parameter appears in multiple sets#27089
Open
powercode wants to merge 2 commits intoPowerShell:masterfrom
Open
Fix positional parameter re-binding when parameter appears in multiple sets#27089powercode wants to merge 2 commits intoPowerShell:masterfrom
powercode wants to merge 2 commits intoPowerShell:masterfrom
Conversation
…e sets A parameter declared with different Position values across multiple parameter sets (e.g. Position=0 in set 'Two' and Position=1 in set 'One') was placed in the sorted positional-binding dictionary at every position it appeared in. BindPositionalParametersInSet() would then bind the same parameter a second time when the loop reached the later position, overwriting the correct value with the wrong argument. Fix: add a BoundParameters.ContainsKey guard at the top of the outer foreach loop in BindPositionalParametersInSet() so that a parameter already bound at an earlier position is skipped. Apply the equivalent guard in PseudoParameterBinder.BindPseudoPositionalParameterInSet() so that tab-completion is consistent with runtime binding. Fixes PowerShell#2212
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a long-standing positional-parameter binding bug where a single parameter declared with different Position values across parameter sets could be bound more than once, causing earlier correct bindings to be overwritten by later positional arguments. The change also aligns tab-completion’s pseudo-binding behavior with runtime binding.
Changes:
- Add a guard in
ParameterBinderController.BindPositionalParametersInSet()to skip positional candidates that are already bound. - Add the equivalent guard in
PseudoParameterBinder.BindPseudoPositionalParameterInSet()to keep completion consistent with runtime binding. - Add new Pester coverage for the runtime binding regression scenarios described in issue #2212.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| test/powershell/Language/Scripting/ParameterBinding.Tests.ps1 | Adds regression tests validating correct positional binding across parameter sets for issue #2212. |
| src/System.Management.Automation/engine/ParameterBinderController.cs | Prevents rebinding a parameter that was already positionally bound at an earlier position. |
| src/System.Management.Automation/engine/CommandCompletion/PseudoParameterBinder.cs | Mirrors the runtime binding guard in the completion pseudo-binder to avoid completion-time rebinding. |
src/System.Management.Automation/engine/CommandCompletion/PseudoParameterBinder.cs
Show resolved
Hide resolved
Collaborator
Author
|
@daxian-dbw @SeeminglyScience Can you have a look? |
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.
PR Summary
Fixes a bug where a parameter declared with different
Positionvalues across multiple parameter sets would be bound twice during positional argument processing, overwriting the correct value.#2212
Root Cause
EvaluateUnboundPositionalParameters()correctly inserts a parameter into the sorted positional dictionary once per parameter set it belongs to — meaning a parameter that isPosition=0in setTwoandPosition=1in setOneappears at both dictionary keys. WhenBindPositionalParametersInSet()iterates these keys in sorted order, it would bind the parameter at position 0 (correctly), then encounter the same parameter again at position 1 and re-bind it — overwriting$Firstwith the argument intended for$Second, and then also binding$Secondto that same argument.Fix
Added a
BoundParameters.ContainsKeyguard at the top of the outerforeachloop inBindPositionalParametersInSet()(ParameterBinderController.cs) so that a parameter already bound at an earlier position is skipped. The equivalent guard was added toBindPseudoPositionalParameterInSet()inPseudoParameterBinder.csso that tab-completion behavior stays consistent with runtime binding.Files Changed
engine/ParameterBinderController.csBindPositionalParametersInSet()engine/CommandCompletion/PseudoParameterBinder.csBindPseudoPositionalParameterInSet()for consistencyParameterBinding.Tests.ps1Testing
All 5 new
CI-tagged tests pass (Tests Passed: 44, Failed: 0) when run against the fixed build on Windows x64.Reproduction from the original issue:
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header