choco icon indicating copy to clipboard operation
choco copied to clipboard

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

Open jberezanski opened this issue 9 years ago • 3 comments

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, 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.)

jberezanski avatar Nov 02 '16 23:11 jberezanski

Besides, $Env:ChocolateyExitCode is untrustworthy since 35585c3d.

jberezanski avatar Feb 04 '17 21:02 jberezanski

I like this.

Environment variables are the worst kind of global variables (polluting even other processes)

This is untrue. Only persistent env vars would pollute other processes. You still need a way to store state.

ferventcoder avatar Mar 19 '17 10:03 ferventcoder

Environment variables are the worst kind of global variables (polluting even other processes)

This is untrue. Only persistent env vars would pollute other processes.

Process environment is inherited by child processes by default, so env variables set by the current process (choco.exe) pollute the environment of all processes started by choco.exe (such as native installers).

You still need a way to store state.

A PowerShell variable would work much better here - it could be strongly typed and scoped to the chocolateyInstaller module, so not even visible by the package scripts (not to mention child processes).

jberezanski avatar Nov 09 '18 19:11 jberezanski