Skip to content

Commit ba5bfaf

Browse files
Address review: filter prereleases when not requested and remove all loaded instances
Per PR review: resolution now excludes prerelease versions when Prerelease is false and adds a stable-wins tie-breaker (so a Prerelease:false run never imports an installed prerelease, and stable wins at equal base version); and 'Get-Module -Name \ -All | Remove-Module' removes every loaded instance before import.
1 parent 3117518 commit ba5bfaf

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

src/init.ps1

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,24 @@ process {
6868
}
6969

7070
# Resolve the exact installed version that satisfies the request (newest match), so the loaded
71-
# module is deterministic instead of whatever PowerShell would auto-load from PSModulePath.
71+
# module is deterministic instead of whatever PowerShell would auto-load from PSModulePath. When
72+
# prerelease versions are not requested, never resolve to one; when a stable and a prerelease share
73+
# a base version, the stable one wins.
7274
$resolveParams = @{
7375
Name = $Name
7476
ErrorAction = 'SilentlyContinue'
7577
}
7678
if ($Version) {
7779
$resolveParams['Version'] = $Version
7880
}
79-
$resolved = Get-InstalledPSResource @resolveParams | Sort-Object Version -Descending | Select-Object -First 1
81+
$candidates = @(Get-InstalledPSResource @resolveParams)
82+
if (-not $Prerelease) {
83+
$candidates = @($candidates | Where-Object { -not $_.IsPrerelease })
84+
}
85+
$resolved = $candidates | Sort-Object -Property @(
86+
@{ Expression = 'Version'; Descending = $true }
87+
@{ Expression = 'IsPrerelease'; Descending = $false }
88+
) | Select-Object -First 1
8089
if (-not $resolved) {
8190
throw "No installed '$Name' version satisfies the requested version '$Version'."
8291
}
@@ -86,10 +95,10 @@ process {
8695
Write-Output 'Already imported:'
8796
$alreadyImported | Format-List | Out-String
8897
}
89-
# Remove any already-loaded versions so only the chosen version remains loaded, then import that
90-
# exact version into the global session state so every subsequent command (info.ps1, the user
91-
# script, clean.ps1) uses the selected version.
92-
Remove-Module -Name $Name -Force -ErrorAction SilentlyContinue
98+
# Remove every already-loaded instance (all versions, including nested) so only the chosen version
99+
# remains loaded, then import that exact version into the global session state so every subsequent
100+
# command (info.ps1, the user script, clean.ps1) uses the selected version.
101+
Get-Module -Name $Name -All | Remove-Module -Force -ErrorAction SilentlyContinue
93102
Write-Verbose "Importing module: $Name $($resolved.Version)"
94103
Import-Module -Name $Name -RequiredVersion $resolved.Version -Force -Global
95104

0 commit comments

Comments
 (0)