Fix Set-Location history with wildcard paths#27527
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR fixes Set-Location - / Set-Location + history navigation when prior locations contain wildcard characters by escaping history paths, and adds regression coverage for paths containing [ / ].
Changes:
- Escape location history paths during undo/redo (
-/+) so wildcard characters aren’t treated as glob patterns. - Add a Pester test that navigates back/forward through history when a prior path includes wildcard characters.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| test/powershell/Modules/Microsoft.PowerShell.Management/Set-Location.Tests.ps1 | Adds regression test covering history navigation with wildcard characters in directory names. |
| src/System.Management.Automation/engine/SessionStateLocationAPIs.cs | Escapes undo/redo history paths (and re-qualifies them) before passing them back into SetLocation. |
| private static string GetEscapedLocationHistoryPath(PathInfo location) | ||
| { | ||
| return LocationGlobber.GetMshQualifiedPath( | ||
| WildcardPattern.Escape(location.Path), | ||
| location.GetDrive()); | ||
| } |
There was a problem hiding this comment.
Addressed in 7d89ddf: renamed the helper to GetMshQualifiedEscapedLocationHistoryPath so the name reflects both wildcard escaping and MSH path qualification. Tested with git diff --check, Start-PSBuild -Configuration Debug -Runtime win7-x64, and Start-PSPester -Path test\powershell\Modules\Microsoft.PowerShell.Management\Set-Location.Tests.ps1 (22 passed, 2 skipped, 1 pending).
|
|
||
| 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 |
There was a problem hiding this comment.
Addressed in 7d89ddf: the wildcard location-history test now resolves the expected paths with Resolve-Path -LiteralPath and uses non-exact path comparison, so it no longer depends on raw joined string casing or formatting. Tested with git diff --check, Start-PSBuild -Configuration Debug -Runtime win7-x64, and Start-PSPester -Path test\powershell\Modules\Microsoft.PowerShell.Management\Set-Location.Tests.ps1 (22 passed, 2 skipped, 1 pending).
PR Summary
Fixes #27507.
Set-Location -andSet-Location +replay paths saved in the location history. When the saved path contains wildcard characters such as[foo], the replayed path was passed through normal wildcard expansion and failed to resolve the literal directory.This change escapes the saved history path before resolving it, matching the existing
Pop-Locationbehavior for stacked locations.PR Context
Pop-Locationalready escapes savedPathInfoentries before passing them back throughSetLocation. The location-history undo/redo path used the raw saved path instead, so literal filesystem names containing wildcard characters were treated as patterns.PR Checklist
Tests
PathNotFoundforSet-Location -Path '-'when the previous path contained[abc].Start-PSBuild -Configuration Debug -SMAOnlypwsh.exereturned the wildcard-containing path successfully.Start-PSPester -Path .\test\powershell\Modules\Microsoft.PowerShell.Management\Set-Location.Tests.ps1 -Terse -ThrowOnFailure(22 passed, 0 failed, 2 skipped, 1 pending; an existing non-fatalNew-Itemmessage was printed after the passing summary).