mirror of
https://github.com/PowerShell/PowerShell.git
synced 2024-12-04 07:13:53 +08:00
Fix detection of $PSHOME in front of $env:PATH (#11141)
This commit is contained in:
parent
efa804f498
commit
a1c2d3c977
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user