From 3c07aad4bc062f4fc192853461d5bc3e304795b5 Mon Sep 17 00:00:00 2001 From: "Mathias R. Jessen" Date: Thu, 14 May 2020 19:24:28 +0200 Subject: [PATCH] Fix path handling bug in `PSTask` (#12554) --- .../engine/hostifaces/PSTask.cs | 7 ++++++- .../Foreach-Object-Parallel.Tests.ps1 | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/hostifaces/PSTask.cs b/src/System.Management.Automation/engine/hostifaces/PSTask.cs index 0c63c2a0b7..fe42bd55d2 100644 --- a/src/System.Management.Automation/engine/hostifaces/PSTask.cs +++ b/src/System.Management.Automation/engine/hostifaces/PSTask.cs @@ -447,7 +447,12 @@ namespace System.Management.Automation.PSTasks try { Runspace.DefaultRunspace = runspace; - runspace.ExecutionContext.SessionState.Internal.SetLocation(_currentLocationPath); + var context = new CmdletProviderContext(runspace.ExecutionContext) + { + // _currentLocationPath denotes the current path as-is, and should not be attempted expanded. + SuppressWildcardExpansion = true + }; + runspace.ExecutionContext.SessionState.Internal.SetLocation(_currentLocationPath, context); } catch (DriveNotFoundException) { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Foreach-Object-Parallel.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Foreach-Object-Parallel.Tests.ps1 index eadf67ae4c..484454e3a8 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Foreach-Object-Parallel.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Foreach-Object-Parallel.Tests.ps1 @@ -105,6 +105,27 @@ Describe 'ForEach-Object -Parallel Basic Tests' -Tags 'CI' { $parallelScriptLocation.Path | Should -BeExactly $PWD.Path } + It 'Verifies that the current working directory can have wildcards in its name' { + $oldLocation = Get-Location + + $wildcardName = New-Item -Path 'TestDrive:\' -Name '[' -ItemType Directory + Set-Location -LiteralPath $wildcardName.FullName + try + { + { 1..1 | ForEach-Object -Parallel { $PWD } } | Should -Not -Throw + + $wildcardPathResult = 1..1 | ForEach-Object -Parallel { $PWD } + $wildcardPathResult.Path | Should -BeExactly $PWD.Path + } + finally + { + Set-Location -Path $oldLocation + if ($drive -is [System.IO.DirectoryInfo]) { + $drive | Remove-Item -Force + } + } + } + It 'Verifies no terminating error if current working drive is not found' { $oldLocation = Get-Location try