From 7459b5463904b7849320b7e834fda4e3274acd05 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 16 Jan 2018 20:58:55 -0800 Subject: [PATCH] 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. --- .../namespaces/LocationGlobber.cs | 14 +++++++++++ test/powershell/Host/Base-Directory.Tests.ps1 | 23 +++++++++++++++++++ .../Set-Location.Tests.ps1 | 10 ++++++++ 3 files changed, 47 insertions(+) diff --git a/src/System.Management.Automation/namespaces/LocationGlobber.cs b/src/System.Management.Automation/namespaces/LocationGlobber.cs index 9998476f75..5afeb0cd22 100644 --- a/src/System.Management.Automation/namespaces/LocationGlobber.cs +++ b/src/System.Management.Automation/namespaces/LocationGlobber.cs @@ -502,6 +502,20 @@ namespace System.Management.Automation Collection stringResult = new Collection(); + // 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 diff --git a/test/powershell/Host/Base-Directory.Tests.ps1 b/test/powershell/Host/Base-Directory.Tests.ps1 index c518cff84d..977da42dfe 100644 --- a/test/powershell/Host/Base-Directory.Tests.ps1 +++ b/test/powershell/Host/Base-Directory.Tests.ps1 @@ -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 + } +} 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 a9b6543616..be9e0965c0 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Set-Location.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Set-Location.Tests.ps1 @@ -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' {