winget-cli icon indicating copy to clipboard operation
winget-cli copied to clipboard

`Update-WinGetPackage` does not throw on error and `$?` = `True`

Open o-l-a-v opened this issue 1 year ago • 1 comments

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

  1. Should by default throw if it does not succeed.
  2. Should set $LASTEXITCODE, like winget upgrade --silent --id GitHub.GitHubDesktop does
  3. Should add error/exception to $Error variable

Actual behavior

  1. Does not throw
  2. Does not set $LASTEXITCODE
  3. Does not add error to $Error variable

Environment

PowerShell v7.4.4 x64
Microsoft.WinGet.Client v1.8.1911
WinGet v1.8.1911

o-l-a-v avatar Aug 02 '24 10:08 o-l-a-v

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 -ErrorVariable common 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.

mklement0 avatar Aug 03 '24 21:08 mklement0