diff --git a/build.ps1 b/build.ps1 index 8c1a6ea..79586be 100644 --- a/build.ps1 +++ b/build.ps1 @@ -49,11 +49,63 @@ 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 + $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 + } + $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 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 + # 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 = $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 PSDepend -Verbose:$false - Invoke-PSDepend -Path './requirements.psd1' -Install -Import -Force -WarningAction SilentlyContinue + Import-Module -Name Pester -RequiredVersion $pesterVersion -Global -Force -Verbose:$false + + # 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. + 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'