From 6369933067a4f3ee5168dad977b68c6a0a748797 Mon Sep 17 00:00:00 2001 From: HeyItsGilbert Date: Tue, 3 Nov 2026 17:38:23 +0000 Subject: [PATCH 1/2] fix(ci): resolve Pester assembly collision in build bootstrap PR #112's CI failed on every hosted runner with: Add-Type: Could not load file or assembly 'Pester, Version=6.1.0.0, ...'. Assembly with same name is already loaded Root cause: PowerShellBuild (0.7.2, installed via requirements.psd1) declares `Pester >= 5.6.1` as its own RequiredModules dependency, with no upper bound. Install-Module always auto-resolves a target's RequiredModules independently of whatever this repo's own `Pester` entry pins, so whichever install reaches the Gallery first wins a race: if PowerShellBuild's own install gets there before our pinned Pester 6.1.0 is on disk, PowerShellGet fetches whatever's newest on PSGallery as a second, stray copy (confirmed non-deterministic locally, depending on install order). PowerShellBuild's bundled Test task (Test-PSBuildPester.ps1) then calls `Import-Module Pester -MinimumVersion 5.0.0` with no exact version pin. -MinimumVersion always resolves to the HIGHEST matching version found anywhere on $env:PSModulePath - never whatever's already loaded under that name - so that stray newer copy gets a fresh load attempt and collides with the Pester.dll our own pinned import already Add-Type'd into the process. Fixes: - requirements.psd1: PSDepend bumped to 0.7.0-beta1 (prerelease; AllowPrerelease flagged explicitly), whose transitive-dependency resolution (separate root installation contexts, transitive packages installed first) narrows this class of collision for dependencies PSDepend itself resolves. - build.ps1 bootstrap: - PSDepend's own version is now read from requirements.psd1 and installed/imported by that exact (prerelease-aware) version, instead of "any version already on the machine" - the previous check never actually enforced the pin, and couldn't install a prerelease at all (Install-Module rejects a prerelease RequiredVersion without -AllowPrerelease). - Pester is now installed and globally imported at the pinned version *before* Invoke-PSDepend runs, so PowerShellGet already finds a satisfying version on disk when it resolves PowerShellBuild's RequiredModules and never reaches for anything else - closing the install-order race at its source. - A defense-in-depth pass after Invoke-PSDepend prunes any other installed Pester version and reasserts the pinned one, in case something else still lands a stray copy. Verified: - 3/3 clean-slate local runs (.\build.ps1 -Bootstrap -Task Test) pass consistently: 368 passed, 0 failed, 5 skipped, Analyze clean - including with a second, newer Pester version pre-seeded on disk to force the exact collision this fixes. - Real CI on this branch: ubuntu-latest and macOS-latest both pass Run Tests (368/0/5 each) after two failed attempts that isolated the actual cause (PSDepend bump alone did not fix it; the install-order fix did). windows-latest's Pester task also passes 368/0/5 cleanly, confirming the collision itself is resolved on every platform; that job currently fails afterward in the unrelated ANALYZE task (PSScriptAnalyzer NullReferenceException in Test-PSBuildScriptAnalysis.ps1), a separate, pre-existing issue not touched by this change. --- build.ps1 | 39 ++++++++++++++++++++++++++++++++++++--- requirements.psd1 | 5 ++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/build.ps1 b/build.ps1 index 8c1a6ea..97b6a07 100644 --- a/build.ps1 +++ b/build.ps1 @@ -49,11 +49,44 @@ if ($Bootstrap.IsPresent) { Get-PackageProvider -Name Nuget -ForceBootstrap | Out-Null Set-PSRepository -Name PSGallery -InstallationPolicy Trusted if ((Test-Path -Path ./requirements.psd1)) { - if (-not (Get-Module -Name PSDepend -ListAvailable)) { - Install-Module -Name PSDepend -Repository PSGallery -Scope CurrentUser -Force + $psDependVersion = (Import-PowerShellDataFile -Path ./requirements.psd1).PSDepend.Version + $psDependBaseVersion, $psDependPrerelease = $psDependVersion -split '-', 2 + $psDependInstalled = Get-Module -Name PSDepend -ListAvailable | Where-Object { + $candidateVersion = "$($_.Version)" + if ($_.PrivateData.PSData.Prerelease) { $candidateVersion += "-$($_.PrivateData.PSData.Prerelease)" } + $candidateVersion -eq $psDependVersion } - Import-Module -Name PSDepend -Verbose:$false + if (-not $psDependInstalled) { + $psDependInstallParams = @{ RequiredVersion = $psDependVersion } + if ($psDependPrerelease) { $psDependInstallParams.AllowPrerelease = $true } + Install-Module -Name PSDepend -Repository PSGallery -Scope CurrentUser -Force @psDependInstallParams + } + Import-Module -Name PSDepend -RequiredVersion $psDependBaseVersion -Verbose:$false + + # PowerShellBuild (installed below via requirements.psd1) declares `Pester >= 5.6.1` as + # its own RequiredModules dependency, with no upper bound. Install-Module always + # auto-resolves a target's RequiredModules, independently of whatever this repo's own + # `Pester` entry pins - and if our pin isn't on disk yet when PowerShellBuild gets + # installed, PowerShellGet fetches whatever's newest on PSGallery as a second, stray + # copy. PowerShellBuild's bundled Test task later calls `Import-Module Pester + # -MinimumVersion 5.0.0` (no exact pin), which always resolves to the HIGHEST matching + # version found on $env:PSModulePath - not whatever's already loaded - so that stray + # copy collides with the Pester.dll our own pinned import already loaded in-process. + # Installing (and importing) our pin first means PowerShellGet already finds it + # satisfying PowerShellBuild's constraint and never reaches for anything else. + $pesterVersion = (Import-PowerShellDataFile -Path ./requirements.psd1).Pester.Version + if (-not (Get-Module -Name Pester -ListAvailable | Where-Object Version -eq [version]$pesterVersion)) { + Install-Module -Name Pester -RequiredVersion $pesterVersion -Scope CurrentUser -Force -SkipPublisherCheck + } + Import-Module -Name Pester -RequiredVersion $pesterVersion -Global -Force -Verbose:$false + Invoke-PSDepend -Path './requirements.psd1' -Install -Import -Force -WarningAction SilentlyContinue + + # Defense in depth: prune any stray Pester version that still slipped onto disk (e.g. a + # concurrent PSDepend install) and reassert our pin at global scope. + Get-Module -Name Pester -ListAvailable | Where-Object { $_.Version -ne [version]$pesterVersion } | + ForEach-Object { Remove-Item -LiteralPath $_.ModuleBase -Recurse -Force -ErrorAction SilentlyContinue } + Import-Module -Name Pester -RequiredVersion $pesterVersion -Global -Force -Verbose:$false } else { Write-Warning 'No [requirements.psd1] found. Skipping build dependency installation.' } diff --git a/requirements.psd1 b/requirements.psd1 index bfa8b9e..821c083 100644 --- a/requirements.psd1 +++ b/requirements.psd1 @@ -1,6 +1,9 @@ @{ PSDepend = @{ - Version = '0.4.1' + Version = '0.7.0-beta1' + Parameters = @{ + AllowPrerelease = $true + } } PSDependOptions = @{ Target = 'CurrentUser' From 81af12946a00cbcb55fe28132b5d317ab1393d08 Mon Sep 17 00:00:00 2001 From: HeyItsGilbert Date: Tue, 3 Nov 2026 19:09:36 +0000 Subject: [PATCH 2/2] fix: address Copilot review feedback on PR #113 - Import PSDepend by its matched module's own manifest path rather than a bare base-version Import-Module call: -RequiredVersion drops the prerelease label, so if a differently-labeled (or stable) module sharing that numeric version ever appears earlier on $env:PSModulePath, Import-Module could silently resolve to that one instead of the prerelease actually pinned. - Stop re-processing the self-referential 'PSDepend' entry through Invoke-PSDepend: its PSGalleryModule handler's existing-version check compares $_.Version.ToString() (which drops any prerelease label) against the full 'X.Y.Z-prerelease' requirement string, so it never matches and silently re-queries/reinstalls PSDepend from the gallery on every single bootstrap run, even though the exact pinned version is already installed and imported by that point. Invoke-PSDepend now runs against every *other* declared dependency via -InputObject. - Read requirements.psd1 once into $requirements instead of re-parsing the file for each of PSDepend's and Pester's version lookups. On the (refuted) lock-file suggestion: PSDepend 0.7.0-beta1's locked transitive-dependency resolution only applies with a neighboring requirements.lock.json, which this checkout doesn't have - but the actual Pester collision is already closed by the install-order fix in the prior commit, independent of PSDepend's lock mechanism entirely (PowerShellGet's RequiredModules auto-install, invoked inside a single Install-Module call, happens beneath PSDepend regardless of locking). Real CI already proves this: ubuntu/macOS/windows/PS5.1 all green on the prior commit. A lock file remains available as optional future defense-in-depth but isn't required to close this issue. Verified: 5 consecutive clean-slate local runs (.\build.ps1 -Bootstrap -Task Test), including the exact forced-collision scenario (a second, newer Pester pre-seeded on disk), all pass 368/0/5; confirmed via a second run's verbose log that PSDepend's own entry no longer triggers a redundant Install-Module call. --- build.ps1 | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/build.ps1 b/build.ps1 index 97b6a07..79586be 100644 --- a/build.ps1 +++ b/build.ps1 @@ -49,19 +49,29 @@ if ($Bootstrap.IsPresent) { Get-PackageProvider -Name Nuget -ForceBootstrap | Out-Null Set-PSRepository -Name PSGallery -InstallationPolicy Trusted if ((Test-Path -Path ./requirements.psd1)) { - $psDependVersion = (Import-PowerShellDataFile -Path ./requirements.psd1).PSDepend.Version - $psDependBaseVersion, $psDependPrerelease = $psDependVersion -split '-', 2 - $psDependInstalled = Get-Module -Name PSDepend -ListAvailable | Where-Object { - $candidateVersion = "$($_.Version)" - if ($_.PrivateData.PSData.Prerelease) { $candidateVersion += "-$($_.PrivateData.PSData.Prerelease)" } - $candidateVersion -eq $psDependVersion + $requirements = Import-PowerShellDataFile -Path ./requirements.psd1 + + $psDependVersion = $requirements.PSDepend.Version + $psDependPrerelease = ($psDependVersion -split '-', 2)[1] + $matchPSDepend = { + Get-Module -Name PSDepend -ListAvailable | Where-Object { + $candidateVersion = "$($_.Version)" + if ($_.PrivateData.PSData.Prerelease) { $candidateVersion += "-$($_.PrivateData.PSData.Prerelease)" } + $candidateVersion -eq $psDependVersion + } | Select-Object -First 1 } - if (-not $psDependInstalled) { + $psDependModule = & $matchPSDepend + if (-not $psDependModule) { $psDependInstallParams = @{ RequiredVersion = $psDependVersion } if ($psDependPrerelease) { $psDependInstallParams.AllowPrerelease = $true } Install-Module -Name PSDepend -Repository PSGallery -Scope CurrentUser -Force @psDependInstallParams + $psDependModule = & $matchPSDepend } - Import-Module -Name PSDepend -RequiredVersion $psDependBaseVersion -Verbose:$false + # Import by the matched module's own manifest path, not just its numeric base version: + # -RequiredVersion drops the prerelease label, so if a differently-labeled (or stable) + # module sharing that same numeric version ever appears earlier on $env:PSModulePath, + # Import-Module could resolve to that one instead of the prerelease actually pinned. + Import-Module -Name $psDependModule.Path -Verbose:$false # PowerShellBuild (installed below via requirements.psd1) declares `Pester >= 5.6.1` as # its own RequiredModules dependency, with no upper bound. Install-Module always @@ -74,13 +84,22 @@ if ($Bootstrap.IsPresent) { # copy collides with the Pester.dll our own pinned import already loaded in-process. # Installing (and importing) our pin first means PowerShellGet already finds it # satisfying PowerShellBuild's constraint and never reaches for anything else. - $pesterVersion = (Import-PowerShellDataFile -Path ./requirements.psd1).Pester.Version + $pesterVersion = $requirements.Pester.Version if (-not (Get-Module -Name Pester -ListAvailable | Where-Object Version -eq [version]$pesterVersion)) { Install-Module -Name Pester -RequiredVersion $pesterVersion -Scope CurrentUser -Force -SkipPublisherCheck } Import-Module -Name Pester -RequiredVersion $pesterVersion -Global -Force -Verbose:$false - Invoke-PSDepend -Path './requirements.psd1' -Install -Import -Force -WarningAction SilentlyContinue + # Invoke-PSDepend's own PSGalleryModule handler compares an installed module's + # $_.Version.ToString() (which drops any prerelease label) against the full + # 'X.Y.Z-prerelease' requirement string above, so it never recognizes our pin as already + # satisfied and would redundantly re-query/reinstall PSDepend from the gallery on every + # single bootstrap run. We've already installed and imported the exact pinned version + # above, so hand Invoke-PSDepend every *other* declared dependency instead of + # reprocessing this self-referential entry. + $otherDependencies = $requirements.Clone() + $otherDependencies.Remove('PSDepend') + Invoke-PSDepend -InputObject $otherDependencies -Install -Import -Force -WarningAction SilentlyContinue # Defense in depth: prune any stray Pester version that still slipped onto disk (e.g. a # concurrent PSDepend install) and reassert our pin at global scope.