From 7d44cd9839781b7f142da27bdffc20c5532df97d Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 21 Nov 2026 11:06:16 +0100 Subject: [PATCH] Validate bootstrap PowerShell and select its MSI installer --- TASKS.md | 2 ++ bootstrap.ps1 | 27 +++++++++++++++++++---- tests/Bootstrap.Tests.ps1 | 45 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/TASKS.md b/TASKS.md index 4567727..931cbb1 100644 --- a/TASKS.md +++ b/TASKS.md @@ -168,3 +168,5 @@ open until it is run on disposable machines with the required account setups. ## PowerShell prerequisite reliability - [x] Remove PowerShell from routine software updates and block runtime replacement in plans/workers, including missing inventory and stale plans (Linux mocked package tests pass). +- [x] Select the PowerShell MSI during bootstrap, probe runtime startup with retained diagnostics, and bind the launcher to the validated executable (Linux mocked bootstrap/output tests pass). +- [ ] Verify MSI setup, broken .NET-tool/PATH recovery, and separate PowerShell updates on disposable Windows. diff --git a/bootstrap.ps1 b/bootstrap.ps1 index e9738c8..d6d21cb 100644 --- a/bootstrap.ps1 +++ b/bootstrap.ps1 @@ -51,20 +51,38 @@ function Test-Administrator { function Invoke-BootstrapWingetInstall { param([Parameter(Mandatory = $true)][string]$Id) - & winget.exe install --id $Id --exact --source winget --silent --disable-interactivity --accept-package-agreements --accept-source-agreements --scope machine + $installerArguments = @('install', '--id', $Id, '--exact', '--source', 'winget', '--silent', '--disable-interactivity', '--accept-package-agreements', '--accept-source-agreements', '--scope', 'machine') + # Require the self-contained MSI instead of following WinGet's MSIX default. + if ($Id -eq 'Microsoft.PowerShell') { $installerArguments += @('--installer-type', 'wix') } + & winget.exe @installerArguments if ($LASTEXITCODE -ne 0) { throw "WinGet failed to install '$Id' (exit $LASTEXITCODE)." } } function Find-PowerShell7Path { - $command = Get-Command pwsh.exe -ErrorAction SilentlyContinue - if ($command) { return $command.Source } if (-not [string]::IsNullOrWhiteSpace($env:ProgramFiles)) { $installedPath = Join-Path $env:ProgramFiles 'PowerShell\7\pwsh.exe' if (Test-Path -LiteralPath $installedPath -PathType Leaf) { return $installedPath } } + $command = Get-Command pwsh.exe -CommandType Application -ErrorAction SilentlyContinue + if ($command) { return $command.Source } return $null } +function Assert-BootstrapPowerShell { + param([Parameter(Mandatory = $true)][string]$Path) + # Preserve native startup diagnostics (including missing .NET) under PS 5.1. + $previousPreference = $ErrorActionPreference + try { + $ErrorActionPreference = 'Continue' + $diagnostics = & $Path -NoLogo -NoProfile -NonInteractive -Command 'if ($PSVersionTable.PSVersion.Major -lt 7) { exit 1 }; Write-Output "ConfigCentral.PowerShell7.Ready"; exit 0' 2>&1 + $code = $LASTEXITCODE + } + finally { $ErrorActionPreference = $previousPreference } + if ($code -ne 0 -or @($diagnostics) -notcontains 'ConfigCentral.PowerShell7.Ready') { + throw "PowerShell at '$Path' could not start (exit $code): $($diagnostics -join [Environment]::NewLine). Repair that installation or install the official PowerShell 7 MSI, then rerun setup." + } +} + function Find-GitPath { $command = Get-Command git.exe -ErrorAction SilentlyContinue if ($command) { return $command.Source } @@ -143,6 +161,7 @@ $git = Find-GitPath if (-not $git) { throw 'Git was installed but git.exe could not be located.' } $powerShell7 = Find-PowerShell7Path if (-not $powerShell7) { throw 'PowerShell 7 was installed but pwsh.exe could not be located.' } +Assert-BootstrapPowerShell -Path $powerShell7 if (Test-Path -LiteralPath (Join-Path $Checkout '.git')) { Push-Location $Checkout try { @@ -185,7 +204,7 @@ $settings | ConvertTo-Json -Depth 8 | Set-Content -LiteralPath (Join-Path $dataR $launcherDirectory = Join-Path $env:LOCALAPPDATA 'ConfigCentral\bin' New-Item -ItemType Directory -Path $launcherDirectory -Force | Out-Null $launcherPath = Join-Path $launcherDirectory 'cc.cmd' -$launcherContent = "@echo off`r`npwsh.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -File `"$Checkout\cc.ps1`" %*`r`n" +$launcherContent = "@echo off`r`n`"$powerShell7`" -NoLogo -NoProfile -ExecutionPolicy Bypass -File `"$Checkout\cc.ps1`" %*`r`n" [System.IO.File]::WriteAllText($launcherPath, $launcherContent, [Text.Encoding]::ASCII) $userPath = [Environment]::GetEnvironmentVariable('Path', 'User') if (($userPath -split ';') -notcontains $launcherDirectory) { diff --git a/tests/Bootstrap.Tests.ps1 b/tests/Bootstrap.Tests.ps1 index 9083588..ac2eb6d 100644 --- a/tests/Bootstrap.Tests.ps1 +++ b/tests/Bootstrap.Tests.ps1 @@ -42,3 +42,48 @@ Describe 'Bootstrap invocation' { finally { $env:ProgramFiles = $previousProgramFiles } } } + +Describe 'Bootstrap PowerShell prerequisites' { + BeforeAll { + foreach ($name in @('Find-PowerShell7Path', 'Invoke-BootstrapWingetInstall', 'Assert-BootstrapPowerShell')) { + $fn = $bootstrapAst.Find({ param($node) $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq $name }, $true) + . ([scriptblock]::Create($fn.Extent.Text)) + } + function winget.exe { $global:LASTEXITCODE = 0 } + function Test-Runtime { $global:LASTEXITCODE = 0; 'ConfigCentral.PowerShell7.Ready' } + } + + It 'requests the MSI only for PowerShell' { + Mock winget.exe { $global:LASTEXITCODE = 0 } + Invoke-BootstrapWingetInstall -Id Microsoft.PowerShell + Should -Invoke winget.exe -Times 1 -Exactly -ParameterFilter { $args -contains '--installer-type' -and $args -contains 'wix' -and $args -contains 'machine' } + Invoke-BootstrapWingetInstall -Id Git.Git + Should -Invoke winget.exe -Times 1 -Exactly -ParameterFilter { $args -contains 'Git.Git' -and $args -notcontains '--installer-type' } + } + + It 'prefers the standard installation over a dotnet shim on PATH' { + $previousProgramFiles = $env:ProgramFiles + try { + $env:ProgramFiles = $TestDrive + $expected = Join-Path $TestDrive 'PowerShell/7/pwsh.exe' + New-Item -ItemType File -Path $expected -Force | Out-Null + Mock Get-Command { [pscustomobject]@{ Source = 'broken-dotnet-shim' } } -ParameterFilter { $Name -eq 'pwsh.exe' } + Find-PowerShell7Path | Should -Be $expected + } + finally { $env:ProgramFiles = $previousProgramFiles } + } + + It 'accepts a working runtime' { + { Assert-BootstrapPowerShell -Path Test-Runtime } | Should -Not -Throw + } + + It 'retains startup diagnostics and recommends repair' { + Mock Test-Runtime { $global:LASTEXITCODE = 1; 'You must install or update .NET' } + { Assert-BootstrapPowerShell -Path Test-Runtime } | Should -Throw '*install or update .NET*Repair*' + } + + It 'rejects a shim that exits successfully without starting PowerShell' { + Mock Test-Runtime { $global:LASTEXITCODE = 0 } + { Assert-BootstrapPowerShell -Path Test-Runtime } | Should -Throw '*could not start*' + } +}