Skip to content

Commit cc8e5cc

Browse files
committed
fix(install): repair stale Winget Node registrations
1 parent 8968e95 commit cc8e5cc

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

scripts/install.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ function Install-Node {
521521
if (Get-Command winget -ErrorAction SilentlyContinue) {
522522
Write-Host " Using winget..." -ForegroundColor Gray
523523
winget install OpenJS.NodeJS.LTS --source winget --accept-package-agreements --accept-source-agreements | Out-Host
524+
$wingetExitCode = $LASTEXITCODE
524525

525526
# Refresh PATH
526527
Refresh-ProcessPath
@@ -529,6 +530,16 @@ function Install-Node {
529530
Write-Host "[OK] Node.js installed via winget" -ForegroundColor Green
530531
return $true
531532
}
533+
if ($wingetExitCode -eq -1978335189) {
534+
Write-Host "[!] winget found a stale Node.js registration; retrying with --force" -ForegroundColor Yellow
535+
winget install OpenJS.NodeJS.LTS --source winget --force --accept-package-agreements --accept-source-agreements | Out-Host
536+
Refresh-ProcessPath
537+
Add-InstalledNodeToProcessPath | Out-Null
538+
if (Check-Node) {
539+
Write-Host "[OK] Node.js repaired via winget" -ForegroundColor Green
540+
return $true
541+
}
542+
}
532543
Write-Host "[!] winget completed, but Node.js is still unavailable in this shell" -ForegroundColor Yellow
533544
Write-Host "Restart PowerShell and re-run the installer if Node.js was installed successfully." -ForegroundColor Yellow
534545
return $false

test/scripts/install-ps1.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,40 @@ describe("install.ps1 failure handling", () => {
341341
"",
342342
].join("\n"),
343343
},
344+
{
345+
name: "winget-stale-node-registration",
346+
source: [
347+
scriptWithoutEntryPoint,
348+
"",
349+
"function Get-Command {",
350+
" [CmdletBinding()]",
351+
" param([string]$Name)",
352+
" if ($Name -eq 'winget') { return $true }",
353+
" return $null",
354+
"}",
355+
"filter Out-Host { }",
356+
"function Refresh-ProcessPath { }",
357+
"function Add-InstalledNodeToProcessPath { return $false }",
358+
"$script:wingetCalls = @()",
359+
"function winget {",
360+
" $script:wingetCalls += ($args -join ' ')",
361+
" if ($script:wingetCalls.Count -eq 1) {",
362+
" $global:LASTEXITCODE = -1978335189",
363+
" } else {",
364+
" $global:LASTEXITCODE = 0",
365+
" }",
366+
" Write-Output 'winget output'",
367+
"}",
368+
"function Check-Node { return ($script:wingetCalls.Count -ge 2) }",
369+
"$result = @(Install-Node)",
370+
'if ($result.Count -ne 1 -or $result[0] -ne $true) { throw "Install-Node returned $result" }',
371+
"if ($script:wingetCalls.Count -ne 2) { throw \"Calls=$($script:wingetCalls -join '|')\" }",
372+
"if ($script:wingetCalls[1] -ne 'install OpenJS.NodeJS.LTS --source winget --force --accept-package-agreements --accept-source-agreements') {",
373+
' throw "ForceCall=$($script:wingetCalls[1])"',
374+
"}",
375+
"",
376+
].join("\n"),
377+
},
344378
{
345379
name: "chocolatey-node-upgrade",
346380
source: [
@@ -601,6 +635,7 @@ describe("install.ps1 failure handling", () => {
601635

602636
runIfPowerShell("upgrades and validates Node installed by Windows package managers", () => {
603637
expectBatchedPowerShellCase("winget-node-delayed-path");
638+
expectBatchedPowerShellCase("winget-stale-node-registration");
604639
expectBatchedPowerShellCase("chocolatey-node-upgrade");
605640
expectBatchedPowerShellCase("scoop-node-update");
606641
expectBatchedPowerShellCase("package-manager-node-validation-failure");

0 commit comments

Comments
 (0)