Skip to content

Fix Set-Location history with wildcard paths#27527

Open
KirtiRamchandani wants to merge 1 commit into
PowerShell:masterfrom
KirtiRamchandani:fix/location-history-wildcard-paths
Open

Fix Set-Location history with wildcard paths#27527
KirtiRamchandani wants to merge 1 commit into
PowerShell:masterfrom
KirtiRamchandani:fix/location-history-wildcard-paths

Conversation

@KirtiRamchandani
Copy link
Copy Markdown

PR Summary

Fixes #27507.

Set-Location - and Set-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-Location behavior for stacked locations.

PR Context

Pop-Location already escapes saved PathInfo entries before passing them back through SetLocation. 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 pass

Tests

  • Manual repro before fix: old binary returned PathNotFound for Set-Location -Path '-' when the previous path contained [abc].
  • Start-PSBuild -Configuration Debug -SMAOnly
  • Manual repro after fix: publish pwsh.exe returned 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-fatal New-Item message was printed after the passing summary).

Copilot AI review requested due to automatic review settings May 25, 2026 04:07
@KirtiRamchandani KirtiRamchandani requested a review from a team as a code owner May 25, 2026 04:07
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +858 to +863
private static string GetEscapedLocationHistoryPath(PathInfo location)
{
return LocationGlobber.GetMshQualifiedPath(
WildcardPattern.Escape(location.Path),
location.GetDrive());
}
Comment on lines +157 to +167

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Set-Location: does not escape on -Path - when resolved path contains wildcard character

2 participants