Skip to content

Commit 0f21156

Browse files
authored
Fix $PSNativeCommandArgPassing = Windows to handle empty args correctly (PowerShell#16639)
1 parent e2f39fa commit 0f21156

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

src/System.Management.Automation/engine/NativeCommandParameterBinder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ private void AppendOneNativeArgument(ExecutionContext context, CommandParameterI
314314
}
315315
else
316316
{
317-
if (argArrayAst != null && ArgumentPassingStyle == NativeArgumentPassingStyle.Standard)
317+
if (argArrayAst != null && ArgumentPassingStyle != NativeArgumentPassingStyle.Legacy)
318318
{
319319
// We have a literal array, so take the extent, break it on spaces and add them to the argument list.
320320
foreach (string element in argArrayAst.Extent.Text.Split(' ', StringSplitOptions.RemoveEmptyEntries))
@@ -331,7 +331,7 @@ private void AppendOneNativeArgument(ExecutionContext context, CommandParameterI
331331
}
332332
}
333333
}
334-
else if (ArgumentPassingStyle == NativeArgumentPassingStyle.Standard && currentObj != null)
334+
else if (ArgumentPassingStyle != NativeArgumentPassingStyle.Legacy && currentObj != null)
335335
{
336336
// add empty strings to arglist, but not nulls
337337
AddToArgumentList(parameter, arg);

test/powershell/Language/Scripting/NativeExecution/NativeCommandArguments.Tests.ps1

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ Describe "Will error correctly if an attempt to set variable to improper value"
171171
}
172172
}
173173

174-
foreach ( $argumentListValue in "Standard","Legacy" ) {
174+
foreach ( $argumentListValue in "Standard","Legacy","Windows" ) {
175175
$PSNativeCommandArgumentPassing = $argumentListValue
176176
Describe "Native Command Arguments (${PSNativeCommandArgumentPassing})" -tags "CI" {
177177
# When passing arguments to native commands, quoted segments that contain
@@ -263,6 +263,28 @@ foreach ( $argumentListValue in "Standard","Legacy" ) {
263263
$lines[$i] | Should -BeExactly "Arg $i is <$($expected[$i])>"
264264
}
265265
}
266+
267+
It "Should handle empty args correctly (ArgumentList=${PSNativeCommandArgumentPassing})" {
268+
if ($PSNativeCommandArgumentPassing -eq 'Legacy') {
269+
$expectedLines = 2
270+
}
271+
else {
272+
$expectedLines = 3
273+
}
274+
275+
$lines = testexe -echoargs 1 '' 2
276+
$lines.Count | Should -Be $expectedLines
277+
$lines[0] | Should -BeExactly 'Arg 0 is <1>'
278+
279+
if ($expectedLines -eq 2) {
280+
$lines[1] | Should -BeExactly 'Arg 1 is <2>'
281+
}
282+
else {
283+
$lines[1] | Should -BeExactly 'Arg 1 is <>'
284+
$lines[2] | Should -BeExactly 'Arg 2 is <2>'
285+
}
286+
287+
}
266288
}
267289
}
268290
Describe 'PSPath to native commands' -tags "CI" {

0 commit comments

Comments
 (0)