Install missing packages when scoped upgrades find no match

This commit is contained in:
Dandaedre 2026-10-19 13:45:38 +02:00
parent b1d6b0eeee
commit 348d8d322c
3 changed files with 59 additions and 1 deletions

View File

@ -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.

View File

@ -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 {

View File

@ -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
}
}
}