diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 07241e15..c2643a14 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -869,6 +869,13 @@ jobs: Remove-Item -LiteralPath $root -Recurse -Force -ErrorAction SilentlyContinue } + # scripts/test-ps-module-path.ps1 checks that Windows PowerShell 5.1 keeps + # its own cmdlet set when the process is started from a PowerShell 7 + # session, which hands it PowerShell 7's PSModulePath. + - name: Validate the Windows PowerShell module path + shell: pwsh + run: ./scripts/test-ps-module-path.ps1 + - name: Validate adoption of existing installs shell: pwsh run: | diff --git a/platforms/windows-11/converge.ps1 b/platforms/windows-11/converge.ps1 index 0f5bb5d9..c35c593c 100644 --- a/platforms/windows-11/converge.ps1 +++ b/platforms/windows-11/converge.ps1 @@ -30,6 +30,13 @@ param( $ErrorActionPreference = 'Continue' Set-StrictMode -Version 2.0 +# Windows PowerShell 5.1 inherits PowerShell 7's PSModulePath when the process +# is started from a PowerShell 7 session, and then loses its own cmdlets +# (Get-FileHash, Import-PowerShellDataFile). The machine's own module path is +# the only one that carries them. +. (Join-Path (Split-Path -Parent $MyInvocation.MyCommand.Path) 'ps-module-path.ps1') +Reset-WindowsPowerShellModulePath | Out-Null + $script:Steps = New-Object System.Collections.ArrayList $script:DeployOk = $false $script:RunStarted = (Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ') diff --git a/platforms/windows-11/ps-module-path.ps1 b/platforms/windows-11/ps-module-path.ps1 new file mode 100644 index 00000000..571c3185 --- /dev/null +++ b/platforms/windows-11/ps-module-path.ps1 @@ -0,0 +1,24 @@ +# Restore the Windows PowerShell 5.1 module path. +# +# Windows PowerShell 5.1 inherits PowerShell 7's PSModulePath when the process +# is started from a PowerShell 7 session: the MSIX install of PowerShell 7 +# registers its own module directory in the environment, and every child +# process inherits it. 5.1 then autoloads the PowerShell 7 module set and +# loses its own cmdlets, so `Get-FileHash` and `Import-PowerShellDataFile` +# disappear and every script that uses them fails with a "not recognized" +# error. +# +# The machine's own module path is the only one that carries the 5.1 modules. +# PowerShell 7 assembles its own module path at startup, so it is left alone. +function Reset-WindowsPowerShellModulePath { + [CmdletBinding()] + param() + + if ($PSVersionTable.PSVersion.Major -ge 7) { return $false } + + $machineModulePath = [Environment]::GetEnvironmentVariable('PSModulePath', 'Machine') + if (-not $machineModulePath) { return $false } + + $env:PSModulePath = $machineModulePath + return $true +} diff --git a/platforms/windows-11/verify.ps1 b/platforms/windows-11/verify.ps1 index f152ab94..c9bc61f2 100644 --- a/platforms/windows-11/verify.ps1 +++ b/platforms/windows-11/verify.ps1 @@ -4,6 +4,13 @@ param() Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' +# Windows PowerShell 5.1 inherits PowerShell 7's PSModulePath when the process +# is started from a PowerShell 7 session, and then loses its own cmdlets +# (Get-FileHash, Import-PowerShellDataFile). The machine's own module path is +# the only one that carries them. +. (Join-Path (Split-Path -Parent $MyInvocation.MyCommand.Path) 'ps-module-path.ps1') +Reset-WindowsPowerShellModulePath | Out-Null + $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path $RepoRoot = Resolve-Path (Join-Path $ScriptDir '..\..') diff --git a/scripts/ci.sh b/scripts/ci.sh index 31f0e080..90429c0d 100755 --- a/scripts/ci.sh +++ b/scripts/ci.sh @@ -605,6 +605,15 @@ for template, installer, render_name in checks: print(f"ok {template} marker and placeholders match {installer}") PY +printf 'Checking the Windows PowerShell module path rules...\n' +if command -v pwsh >/dev/null 2>&1; then + # Windows PowerShell 5.1 inherits PowerShell 7's PSModulePath when it is + # started from a PowerShell 7 session and then loses its own cmdlets + # (Get-FileHash, Import-PowerShellDataFile). The converge and verification + # entry points restore the machine's module path (rebuildup/pc-setup#287). + pwsh -NoProfile -NonInteractive -File scripts/test-ps-module-path.ps1 +fi + printf 'Checking executable bits...\n' for script in "${shell_scripts[@]}"; do if [[ ! -x "$script" ]]; then diff --git a/scripts/test-ps-module-path.ps1 b/scripts/test-ps-module-path.ps1 new file mode 100644 index 00000000..a319eaef --- /dev/null +++ b/scripts/test-ps-module-path.ps1 @@ -0,0 +1,98 @@ +Set-StrictMode -Version Latest +$ErrorActionPreference = 'Stop' + +# Windows PowerShell 5.1 inherits PowerShell 7's PSModulePath when the process +# is started from a PowerShell 7 session, and then autoloads the PowerShell 7 +# module set instead of its own. Get-FileHash and Import-PowerShellDataFile +# disappear and every script that uses them fails (rebuildup/pc-setup#287). +# +# The test shadows the 5.1 Utility module with an empty one, which reproduces +# the failure deterministically, and then checks that the shared helper +# restores the machine's module path. + +$repo = Resolve-Path (Join-Path $PSScriptRoot '..') +$helper = Join-Path $repo 'platforms\windows-11\ps-module-path.ps1' +if (-not (Test-Path -LiteralPath $helper)) { throw "missing helper: $helper" } + +$root = Join-Path ([System.IO.Path]::GetTempPath()) "pc-setup-psmodulepath-$([guid]::NewGuid())" +New-Item -ItemType Directory -Force -Path $root | Out-Null + +try { + # An empty module with the name of the 5.1 Utility module. Autoloading it + # hides every cmdlet the real module provides. + $shadow = Join-Path $root 'shadow\Microsoft.PowerShell.Utility' + New-Item -ItemType Directory -Force -Path $shadow | Out-Null + @' +@{ + ModuleVersion = '3.1.0.0' + GUID = '00000000-0000-0000-0000-000000000000' + RootModule = '' + FunctionsToExport = @() + CmdletsToExport = @() + VariablesToExport = @() + AliasesToExport = @() +} +'@ | Set-Content -LiteralPath (Join-Path $shadow 'Microsoft.PowerShell.Utility.psd1') + + $probe = Join-Path $root 'probe.ps1' + @" +. '$helper' +`$changed = Reset-WindowsPowerShellModulePath +Write-Output "changed=`$changed" +if (Get-Command Get-FileHash -ErrorAction SilentlyContinue) { Write-Output 'has-hash' } +if (Get-Command Import-PowerShellDataFile -ErrorAction SilentlyContinue) { Write-Output 'has-datafile' } +"@ | Set-Content -LiteralPath $probe + + $oldModulePath = $env:PSModulePath + $env:PSModulePath = (Join-Path $root 'shadow') + ';' + $oldModulePath + + try { + $output = @(& powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File $probe 2>&1 | + ForEach-Object { [string]$_ }) + } + finally { + $env:PSModulePath = $oldModulePath + } + + $text = ($output -join "`n") + if ($text -notmatch 'has-hash') { + throw "Get-FileHash is still not available after the reset:`n$text" + } + if ($text -notmatch 'has-datafile') { + throw "Import-PowerShellDataFile is still not available after the reset:`n$text" + } + Write-Host 'ok the Windows PowerShell 5.1 module path is restored' + + # Without the reset the same environment must not resolve the cmdlets, + # otherwise the test above would pass without proving anything. + $control = Join-Path $root 'control.ps1' + @' +if (Get-Command Get-FileHash -ErrorAction SilentlyContinue) { Write-Output 'has-hash' } +'@ | Set-Content -LiteralPath $control + $env:PSModulePath = (Join-Path $root 'shadow') + ';' + $oldModulePath + try { + $controlOutput = @(& powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File $control 2>&1 | + ForEach-Object { [string]$_ }) + } + finally { + $env:PSModulePath = $oldModulePath + } + if (($controlOutput -join "`n") -match 'has-hash') { + throw 'the shadowed module did not hide Get-FileHash; the test setup is not reproducing the failure' + } + Write-Host 'ok the shadowed module reproduces the failure' + + # The two entry points have to apply the reset. + foreach ($entry in 'platforms\windows-11\converge.ps1', 'platforms\windows-11\verify.ps1') { + $text2 = Get-Content -LiteralPath (Join-Path $repo $entry) -Raw + if (-not $text2.Contains('Reset-WindowsPowerShellModulePath')) { + throw "$entry does not reset the Windows PowerShell module path" + } + } + Write-Host 'ok converge and verification apply the reset' +} +finally { + Remove-Item -LiteralPath $root -Recurse -Force -ErrorAction SilentlyContinue +} + +Write-Host 'ok Windows PowerShell module path rules verified'