Repository navigation
Fix the behavior when multiple verbatim arguments --% are passed to a native command. - #26218
kzrnm (kzrnm) wants to merge 7 commits into
Conversation
|
@microsoft-github-policy-service agree |
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
| if (verbatimToken.Value != string.Empty) | ||
| { | ||
| ast = new StringConstantExpressionAst(verbatimToken.Extent, verbatimToken.Value, StringConstantType.BareWord); | ||
| elements.Add(ast); | ||
| endExtent = ast.Extent; | ||
| } |
There was a problem hiding this comment.
Is this change needed for the fix? We should revert this if not.
There was a problem hiding this comment.
This is required to fix #26217.
An empty string is passed as an argument when the last argument is --%.
echo python -c "import sys;print(sys.argv)" a --% > run.ps1
C:\>powershell.exe ./run.ps1
['-c', 'a']
C:\>pwsh.exe ./run.ps1
['-c', 'a', '']There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR refines handling of PowerShell’s verbatim argument marker (--%) so that it doesn’t accidentally produce an empty argument, and adds tests to validate the updated behavior.
Changes:
- Update the parser to avoid adding an empty argument element when
--%appears with no following argument text. - Update native command parameter binding so only the first
--%is treated as the verbatim marker. - Add Pester tests covering multiple
--%and quoting combinations for verbatim argument behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| test/powershell/Language/Scripting/NativeExecution/NativeCommandArguments.Tests.ps1 | Adds coverage for verbatim argument marker edge-cases and quoting scenarios. |
| src/System.Management.Automation/engine/parser/Parser.cs | Prevents creation of an empty AST argument when --% has no trailing argument. |
| src/System.Management.Automation/engine/NativeCommandParameterBinder.cs | Ensures only the first --% is treated as the marker and reuses a central constant. |
|
|
||
| // Add the element following the verbatim argument. | ||
| // If there is no argument after the verbatim argument, | ||
| // the element will be an empty string, so nothing should be added. | ||
| if (verbatimToken.Value != string.Empty) | ||
| { | ||
| ast = new StringConstantExpressionAst(verbatimToken.Extent, verbatimToken.Value, StringConstantType.BareWord); | ||
| elements.Add(ast); | ||
| endExtent = ast.Extent; |
| // We do need to remember it though - we'll expand environment variables in subsequent args. | ||
| object argValue = parameter.ArgumentValue; | ||
| if (string.Equals("--%", argValue as string, StringComparison.OrdinalIgnoreCase)) | ||
| if (!sawVerbatimArgumentMarker && string.Equals(Parser.VERBATIM_ARGUMENT, argValue as string, StringComparison.OrdinalIgnoreCase)) |
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
PR Summary
Fix #26198
Fix #26217
PR Context
Since
--%disables further parsing, any subsequent--%should be treated as a normal argument.However, as shown in the command above, when multiple
--%arguments are passed, all of them are ignored.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header--%) are ignored when passed to a native command. #26198 Arguments of a command ending with the verbatim argument--%include"". #26217