mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
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.
This commit is contained in:
@@ -502,6 +502,20 @@ namespace System.Management.Automation
|
||||
|
||||
Collection<string> stringResult = new Collection<string>();
|
||||
|
||||
// if the directory exists, just return it
|
||||
try
|
||||
{
|
||||
if (Utils.NativeDirectoryExists(userPath))
|
||||
{
|
||||
result.Add(new PathInfo(drive, provider, userPath, _sessionState));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// in cases of Access Denied or other errors, fallback to previous behavior and let provider handle it
|
||||
}
|
||||
|
||||
if (!context.SuppressWildcardExpansion)
|
||||
{
|
||||
// See if the provider will expand the wildcard
|
||||
|
||||
@@ -115,3 +115,26 @@ Describe "Configuration file locations" -tags "CI","Slow" {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Describe "Working directory on startup" -Tag "CI" {
|
||||
BeforeAll {
|
||||
$powershell = Join-Path -Path $PSHOME -ChildPath "pwsh"
|
||||
$testPath = New-Item -ItemType Directory -Path "$TestDrive\test[dir]"
|
||||
$currentDirectory = Get-Location
|
||||
}
|
||||
|
||||
AfterAll {
|
||||
Set-Location $currentDirectory
|
||||
}
|
||||
|
||||
It "Can start in directory where name contains wildcard characters" {
|
||||
Set-Location -LiteralPath $testPath.FullName
|
||||
if ($IsMacOS) {
|
||||
# on macOS, /tmp is a symlink to /private so the real path is under /private/tmp
|
||||
$expectedPath = "/private" + $testPath.FullName
|
||||
} else {
|
||||
$expectedPath = $testPath.FullName
|
||||
}
|
||||
& $powershell -noprofile -c { $PWD.Path } | Should BeExactly $expectedPath
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,16 @@ Describe "Set-Location" -Tags "CI" {
|
||||
$result | Should BeOfType System.Management.Automation.PathInfo
|
||||
}
|
||||
|
||||
It "Should accept path containing wildcard characters" {
|
||||
$null = New-Item -ItemType Directory -Path "$TestDrive\aa"
|
||||
$null = New-Item -ItemType Directory -Path "$TestDrive\ba"
|
||||
$testPath = New-Item -ItemType Directory -Path "$TestDrive\[ab]a"
|
||||
|
||||
Set-Location $TestDrive
|
||||
Set-Location -Path "[ab]a"
|
||||
$(Get-Location).Path | Should BeExactly $testPath.FullName
|
||||
}
|
||||
|
||||
Context 'Set-Location with no arguments' {
|
||||
|
||||
It 'Should go to $env:HOME when Set-Location run with no arguments from FileSystem provider' {
|
||||
|
||||
Reference in New Issue
Block a user