From 7998ee9852b859eb7176af5cc64e3579945bf094 Mon Sep 17 00:00:00 2001 From: Daniel Traynor <198297846+dantraynor@users.noreply.github.com> Date: Fri, 13 Nov 2026 02:02:24 -0500 Subject: [PATCH] fix(install): keep gh probes from aborting on Windows PowerShell 5.1 With ErrorActionPreference=Stop, Windows PowerShell 5.1 turns redirected native stderr into a terminating NativeCommandError. A gh that is installed but not logged in therefore aborted the install before the exit-code check could print the intended warning. Run the gh probes with a local Continue preference and rely on the exit code. Fixes #151 --- scripts/install.ps1 | 18 ++++++++++++------ scripts/install.test.ps1 | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/scripts/install.ps1 b/scripts/install.ps1 index 1ce2bd4..e0901e8 100644 --- a/scripts/install.ps1 +++ b/scripts/install.ps1 @@ -164,24 +164,30 @@ function Invoke-Download([string]$Uri, [string]$Path) { Invoke-WebRequest -UseBasicParsing -Uri $Uri -OutFile $Path } +function Invoke-GhProbe([string]$GhPath, [string[]]$Arguments) { + # Windows PowerShell 5.1 turns redirected native stderr into a terminating + # NativeCommandError under ErrorActionPreference=Stop, so a gh that is + # installed but not logged in would abort the install. Rely on the exit code. + $ErrorActionPreference = 'Continue' + & $GhPath @Arguments *> $null + return $LASTEXITCODE +} + function Test-AvailableAttestation([string]$ArtifactPath) { $gh = Get-Command gh -ErrorAction SilentlyContinue if (-not $gh) { Write-Warning 'GitHub CLI attestation verification is unavailable; the checksum and artifact share the GitHub Release trust boundary.' return } - & $gh.Source attestation verify --help *> $null - if ($LASTEXITCODE -ne 0) { + if ((Invoke-GhProbe $gh.Source @('attestation', 'verify', '--help')) -ne 0) { Write-Warning 'GitHub CLI attestation verification is unavailable; the checksum and artifact share the GitHub Release trust boundary.' return } - & $gh.Source auth status --hostname github.com *> $null - if ($LASTEXITCODE -ne 0) { + if ((Invoke-GhProbe $gh.Source @('auth', 'status', '--hostname', 'github.com')) -ne 0) { Write-Warning 'GitHub CLI is not authenticated; the checksum and artifact share the GitHub Release trust boundary.' return } - & $gh.Source attestation verify $ArtifactPath --hostname github.com --repo $Repository *> $null - if ($LASTEXITCODE -ne 0) { + if ((Invoke-GhProbe $gh.Source @('attestation', 'verify', $ArtifactPath, '--hostname', 'github.com', '--repo', $Repository)) -ne 0) { Fail 'GitHub artifact attestation verification failed.' } } diff --git a/scripts/install.test.ps1 b/scripts/install.test.ps1 index 51b2e55..cdb6a76 100644 --- a/scripts/install.test.ps1 +++ b/scripts/install.test.ps1 @@ -6,6 +6,7 @@ $Installer = Join-Path $Root 'scripts/install.ps1' . $Installer $ProductionInvokeInstalledHelper = (Get-Command Invoke-InstalledHelper -CommandType Function).ScriptBlock $ProductionGetNativeArchitecture = (Get-Command Get-NativeArchitecture -CommandType Function).ScriptBlock +$ProductionTestAvailableAttestation = (Get-Command Test-AvailableAttestation -CommandType Function).ScriptBlock $TestsRun = 0 $TestsFailed = 0 @@ -146,6 +147,41 @@ if ($helperSuccess -eq 0 -and $helperFailure -eq 7) { FailTest 'keeps helper stdout out of Invoke-InstalledHelper exit-code results' ("success=[$helperSuccess] failure=[$helperFailure]") } +# A gh that is installed but not logged in writes to stderr. Windows PowerShell +# 5.1 must not turn that redirected native stderr into a terminating error. +$ghFixture = Join-Path ([IO.Path]::GetTempPath()) ('tailchrome-gh-' + [guid]::NewGuid().ToString('N')) +New-Item -ItemType Directory -Path $ghFixture | Out-Null +if ($WindowsTest) { + @' +@echo off +if "%1"=="auth" ( + echo You are not logged into any GitHub hosts. 1>&2 + exit /b 1 +) +exit /b 0 +'@ | Set-Content -LiteralPath (Join-Path $ghFixture 'gh.cmd') -Encoding Ascii +} else { + $ghScript = Join-Path $ghFixture 'gh' + "#!/bin/sh`nif [ `"`$1`" = auth ]; then echo 'You are not logged into any GitHub hosts.' >&2; exit 1; fi`nexit 0`n" | + Set-Content -LiteralPath $ghScript -NoNewline + & chmod +x $ghScript +} +$savedPath = $env:PATH +$env:PATH = $ghFixture + [IO.Path]::PathSeparator + $env:PATH +try { + $ghWarnings = (& $ProductionTestAvailableAttestation 'unused-artifact' 3>&1 | Out-String) + if ($ghWarnings -like '*GitHub CLI is not authenticated*') { + Pass 'warns instead of aborting when gh is installed but not logged in' + } else { + FailTest 'warns instead of aborting when gh is installed but not logged in' ("output=[$ghWarnings]") + } +} catch { + FailTest 'warns instead of aborting when gh is installed but not logged in' $_.Exception.Message +} finally { + $env:PATH = $savedPath + Remove-Item -LiteralPath $ghFixture -Recurse -Force -ErrorAction SilentlyContinue +} + New-Fixture $popupBin = Join-Path $script:Fixture "scriptblock bin/owner's helper" New-Item -ItemType Directory -Path $popupBin -Force | Out-Null