Fix detection of $PSHOME in front of $env:PATH (#11141)

This commit is contained in:
Steve Lee 2019-11-23 15:17:08 -08:00 committed by Travis Plunk
parent efa804f498
commit a1c2d3c977
2 changed files with 32 additions and 6 deletions

View File

@ -113,13 +113,17 @@ namespace Microsoft.PowerShell
// 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

View File

@ -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)
}
}