mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Update PowerShell to handle the case where the Windows PowerShell module path is already in the environment's PSModulePath (#7727)
This change updates ModuleIntrinsics.GetModulePath to handle the case where the Windows PowerShell module path is already in the environment's PSModulePath or when launched from a different version of PowerShell. Previously, GetModulePath would append $PSHOME\Modules to the PSModulePath after removing the path for the launching version without considering the Windows PowerShell module path. The result, was the Windows PowerShell modules were found first and loaded incompatible modules; such as the built-in modules. The change detects the Windows PowerShell module path and inserts $PSHOME\Modules path before it. The new test simulates launching from a different version of pwsh that has already added the Windows PowerShell module path. Fixes #7679
This commit is contained in:
committed by
Travis Plunk
parent
b8fb55340d
commit
4683934793
@@ -130,4 +130,33 @@ Describe "SxS Module Path Basic Tests" -tags "CI" {
|
||||
$paths -contains $customeModules | Should -BeTrue
|
||||
}
|
||||
|
||||
It 'Ensures $PSHOME\Modules is inserted correctly when launched from a different version of PowerShell' -Skip:(!($IsCoreCLR -and $IsWindows)) {
|
||||
# When launched from a different version of PowerShell, PSModulePath contains the other version's PSHOME\Modules path
|
||||
# and the Windows PowerShell modoule path. THe other version's module path should be removed and this version's
|
||||
# PSHOME\Modules path should be inserted before Windows PowerShell module path.
|
||||
$winpwshModulePath = [System.IO.Path]::Combine([System.Environment]::SystemDirectory, "WindowsPowerShell", "v1.0", "Modules");
|
||||
$pwshModulePath = Join-Path -Path $PSHOME -ChildPath 'Modules'
|
||||
|
||||
# create a fake 'other version' $PSHOME and $PSHOME\Modules
|
||||
$fakeHome = Join-Path -Path $TestDrive -ChildPath 'fakepwsh'
|
||||
$fakeModulePath = Join-Path -Path $fakeHome -ChildPath 'Modules'
|
||||
|
||||
$null = New-Item -Path $fakeHome -ItemType Directory
|
||||
$null = New-Item -Path $fakeModulePath -ItemType Directory
|
||||
|
||||
# powershell looks for these to files to determine the directory is a pwsh directory.
|
||||
Set-Content -Path "$fakeHome\pwsh.exe" -Value "fake pwsh.exe"
|
||||
Set-Content -Path "$fakeHome\pwsh.deps.json" -Value 'fake pwsh.deps.json'
|
||||
|
||||
# replace the actual pwsh module path with the fake one.
|
||||
$fakeModulePath = $env:PSModulePath.Replace($pwshModulePath, $fakeModulePath, [StringComparison]::OrdinalIgnoreCase)
|
||||
|
||||
$newModulePath = & $powershell -nopro -c '$env:PSModulePath'
|
||||
$pwshIndex = $newModulePath.IndexOf($pwshModulePath, [StringComparison]::OrdinalIgnoreCase)
|
||||
$wpshIndex = $newModulePath.IndexOf($winpwshModulePath, [StringComparison]::OrdinalIgnoreCase)
|
||||
# ensure both module paths exist and the pwsh module path occurs before the Windows PowerShell module path
|
||||
$pwshIndex | Should -Not -Be -1
|
||||
$wpshIndex | Should -Not -Be -1
|
||||
$pwshIndex | Should -BeLessThan $wpshIndex
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user