From fe69f284eecece99653ff94fd8d2387d97c9d6e6 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Wed, 20 Nov 2019 16:59:16 -0800 Subject: [PATCH 1/2] Fix detection of $PSHOME in front of $env:PATH --- .../host/msh/ConsoleHost.cs | 16 +++++++++----- test/powershell/Host/ConsoleHost.Tests.ps1 | 22 +++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index 1089985ec0f..e6e8759d3cd 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -113,13 +113,17 @@ internal static int Start(string bannerText, string helpText, string[] args) // put PSHOME in front of PATH so that calling `powershell` within `powershell` always starts the same running version string path = Environment.GetEnvironmentVariable("PATH"); - if (path != null) + string pshome = Utils.DefaultPowerShellAppBase + Path.PathSeparator; + + // to not impact startup perf, we don't remove duplicates, but we avoid adding a duplicate to the front + // we also don't handle the edge case where PATH only contains $PSHOME + if (string.IsNullOrEmpty(path)) { - string pshome = Utils.DefaultPowerShellAppBase; - if (!path.Contains(pshome)) - { - Environment.SetEnvironmentVariable("PATH", pshome + Path.PathSeparator + path); - } + Environment.SetEnvironmentVariable("PATH", pshome); + } + else if (!path.StartsWith(pshome)) + { + Environment.SetEnvironmentVariable("PATH", pshome + path); } try diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1 index e18389fd9be..a1dd0001ca1 100644 --- a/test/powershell/Host/ConsoleHost.Tests.ps1 +++ b/test/powershell/Host/ConsoleHost.Tests.ps1 @@ -987,3 +987,25 @@ Describe 'Pwsh startup in directories that contain wild cards' -Tag CI { } } } + +Describe 'Pwsh startup and PATH' -Tag CI { + BeforeEach { + $oldPath = $env:PATH + } + + AfterEach { + $env:PATH = $oldPath + } + + It 'Calling pwsh starts the same version of PowerShell as currently running' { + $version = pwsh -v + $version | Should -BeExactly "PowerShell $($PSVersionTable.GitCommitId)" + } + + It 'pwsh starts even if PATH is not defined' { + $pwsh = Join-Path -Path $PSHOME -ChildPath "pwsh" + Remove-Item Env:\Path + $path = & $pwsh -noprofile -command '$env:PATH' + $path | Should -BeExactly ($PSHOME + [System.IO.Path]::PathSeparator) + } +} From e1f5be2dd171dddafaffd5b54307b8108cda4f55 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Wed, 20 Nov 2019 20:16:42 -0800 Subject: [PATCH 2/2] fix test --- test/powershell/Host/ConsoleHost.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1 index a1dd0001ca1..d09f3bb02e1 100644 --- a/test/powershell/Host/ConsoleHost.Tests.ps1 +++ b/test/powershell/Host/ConsoleHost.Tests.ps1 @@ -1004,7 +1004,7 @@ Describe 'Pwsh startup and PATH' -Tag CI { It 'pwsh starts even if PATH is not defined' { $pwsh = Join-Path -Path $PSHOME -ChildPath "pwsh" - Remove-Item Env:\Path + Remove-Item Env:\PATH $path = & $pwsh -noprofile -command '$env:PATH' $path | Should -BeExactly ($PSHOME + [System.IO.Path]::PathSeparator) }