Skip to content

Commit e76e997

Browse files
Address review: use Prerelease string, stop on import failure, clearer error
Filter/sort on the 'Prerelease' string instead of IsPrerelease; add -ErrorAction Stop to the final Import-Module so a failed load stops the run; and render 'latest' in the throw message when no Version was requested (matching the moduleStatus display).
1 parent ba5bfaf commit e76e997

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

src/init.ps1

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ process {
7070
# Resolve the exact installed version that satisfies the request (newest match), so the loaded
7171
# module is deterministic instead of whatever PowerShell would auto-load from PSModulePath. When
7272
# prerelease versions are not requested, never resolve to one; when a stable and a prerelease share
73-
# a base version, the stable one wins.
73+
# a base version, the stable one wins. PSResourceGet exposes prerelease state as the 'Prerelease'
74+
# string (empty for stable).
7475
$resolveParams = @{
7576
Name = $Name
7677
ErrorAction = 'SilentlyContinue'
@@ -80,14 +81,15 @@ process {
8081
}
8182
$candidates = @(Get-InstalledPSResource @resolveParams)
8283
if (-not $Prerelease) {
83-
$candidates = @($candidates | Where-Object { -not $_.IsPrerelease })
84+
$candidates = @($candidates | Where-Object { [string]::IsNullOrWhiteSpace($_.Prerelease) })
8485
}
8586
$resolved = $candidates | Sort-Object -Property @(
8687
@{ Expression = 'Version'; Descending = $true }
87-
@{ Expression = 'IsPrerelease'; Descending = $false }
88+
@{ Expression = { -not [string]::IsNullOrWhiteSpace($_.Prerelease) }; Descending = $false }
8889
) | Select-Object -First 1
8990
if (-not $resolved) {
90-
throw "No installed '$Name' version satisfies the requested version '$Version'."
91+
$requested = [string]::IsNullOrWhiteSpace($Version) ? 'latest' : $Version
92+
throw "No installed '$Name' version satisfies the requested version '$requested'."
9193
}
9294

9395
$alreadyImported = Get-Module -Name $Name
@@ -100,7 +102,7 @@ process {
100102
# command (info.ps1, the user script, clean.ps1) uses the selected version.
101103
Get-Module -Name $Name -All | Remove-Module -Force -ErrorAction SilentlyContinue
102104
Write-Verbose "Importing module: $Name $($resolved.Version)"
103-
Import-Module -Name $Name -RequiredVersion $resolved.Version -Force -Global
105+
Import-Module -Name $Name -RequiredVersion $resolved.Version -Force -Global -ErrorAction Stop
104106

105107
$providedToken = -not [string]::IsNullOrEmpty($env:PSMODULE_GITHUB_SCRIPT_INPUT_Token)
106108
$providedClientID = -not [string]::IsNullOrEmpty($env:PSMODULE_GITHUB_SCRIPT_INPUT_ClientID)

0 commit comments

Comments
 (0)