Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
7 changes: 7 additions & 0 deletions platforms/windows-11/converge.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
24 changes: 24 additions & 0 deletions platforms/windows-11/ps-module-path.ps1
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 7 additions & 0 deletions platforms/windows-11/verify.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 '..\..')

Expand Down
9 changes: 9 additions & 0 deletions scripts/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
98 changes: 98 additions & 0 deletions scripts/test-ps-module-path.ps1
Original file line number Diff line number Diff line change
@@ -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'
Loading