Skip to content

Commit 3117518

Browse files
Import the selected GitHub module version and remove other loaded versions
init.ps1 now resolves the exact installed version satisfying the Version input, removes any already-loaded GitHub module versions from the session, and imports the resolved version with -RequiredVersion -Global — so the user script uses the selected version instead of whatever PowerShell would auto-load from PSModulePath.
1 parent 8083ec1 commit 3117518

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

src/init.ps1

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,31 @@ process {
6767
}
6868
}
6969

70+
# 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.
72+
$resolveParams = @{
73+
Name = $Name
74+
ErrorAction = 'SilentlyContinue'
75+
}
76+
if ($Version) {
77+
$resolveParams['Version'] = $Version
78+
}
79+
$resolved = Get-InstalledPSResource @resolveParams | Sort-Object Version -Descending | Select-Object -First 1
80+
if (-not $resolved) {
81+
throw "No installed '$Name' version satisfies the requested version '$Version'."
82+
}
83+
7084
$alreadyImported = Get-Module -Name $Name
7185
if ($showInit) {
7286
Write-Output 'Already imported:'
7387
$alreadyImported | Format-List | Out-String
7488
}
75-
if (-not $alreadyImported) {
76-
Write-Verbose "Importing module: $Name"
77-
Import-Module -Name $Name
78-
}
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
93+
Write-Verbose "Importing module: $Name $($resolved.Version)"
94+
Import-Module -Name $Name -RequiredVersion $resolved.Version -Force -Global
7995

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

0 commit comments

Comments
 (0)