From 3117518b6d49ddd79fdeb70eb95568c0f05503f0 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sat, 11 Jul 2026 19:23:43 +0200 Subject: [PATCH] Import the selected GitHub module version and remove other loaded versions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/init.ps1 | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/init.ps1 b/src/init.ps1 index a937815..f587ae0 100644 --- a/src/init.ps1 +++ b/src/init.ps1 @@ -67,15 +67,31 @@ process { } } + # Resolve the exact installed version that satisfies the request (newest match), so the loaded + # module is deterministic instead of whatever PowerShell would auto-load from PSModulePath. + $resolveParams = @{ + Name = $Name + ErrorAction = 'SilentlyContinue' + } + if ($Version) { + $resolveParams['Version'] = $Version + } + $resolved = Get-InstalledPSResource @resolveParams | Sort-Object Version -Descending | Select-Object -First 1 + if (-not $resolved) { + throw "No installed '$Name' version satisfies the requested version '$Version'." + } + $alreadyImported = Get-Module -Name $Name if ($showInit) { Write-Output 'Already imported:' $alreadyImported | Format-List | Out-String } - if (-not $alreadyImported) { - Write-Verbose "Importing module: $Name" - Import-Module -Name $Name - } + # Remove any already-loaded versions so only the chosen version remains loaded, then import that + # exact version into the global session state so every subsequent command (info.ps1, the user + # script, clean.ps1) uses the selected version. + Remove-Module -Name $Name -Force -ErrorAction SilentlyContinue + Write-Verbose "Importing module: $Name $($resolved.Version)" + Import-Module -Name $Name -RequiredVersion $resolved.Version -Force -Global $providedToken = -not [string]::IsNullOrEmpty($env:PSMODULE_GITHUB_SCRIPT_INPUT_Token) $providedClientID = -not [string]::IsNullOrEmpty($env:PSMODULE_GITHUB_SCRIPT_INPUT_ClientID)