From 348d8d322c5a7bc015c6da794d7be6cf9d99aa7a Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Mon, 19 Oct 2026 13:45:38 +0200 Subject: [PATCH] Install missing packages when scoped upgrades find no match --- TASKS.md | 1 + src/Packages.psm1 | 11 ++++++++- tests/Packages.Tests.ps1 | 48 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/TASKS.md b/TASKS.md index e6a3a77..efc3451 100644 --- a/TASKS.md +++ b/TASKS.md @@ -125,3 +125,4 @@ open until it is run on disposable machines with the required account setups. - [x] Preserve signed WinGet HRESULT bits without UInt32 overflow and test diagnostic capture. - [x] Rename the bundled default machine to beffroi-laptop and document switching existing setups. - [x] Use PowerShell 7-compatible ACL application for elevation requests; add Windows-only permission regression tests (Windows execution pending). +- [x] Retry an upgrade with an install when WinGet reports no matching installed package, preserving scope and testing bounded retries. diff --git a/src/Packages.psm1 b/src/Packages.psm1 index 1b67918..2c9f59f 100644 --- a/src/Packages.psm1 +++ b/src/Packages.psm1 @@ -1,5 +1,6 @@ Set-StrictMode -Version Latest +$script:WingetNoApplicationsFound = [Convert]::ToUInt32('8A150014', 16) $script:WingetNoApplicableUpgrade = [Convert]::ToUInt32('8A15002B', 16) function Test-VersionGreaterThan { @@ -96,6 +97,14 @@ function Invoke-PackageOperation { if ($package.PSObject.Properties['scope']) { $arguments += @('--scope', $package.scope) } if ($package.PSObject.Properties['version']) { $arguments += @('--version', $package.version) } $result = Invoke-WingetCapture -Arguments $arguments -LogPath $LogPath + # Inventory is collected before execution and does not identify installer scope. + # A package can disappear or be absent in the elevated worker's scope. + $installedAfterMissingUpgrade = $false + if ($verb -eq 'upgrade' -and $result.unsignedExitCode -eq $script:WingetNoApplicationsFound) { + $arguments[0] = 'install' + $result = Invoke-WingetCapture -Arguments $arguments -LogPath $LogPath + $installedAfterMissingUpgrade = $true + } if ($result.unsignedExitCode -eq $script:WingetNoApplicableUpgrade) { return [pscustomobject]@{ name = $Operation.name; status = 'Unchanged'; exitCode = 0; rebootRequired = $false; message = 'No applicable upgrade' } } @@ -105,7 +114,7 @@ function Invoke-PackageOperation { if ($result.exitCode -ne 0) { return [pscustomobject]@{ name = $Operation.name; status = 'Failed'; exitCode = $result.unsignedExitCode; rebootRequired = $false; message = $result.output } } - [pscustomobject]@{ name = $Operation.name; status = 'Succeeded'; exitCode = 0; rebootRequired = $false; message = "$($Operation.action) completed" } + [pscustomobject]@{ name = $Operation.name; status = 'Succeeded'; exitCode = 0; rebootRequired = $false; message = $(if ($installedAfterMissingUpgrade) { 'Installed after upgrade found no matching installation' } else { "$($Operation.action) completed" }) } } function Invoke-PackagePlan { diff --git a/tests/Packages.Tests.ps1 b/tests/Packages.Tests.ps1 index a24ccdd..a19f63d 100644 --- a/tests/Packages.Tests.ps1 +++ b/tests/Packages.Tests.ps1 @@ -67,3 +67,51 @@ Describe 'WinGet signed process exit codes' { } } } + +Describe 'Missing installation during upgrade' { + It 'installs once with the original scope and package constraints' { + InModuleScope Packages { + Mock Invoke-WingetCapture { + if ($Arguments[0] -eq 'upgrade') { + return [pscustomobject]@{ exitCode = -1978335212; unsignedExitCode = 2316632084; output = 'Not installed' } + } + [pscustomobject]@{ exitCode = 0; unsignedExitCode = 0; output = 'Installed' } + } + $operation = [pscustomobject]@{ + action = 'Upgrade'; name = 'Helix.Helix' + package = [pscustomobject]@{ id = 'Helix.Helix'; source = 'winget'; scope = 'machine' } + } + $result = Invoke-PackageOperation -Operation $operation + $result.status | Should -Be 'Succeeded' + $result.message | Should -Match 'Installed after upgrade' + Should -Invoke Invoke-WingetCapture -Times 2 -Exactly + Should -Invoke Invoke-WingetCapture -Times 1 -Exactly -ParameterFilter { + $Arguments[0] -eq 'install' -and $Arguments -contains 'Helix.Helix' -and + $Arguments -contains '--scope' -and $Arguments -contains 'machine' -and + $Arguments -contains '--source' -and $Arguments -contains 'winget' + } + } + } + + It 'reports a failed fallback without retrying again' { + InModuleScope Packages { + Mock Invoke-WingetCapture { + [pscustomobject]@{ exitCode = -1978335212; unsignedExitCode = 2316632084; output = 'Still missing' } + } + $operation = [pscustomobject]@{ action = 'Upgrade'; name = 'Test.App'; package = [pscustomobject]@{ id = 'Test.App'; source = 'winget' } } + $result = Invoke-PackageOperation -Operation $operation + $result.status | Should -Be 'Failed' + $result.message | Should -Be 'Still missing' + Should -Invoke Invoke-WingetCapture -Times 2 -Exactly + } + } + + It 'does not retry unrelated upgrade failures as installs' { + InModuleScope Packages { + Mock Invoke-WingetCapture { [pscustomobject]@{ exitCode = 1; unsignedExitCode = 1; output = 'Other failure' } } + $operation = [pscustomobject]@{ action = 'Upgrade'; name = 'Test.App'; package = [pscustomobject]@{ id = 'Test.App'; source = 'winget' } } + (Invoke-PackageOperation -Operation $operation).status | Should -Be 'Failed' + Should -Invoke Invoke-WingetCapture -Times 1 -Exactly + } + } +}