`Update-WinGetPackage` does not throw on error and `$?` = `True`
Brief description of your issue
Update-WinGetPackage does not throw if it fails, and in my example it does not set $LASTEXITCODE to -2145844844 like winget upgrade does.
Steps to reproduce
PS > Microsoft.WinGet.Client\Update-WinGetPackage -Mode Silent -Id GitHub.GitHubDesktop | Format-List
Id : GitHub.GitHubDesktop
Name : GitHub Desktop
Source : winget
CorrelationData :
InstallerErrorCode : 0
ExtendedErrorCode : System.Runtime.InteropServices.COMException (0x80190194)
RebootRequired : False
Status : DownloadError
PS > '$? = {0}, $LASTEXITCODE = {1}' -f $?.ToString(), $LASTEXITCODE
$? = True, $LASTEXITCODE = 0
PS >
Correct/expected behavior, using winget upgrade
PS > winget upgrade --silent --id GitHub.GitHubDesktop
Found GitHub Desktop [GitHub.GitHubDesktop] Version 3.4.3
This application is licensed to you by its owner.
Microsoft is not responsible for, nor does it grant any licenses to, third-party packages.
Downloading https://desktop.githubusercontent.com/github-desktop/releases/3.4.3-2170ce9b/GitHubDesktopSetup-x64.exe
An unexpected error occurred while executing the command:
Download request status is not success.
0x80190194 : unknown error
PS > '$? = {0}, $LASTEXITCODE = {1}' -f $?.ToString(), $LASTEXITCODE
$? = False, $LASTEXITCODE = -2145844844
PS >
Expected behavior
- Should by default throw if it does not succeed.
- Should set $LASTEXITCODE, like
winget upgrade --silent --id GitHub.GitHubDesktopdoes - Should add error/exception to
$Errorvariable
Actual behavior
- Does not throw
- Does not set $LASTEXITCODE
- Does not add error to
$Errorvariable
Environment
PowerShell v7.4.4 x64
Microsoft.WinGet.Client v1.8.1911
WinGet v1.8.1911
Note that a (process) exit code does not apply to PowerShell-native commands, so you should never expect $LASTEXITCODE to be set by them (with the exception of *.ps1 scripts that explicitly use exit <n>).
However, it is reasonable to expect an error to be written to PowerShell's error stream (and that error can incorporate an error code):
-
Given that multiple packages can be targeted, any errors should be non-terminating errors.
-
These errors can be examined after the fact via
$Error, and/or captured via the-ErrorVariablecommon parameter.
Either way, by virtue of writing a(t least one) non-terminating error to the error stream, $? would then invariably reflect $false, as intended.