chocolatey / chocolatey/choco

Provide a documented way to query the native installer exit code (Get-PowerShellExitCode)

Open
#1,036 3 comments 0 reactions 0 assignees View on GitHub
0 - Backlog Enhancement Up For Grabs
Dominant language
C#
Stars
11.5k
Forks
960
PR merge metrics
No merged PRs in 30d

Description

There are some cases when I would like to examine the exit code of the native installer after Install-ChocolateyPackage or Install-ChocolateyInstallPackage completes and perform additional actions based on the exit code. Chocolatey provides [a helper to set the exit code](https://github.com/chocolatey/choco/blob/master/src/chocolatey.resources/helpers/functions/Set-PowerShellExitCode.ps1), but does not provide one for querying. The current implementation stores the value in the ChocolateyExitCode environment variable, but AFAIK this is an undocumented implementation detail, so it must not be relied upon.

I suggest providing a new helper, named `Get-PowerShellExitCode` (for symmetry) or `Get-LastExitCode` (more semantically correct, I believe), that would return the exit code:
* returned by the last executed native installer,
* set by Set-PowerShellExitCode,
* or zero if neither of the above has been executed for the given package yet.

(Documenting the existing environment variable would also work, but I strongly advise against it. Environment variables are the worst kind of global variables (polluting even other processes), are untyped and provide no chance of ever adding more complex value retrieval logic - like fields vs properties.)

Contributor guide

Open the contributing guide

Research direction

Start with src/chocolatey.resources/helpers/functions/Set-PowerShellExitCode.ps1 and trace how Install-ChocolateyPackage and Install-ChocolateyInstallPackage store the native installer exit code. Add the documented query helper with the three requested return cases, then verify the helper behavior through the project's relevant tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell
Domain
cli
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.