From 95ccf34d32bafccce7f9535ed37efab70b93ab9c Mon Sep 17 00:00:00 2001 From: rebuildup Date: Mon, 30 Nov 2026 14:49:52 +0900 Subject: [PATCH] =?UTF-8?q?fix(#287):=20Windows=20PowerShell=205.1=20?= =?UTF-8?q?=E3=81=AE=20module=20path=20=E3=82=92=E5=BE=A9=E5=85=83?= =?UTF-8?q?=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PowerShell 7 (MSIX) のプロセスから `powershell.exe` を起動すると、PowerShell 7 が設定した PSModulePath を継承する。5.1 はそこで PowerShell 7 版の module set を autoload し、5.1 側の cmdlet を失う。 - `Get-FileHash` が無い → `platforms/windows-11/converge.ps1` の `Get-LockHash` が例外になり `mise-apply` step が error になる。 - `Import-PowerShellDataFile` が無い → `platforms/windows-11/verify.ps1` が 実行できない。 Scheduled Task からは再現しない (task の environment は system のまま) が、 ユーザーが PowerShell 7 の terminal から実行すると必ず再現する。 - `platforms/windows-11/ps-module-path.ps1` に共通処理を置く。 - `converge.ps1` と `verify.ps1` は起動時に module path を machine の値に戻す。 PowerShell 7 は自前の module path を使うため変更しない。 - `scripts/test-ps-module-path.ps1` で、Utility module を空の module で shadow して失敗を再現し、reset 後に Get-FileHash / Import-PowerShellDataFile が解決することを検証する。CI に追加。 Co-Authored-By: Claude Code --- .github/workflows/ci.yml | 7 ++ platforms/windows-11/converge.ps1 | 7 ++ platforms/windows-11/ps-module-path.ps1 | 24 ++++++ platforms/windows-11/verify.ps1 | 7 ++ scripts/ci.sh | 9 +++ scripts/test-ps-module-path.ps1 | 98 +++++++++++++++++++++++++ 6 files changed, 152 insertions(+) create mode 100644 platforms/windows-11/ps-module-path.ps1 create mode 100644 scripts/test-ps-module-path.ps1 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'