Skip to content

Commit 7459b54

Browse files
SteveL-MSFTiSazonov
authored andcommitted
Set-Location should use path with wildcard characters if it exists instead of globbing (#5839)
When InitialSessionState initializes it tries to SetLocation to current working directory, but if the directory name contains PowerShell wildcard characters, it fails and reverts to $PSHOME. The change affects Set-Location in that if the path exists (even if containing wildcard characters), just use it. It is a breaking change.
1 parent a07cc49 commit 7459b54

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/System.Management.Automation/namespaces/LocationGlobber.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,20 @@ private Collection<PathInfo> ResolveDriveQualifiedPath(
502502

503503
Collection<string> stringResult = new Collection<string>();
504504

505+
// if the directory exists, just return it
506+
try
507+
{
508+
if (Utils.NativeDirectoryExists(userPath))
509+
{
510+
result.Add(new PathInfo(drive, provider, userPath, _sessionState));
511+
return result;
512+
}
513+
}
514+
catch
515+
{
516+
// in cases of Access Denied or other errors, fallback to previous behavior and let provider handle it
517+
}
518+
505519
if (!context.SuppressWildcardExpansion)
506520
{
507521
// See if the provider will expand the wildcard

test/powershell/Host/Base-Directory.Tests.ps1

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,26 @@ Describe "Configuration file locations" -tags "CI","Slow" {
115115
}
116116
}
117117
}
118+
119+
Describe "Working directory on startup" -Tag "CI" {
120+
BeforeAll {
121+
$powershell = Join-Path -Path $PSHOME -ChildPath "pwsh"
122+
$testPath = New-Item -ItemType Directory -Path "$TestDrive\test[dir]"
123+
$currentDirectory = Get-Location
124+
}
125+
126+
AfterAll {
127+
Set-Location $currentDirectory
128+
}
129+
130+
It "Can start in directory where name contains wildcard characters" {
131+
Set-Location -LiteralPath $testPath.FullName
132+
if ($IsMacOS) {
133+
# on macOS, /tmp is a symlink to /private so the real path is under /private/tmp
134+
$expectedPath = "/private" + $testPath.FullName
135+
} else {
136+
$expectedPath = $testPath.FullName
137+
}
138+
& $powershell -noprofile -c { $PWD.Path } | Should BeExactly $expectedPath
139+
}
140+
}

test/powershell/Modules/Microsoft.PowerShell.Management/Set-Location.Tests.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ Describe "Set-Location" -Tags "CI" {
4141
$result | Should BeOfType System.Management.Automation.PathInfo
4242
}
4343

44+
It "Should accept path containing wildcard characters" {
45+
$null = New-Item -ItemType Directory -Path "$TestDrive\aa"
46+
$null = New-Item -ItemType Directory -Path "$TestDrive\ba"
47+
$testPath = New-Item -ItemType Directory -Path "$TestDrive\[ab]a"
48+
49+
Set-Location $TestDrive
50+
Set-Location -Path "[ab]a"
51+
$(Get-Location).Path | Should BeExactly $testPath.FullName
52+
}
53+
4454
Context 'Set-Location with no arguments' {
4555

4656
It 'Should go to $env:HOME when Set-Location run with no arguments from FileSystem provider' {

0 commit comments

Comments
 (0)