Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ internal void BindParameters(Collection<CommandParameterInternal> parameters)
// If this is the verbatim argument marker, we don't pass it on to the native command.
// 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))
{
sawVerbatimArgumentMarker = true;
continue;
Expand Down
13 changes: 10 additions & 3 deletions src/System.Management.Automation/engine/parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6629,9 +6629,16 @@ internal Ast CommandRule(bool forDynamicKeyword)
{
foundVerbatimArgument = true;
scanning = false;
ast = new StringConstantExpressionAst(verbatimToken.Extent, verbatimToken.Value, StringConstantType.BareWord);
elements.Add(ast);
endExtent = ast.Extent;

// 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;
Comment on lines +6632 to +6640
}
Comment on lines +6636 to +6641

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

}

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ Describe "find.exe uses legacy behavior on Windows" -Tag 'CI' {
}
}


foreach ( $argumentListValue in "Standard","Legacy","Windows" ) {
$PSNativeCommandArgumentPassing = $argumentListValue
Describe "Native Command Arguments (${PSNativeCommandArgumentPassing})" -tags "CI" {
Expand Down Expand Up @@ -310,4 +311,35 @@ foreach ( $argumentListValue in "Standard","Legacy","Windows" ) {
$lines | Should -BeExactly 'Arg 0 is <temp:/foo>'
}
}

Describe 'Verbatim arguments tests' -tags "CI" {
BeforeAll{
$var = 'foo'
}
It 'Should handle arguments <echoargs> correctly' -testCases @(
@{echoargs = "--%"; Expected = @()}
@{echoargs = "--% "; Expected = @()}
@{echoargs = "--% --%"; Expected = @('--%')}
@{echoargs = "--% --% "; Expected = @('--%')}
@{echoargs = "--% --% --%"; Expected = @('--%', '--%')}
@{echoargs = "--% --% --% "; Expected = @('--%', '--%')}
@{echoargs = "`"--%`" --%"; Expected = @('--%')}
@{echoargs = "'--%' --%"; Expected = @('--%')}
@{echoargs = "a --%"; Expected = @('a')}
@{echoargs = "a --% --%"; Expected = @('a', '--%')}
@{echoargs = "a --% b"; Expected = @('a', 'b')}
@{echoargs = "a --% b --%"; Expected = @('a', 'b', '--%')}

@{
echoargs = "a `$var `"var=`$var`" 'var' (1) --% a `$var `"var=`$var`" 'var' (1) * ~";
Expected = @('a', 'foo', 'var=foo', 'var', '1',
'a', '$var',
($PSNativeCommandArgumentPassing -eq 'Legacy' ? 'var=$var' : '"var=$var"'),
"'var'", '(1)', '*', '~')
}
) {
param($echoargs, $Expected)
@(Invoke-Expression "testexe -echoargs $echoargs") | Should -BeExactly @($Expected | ForEach-Object { $i = 0 } { "Arg {0} is <$_>" -f $i++ })
}
}
}