Skip to content

Fix the behavior when multiple verbatim arguments --% are passed to a native command. - #26218

Open
kzrnm (kzrnm) wants to merge 7 commits into
PowerShell:masterfrom
kzrnm:sawVerbatimArgumentMarker
Open

kzrnm (kzrnm) wants to merge 7 commits into
PowerShell:masterfrom
kzrnm:sawVerbatimArgumentMarker

Conversation

@kzrnm

Copy link
Copy Markdown

PR Summary

Fix #26198
Fix #26217

PR Context

/usr/bin/echo --% --%

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

@kzrnm

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@microsoft-github-policy-service microsoft-github-policy-service Bot added the Review - Needed The PR is being reviewed label Oct 25, 2025
@microsoft-github-policy-service

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

Comment on lines +6632 to +6637
if (verbatimToken.Value != string.Empty)
{
ast = new StringConstantExpressionAst(verbatimToken.Extent, verbatimToken.Value, StringConstantType.BareWord);
elements.Add(ast);
endExtent = ast.Extent;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this change needed for the fix? We should revert this if not.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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', '']

@daxian-dbw Dongbo Wang (daxian-dbw) added CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log and removed Review - Needed The PR is being reviewed labels Apr 20, 2026
Copilot AI review requested due to automatic review settings May 24, 2026 03:35
@kzrnm
kzrnm (kzrnm) requested a review from a team as a code owner May 24, 2026 03:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment on lines +6632 to +6640

// 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))
@microsoft-github-policy-service microsoft-github-policy-service Bot added the Review - Needed The PR is being reviewed label Sep 25, 2026
@microsoft-github-policy-service

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Review - Needed The PR is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Arguments of a command ending with the verbatim argument --% include "". All verbatim arguments (--%) are ignored when passed to a native command.

3 participants