diff --git a/src/System.Management.Automation/engine/SessionStateLocationAPIs.cs b/src/System.Management.Automation/engine/SessionStateLocationAPIs.cs index 56de07b8871..89b8e13af01 100644 --- a/src/System.Management.Automation/engine/SessionStateLocationAPIs.cs +++ b/src/System.Management.Automation/engine/SessionStateLocationAPIs.cs @@ -253,7 +253,7 @@ internal PathInfo SetLocation(string path, CmdletProviderContext context, bool l throw new InvalidOperationException(SessionStateStrings.LocationUndoStackIsEmpty); } - path = _setLocationHistory.Undo(this.CurrentLocation).Path; + path = GetEscapedLocationHistoryPath(_setLocationHistory.Undo(this.CurrentLocation)); break; case string originalPathSwitch when !literalPath && originalPathSwitch.Equals("+", StringComparison.Ordinal): if (_setLocationHistory.RedoCount <= 0) @@ -261,7 +261,7 @@ internal PathInfo SetLocation(string path, CmdletProviderContext context, bool l throw new InvalidOperationException(SessionStateStrings.LocationRedoStackIsEmpty); } - path = _setLocationHistory.Redo(this.CurrentLocation).Path; + path = GetEscapedLocationHistoryPath(_setLocationHistory.Redo(this.CurrentLocation)); break; default: var pushPathInfo = GetNewPushPathInfo(); @@ -855,6 +855,13 @@ private PathInfo GetNewPushPathInfo() return newPushLocation; } + private static string GetEscapedLocationHistoryPath(PathInfo location) + { + return LocationGlobber.GetMshQualifiedPath( + WildcardPattern.Escape(location.Path), + location.GetDrive()); + } + /// /// Resets the current working drive and directory to the first /// entry on the working directory stack and removes that entry diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Set-Location.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Set-Location.Tests.ps1 index 73f3250ae89..34fcb960956 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Set-Location.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Set-Location.Tests.ps1 @@ -149,6 +149,24 @@ Describe "Set-Location" -Tags "CI" { (Get-Location).Path | Should -Be $initialLocation } + It 'Should go through locations with wildcard characters when specifying minus and plus as paths' { + $testPath = Join-Path $TestDrive 'foo - bar(123-456)[foo]' + $otherPath = Join-Path $TestDrive 'other' + $null = New-Item -ItemType Directory -Path $testPath + $null = New-Item -ItemType Directory -Path $otherPath + + Set-Location $TestDrive + $testDrivePath = (Get-Location).Path + Set-Location -LiteralPath $testPath + Set-Location -LiteralPath $otherPath + Set-Location - + (Get-Location).Path | Should -BeExactly $testPath + Set-Location - + (Get-Location).Path | Should -BeExactly $testDrivePath + Set-Location + + (Get-Location).Path | Should -BeExactly $testPath + } + It 'Should go back to previous locations when specifying minus twice' { $initialLocation = (Get-Location).Path Set-Location ([System.IO.Path]::GetTempPath())