From b06dec0f7c9d94117acb8041a1b85658ebc067a7 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 8 Jul 2026 02:57:51 +0200 Subject: [PATCH 1/6] Install built module into its manifest version folder Install-PSModule copied the built module into a hard-coded 999.0.0 folder, so Import-Module failed once the manifest carried a real (non-placeholder) version. Read the manifest ModuleVersion and install into the matching version folder, falling back to 999.0.0 only when the manifest is unstamped. --- src/Helpers/Helpers.psm1 | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Helpers/Helpers.psm1 b/src/Helpers/Helpers.psm1 index 5ddc159..4a2a4b0 100644 --- a/src/Helpers/Helpers.psm1 +++ b/src/Helpers/Helpers.psm1 @@ -313,8 +313,15 @@ function Install-PSModule { Write-Host '::group::Resolving dependencies' Resolve-PSModuleDependency -ManifestFilePath $manifestFilePath Write-Host '::endgroup::' + # Install into a version folder that matches the manifest's ModuleVersion so PowerShell + # accepts the module. Fall back to the 999.0.0 placeholder only when the manifest is + # unstamped (e.g. a build that opted out of version resolution). + $moduleVersion = (Import-PowerShellDataFile -Path $manifestFilePath).ModuleVersion + if ([string]::IsNullOrWhiteSpace($moduleVersion)) { + $moduleVersion = '999.0.0' + } $PSModulePath = $env:PSModulePath -split [System.IO.Path]::PathSeparator | Select-Object -First 1 - $codePath = New-Item -Path "$PSModulePath/$moduleName/999.0.0" -ItemType Directory -Force | Select-Object -ExpandProperty FullName + $codePath = New-Item -Path "$PSModulePath/$moduleName/$moduleVersion" -ItemType Directory -Force | Select-Object -ExpandProperty FullName Copy-Item -Path "$Path/*" -Destination $codePath -Recurse -Force Write-Host '::group::Importing module' Import-Module -Name $moduleName -Verbose From 22e7c03d61d8319363c506c55b620335dd4a4254 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 8 Jul 2026 03:06:38 +0200 Subject: [PATCH 2/6] Use Join-Path for the version folder and import the fresh copy by manifest path Addresses review feedback: build the install path with Join-Path (cross-platform separators) and import the just-installed module by its manifest path so it is not shadowed by another version already on PSModulePath. --- src/Helpers/Helpers.psm1 | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Helpers/Helpers.psm1 b/src/Helpers/Helpers.psm1 index 4a2a4b0..77b9e14 100644 --- a/src/Helpers/Helpers.psm1 +++ b/src/Helpers/Helpers.psm1 @@ -321,10 +321,12 @@ function Install-PSModule { $moduleVersion = '999.0.0' } $PSModulePath = $env:PSModulePath -split [System.IO.Path]::PathSeparator | Select-Object -First 1 - $codePath = New-Item -Path "$PSModulePath/$moduleName/$moduleVersion" -ItemType Directory -Force | Select-Object -ExpandProperty FullName + $codePath = New-Item -Path (Join-Path -Path $PSModulePath -ChildPath $moduleName -AdditionalChildPath $moduleVersion) -ItemType Directory -Force | Select-Object -ExpandProperty FullName Copy-Item -Path "$Path/*" -Destination $codePath -Recurse -Force Write-Host '::group::Importing module' - Import-Module -Name $moduleName -Verbose + # Import the freshly-installed copy explicitly by its manifest path so it is not shadowed + # by another version of the same module that may already be on $env:PSModulePath. + Import-Module -Name (Join-Path -Path $codePath -ChildPath "$moduleName.psd1") -Verbose Write-Host '::endgroup::' if ($PassThru) { return $codePath From 93868d8cdf376e8936ce1635f95b29332336f50b Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 8 Jul 2026 03:09:17 +0200 Subject: [PATCH 3/6] Force-reload the freshly-installed module on import Add -Force so the just-installed copy replaces any already-loaded version of the same module, guaranteeing the correct version is used by downstream tests. --- src/Helpers/Helpers.psm1 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Helpers/Helpers.psm1 b/src/Helpers/Helpers.psm1 index 77b9e14..4a6d259 100644 --- a/src/Helpers/Helpers.psm1 +++ b/src/Helpers/Helpers.psm1 @@ -324,9 +324,10 @@ function Install-PSModule { $codePath = New-Item -Path (Join-Path -Path $PSModulePath -ChildPath $moduleName -AdditionalChildPath $moduleVersion) -ItemType Directory -Force | Select-Object -ExpandProperty FullName Copy-Item -Path "$Path/*" -Destination $codePath -Recurse -Force Write-Host '::group::Importing module' - # Import the freshly-installed copy explicitly by its manifest path so it is not shadowed - # by another version of the same module that may already be on $env:PSModulePath. - Import-Module -Name (Join-Path -Path $codePath -ChildPath "$moduleName.psd1") -Verbose + # Import the freshly-installed copy explicitly by its manifest path (with -Force) so it is + # not shadowed by another version of the same module that may already be loaded or present + # on $env:PSModulePath. + Import-Module -Name (Join-Path -Path $codePath -ChildPath "$moduleName.psd1") -Force -Verbose Write-Host '::endgroup::' if ($PassThru) { return $codePath From 88c954d09f09aaf17ff3c45b28116a76d407ff81 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 8 Jul 2026 03:11:20 +0200 Subject: [PATCH 4/6] Use nested Join-Path for Windows PowerShell 5.1 compatibility Join-Path -AdditionalChildPath is PowerShell 6+ only; nested Join-Path calls keep the same behavior while remaining compatible with Windows PowerShell 5.1. --- src/Helpers/Helpers.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Helpers/Helpers.psm1 b/src/Helpers/Helpers.psm1 index 4a6d259..45990c6 100644 --- a/src/Helpers/Helpers.psm1 +++ b/src/Helpers/Helpers.psm1 @@ -321,7 +321,7 @@ function Install-PSModule { $moduleVersion = '999.0.0' } $PSModulePath = $env:PSModulePath -split [System.IO.Path]::PathSeparator | Select-Object -First 1 - $codePath = New-Item -Path (Join-Path -Path $PSModulePath -ChildPath $moduleName -AdditionalChildPath $moduleVersion) -ItemType Directory -Force | Select-Object -ExpandProperty FullName + $codePath = New-Item -Path (Join-Path -Path (Join-Path -Path $PSModulePath -ChildPath $moduleName) -ChildPath $moduleVersion) -ItemType Directory -Force | Select-Object -ExpandProperty FullName Copy-Item -Path "$Path/*" -Destination $codePath -Recurse -Force Write-Host '::group::Importing module' # Import the freshly-installed copy explicitly by its manifest path (with -Force) so it is From 383aa36cd1b9dc838157cc4d08bb98cebc603216 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 8 Jul 2026 03:16:50 +0200 Subject: [PATCH 5/6] Validate manifest ModuleVersion before using it as a path segment Parse ModuleVersion as [version] and fail fast on a malformed value so a crafted manifest cannot inject path separators/traversal into the install path. Empty/unstamped manifests still fall back to the 999.0.0 placeholder. --- src/Helpers/Helpers.psm1 | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Helpers/Helpers.psm1 b/src/Helpers/Helpers.psm1 index 45990c6..31d94c2 100644 --- a/src/Helpers/Helpers.psm1 +++ b/src/Helpers/Helpers.psm1 @@ -315,10 +315,18 @@ function Install-PSModule { Write-Host '::endgroup::' # Install into a version folder that matches the manifest's ModuleVersion so PowerShell # accepts the module. Fall back to the 999.0.0 placeholder only when the manifest is - # unstamped (e.g. a build that opted out of version resolution). - $moduleVersion = (Import-PowerShellDataFile -Path $manifestFilePath).ModuleVersion - if ([string]::IsNullOrWhiteSpace($moduleVersion)) { + # unstamped (e.g. a build that opted out of version resolution). Validate the version as a + # real [version] so a malformed manifest cannot inject path separators/traversal into the + # install path. + $manifestVersion = (Import-PowerShellDataFile -Path $manifestFilePath).ModuleVersion + if ([string]::IsNullOrWhiteSpace($manifestVersion)) { $moduleVersion = '999.0.0' + } else { + $parsedVersion = $null + if (-not [version]::TryParse($manifestVersion, [ref] $parsedVersion)) { + throw "ModuleVersion '$manifestVersion' in [$manifestFilePath] is not a valid version." + } + $moduleVersion = $parsedVersion.ToString() } $PSModulePath = $env:PSModulePath -split [System.IO.Path]::PathSeparator | Select-Object -First 1 $codePath = New-Item -Path (Join-Path -Path (Join-Path -Path $PSModulePath -ChildPath $moduleName) -ChildPath $moduleVersion) -ItemType Directory -Force | Select-Object -ExpandProperty FullName From 41d79663b9970bb950e0cc8affbd3a636042ff8a Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 8 Jul 2026 12:10:24 +0200 Subject: [PATCH 6/6] Fix Install-PSModule real-version import coverage --- .github/workflows/Action-Test.yml | 4 + src/Helpers/Helpers.psm1 | 8 +- tests/Install-PSModule.Tests.ps1 | 132 ++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 tests/Install-PSModule.Tests.ps1 diff --git a/.github/workflows/Action-Test.yml b/.github/workflows/Action-Test.yml index 4e9b548..d742253 100644 --- a/.github/workflows/Action-Test.yml +++ b/.github/workflows/Action-Test.yml @@ -38,3 +38,7 @@ jobs: } else { throw "Helpers module not found!" } + + - name: Run Install-PSModule regression tests + shell: pwsh + run: ./tests/Install-PSModule.Tests.ps1 diff --git a/src/Helpers/Helpers.psm1 b/src/Helpers/Helpers.psm1 index 31d94c2..a4bda63 100644 --- a/src/Helpers/Helpers.psm1 +++ b/src/Helpers/Helpers.psm1 @@ -329,13 +329,17 @@ function Install-PSModule { $moduleVersion = $parsedVersion.ToString() } $PSModulePath = $env:PSModulePath -split [System.IO.Path]::PathSeparator | Select-Object -First 1 - $codePath = New-Item -Path (Join-Path -Path (Join-Path -Path $PSModulePath -ChildPath $moduleName) -ChildPath $moduleVersion) -ItemType Directory -Force | Select-Object -ExpandProperty FullName + $moduleInstallRoot = Join-Path -Path $PSModulePath -ChildPath $moduleName + $moduleInstallPath = Join-Path -Path $moduleInstallRoot -ChildPath $moduleVersion + $codePath = New-Item -Path $moduleInstallPath -ItemType Directory -Force | + Select-Object -ExpandProperty FullName Copy-Item -Path "$Path/*" -Destination $codePath -Recurse -Force Write-Host '::group::Importing module' # Import the freshly-installed copy explicitly by its manifest path (with -Force) so it is # not shadowed by another version of the same module that may already be loaded or present # on $env:PSModulePath. - Import-Module -Name (Join-Path -Path $codePath -ChildPath "$moduleName.psd1") -Force -Verbose + $installedManifestPath = Join-Path -Path $codePath -ChildPath "$moduleName.psd1" + Import-Module -Name $installedManifestPath -Force -Global -Verbose Write-Host '::endgroup::' if ($PassThru) { return $codePath diff --git a/tests/Install-PSModule.Tests.ps1 b/tests/Install-PSModule.Tests.ps1 new file mode 100644 index 0000000..0de1255 --- /dev/null +++ b/tests/Install-PSModule.Tests.ps1 @@ -0,0 +1,132 @@ +[CmdletBinding()] +param() + +$ErrorActionPreference = 'Stop' + +$repoRoot = Split-Path -Path $PSScriptRoot -Parent +$helpersManifestPath = Join-Path -Path $repoRoot -ChildPath 'src/Helpers/Helpers.psd1' +$originalPSModulePath = $env:PSModulePath +$testRoot = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath "Install-PSModule-$([guid]::NewGuid())" + +$assert = { + param( + [bool] $Condition, + [string] $Message + ) + + if (-not $Condition) { + throw $Message + } +} + +try { + $testPSModulePath = Join-Path -Path $testRoot -ChildPath 'PSModules' + New-Item -Path $testPSModulePath -ItemType Directory -Force | Out-Null + $env:PSModulePath = $testPSModulePath + [System.IO.Path]::PathSeparator + $originalPSModulePath + + Remove-Module -Name Helpers -Force -ErrorAction SilentlyContinue + Import-Module -Name $helpersManifestPath -Force + + $realVersionSourcePath = Join-Path -Path $testRoot -ChildPath 'RealVersionModule' + New-Item -Path $realVersionSourcePath -ItemType Directory -Force | Out-Null + "function Get-RealVersionValue { 'real-1.2.3' }" | + Set-Content -Path (Join-Path -Path $realVersionSourcePath -ChildPath 'RealVersionModule.psm1') + @' +@{ + RootModule = 'RealVersionModule.psm1' + ModuleVersion = '1.2.3' + GUID = '11111111-1111-1111-1111-111111111111' + Author = 'Test' + FunctionsToExport = @('Get-RealVersionValue') +} +'@ | Set-Content -Path (Join-Path -Path $realVersionSourcePath -ChildPath 'RealVersionModule.psd1') + + $realVersionInstalledPath = Install-PSModule -Path $realVersionSourcePath -PassThru + $realVersionInstallRoot = Join-Path -Path $testPSModulePath -ChildPath 'RealVersionModule' + $expectedRealVersionPath = Join-Path -Path $realVersionInstallRoot -ChildPath '1.2.3' + $unexpectedPlaceholderPath = Join-Path -Path $realVersionInstallRoot -ChildPath '999.0.0' + $realVersionManifestPath = Join-Path -Path $expectedRealVersionPath -ChildPath 'RealVersionModule.psd1' + $returnedRealVersionPath = [System.IO.Path]::GetFullPath($realVersionInstalledPath) + $expectedRealVersionFullPath = [System.IO.Path]::GetFullPath($expectedRealVersionPath) + + & $assert ($returnedRealVersionPath -eq $expectedRealVersionFullPath) 'Expected Install-PSModule to return the real version path.' + & $assert (Test-Path -Path $realVersionManifestPath) 'Expected the module to be installed under version 1.2.3.' + & $assert (-not (Test-Path -Path $unexpectedPlaceholderPath)) 'Did not expect stamped modules to be installed under 999.0.0.' + & $assert ((Get-RealVersionValue) -eq 'real-1.2.3') 'Expected the real-version module command to be imported.' + + Remove-Module -Name RealVersionModule -Force -ErrorAction SilentlyContinue + + $shadowInstallRoot = Join-Path -Path $testPSModulePath -ChildPath 'ShadowModule' + $oldShadowInstallPath = Join-Path -Path $shadowInstallRoot -ChildPath '999.0.0' + New-Item -Path $oldShadowInstallPath -ItemType Directory -Force | Out-Null + "function Get-ShadowValue { 'old-999' }" | + Set-Content -Path (Join-Path -Path $oldShadowInstallPath -ChildPath 'ShadowModule.psm1') + @' +@{ + RootModule = 'ShadowModule.psm1' + ModuleVersion = '999.0.0' + GUID = '22222222-2222-2222-2222-222222222222' + Author = 'Test' + FunctionsToExport = @('Get-ShadowValue') +} +'@ | Set-Content -Path (Join-Path -Path $oldShadowInstallPath -ChildPath 'ShadowModule.psd1') + + $shadowSourcePath = Join-Path -Path $testRoot -ChildPath 'ShadowModule' + New-Item -Path $shadowSourcePath -ItemType Directory -Force | Out-Null + "function Get-ShadowValue { 'new-2.0.0' }" | + Set-Content -Path (Join-Path -Path $shadowSourcePath -ChildPath 'ShadowModule.psm1') + @' +@{ + RootModule = 'ShadowModule.psm1' + ModuleVersion = '2.0.0' + GUID = '33333333-3333-3333-3333-333333333333' + Author = 'Test' + FunctionsToExport = @('Get-ShadowValue') +} +'@ | Set-Content -Path (Join-Path -Path $shadowSourcePath -ChildPath 'ShadowModule.psd1') + + $oldShadowManifestPath = Join-Path -Path $oldShadowInstallPath -ChildPath 'ShadowModule.psd1' + Import-Module -Name $oldShadowManifestPath -Force + & $assert ((Get-ShadowValue) -eq 'old-999') 'Expected the fixture to preload the placeholder module.' + + $shadowInstalledPath = Install-PSModule -Path $shadowSourcePath -PassThru + $expectedShadowPath = Join-Path -Path $shadowInstallRoot -ChildPath '2.0.0' + $shadowCommand = Get-Command -Name Get-ShadowValue + $returnedShadowPath = [System.IO.Path]::GetFullPath($shadowInstalledPath) + $expectedShadowFullPath = [System.IO.Path]::GetFullPath($expectedShadowPath) + + & $assert ($returnedShadowPath -eq $expectedShadowFullPath) 'Expected the shadowed module under version 2.0.0.' + & $assert ((Get-ShadowValue) -eq 'new-2.0.0') 'Expected the real-version command to replace the preloaded 999.0.0 command.' + & $assert ($shadowCommand.Version.ToString() -eq '2.0.0') 'Expected command resolution to point at version 2.0.0.' + + Remove-Module -Name ShadowModule -Force -ErrorAction SilentlyContinue + + $badSourcePath = Join-Path -Path $testRoot -ChildPath 'BadModule' + New-Item -Path $badSourcePath -ItemType Directory -Force | Out-Null + "function Get-BadValue { 'bad' }" | + Set-Content -Path (Join-Path -Path $badSourcePath -ChildPath 'BadModule.psm1') + @' +@{ + RootModule = 'BadModule.psm1' + ModuleVersion = '1.0/bad' + GUID = '44444444-4444-4444-4444-444444444444' + Author = 'Test' + FunctionsToExport = @('Get-BadValue') +} +'@ | Set-Content -Path (Join-Path -Path $badSourcePath -ChildPath 'BadModule.psd1') + + $invalidVersionRejected = $false + try { + Install-PSModule -Path $badSourcePath + } catch { + $invalidVersionRejected = $_.Exception.Message -like '*is not a valid version*' + } + + & $assert $invalidVersionRejected 'Expected malformed ModuleVersion values to be rejected.' +} finally { + $env:PSModulePath = $originalPSModulePath + Remove-Module -Name Helpers, RealVersionModule, ShadowModule, BadModule -Force -ErrorAction SilentlyContinue + if (Test-Path -Path $testRoot) { + Remove-Item -Path $testRoot -Recurse -Force + } +}