Skip to content

Commit

Permalink
(GH-1000) Don't check $LASTEXITCODE by default
Browse files Browse the repository at this point in the history
Starting with 33b6285, which was released as part of GH-512 in
0.9.10, we started checking `$LASTEXITCODE` in addition to the script
command success. This meant it offered the ability to capture when a
script exited with `exit 1` and handle that accordingly. However that
was not a recommended scenario for returning errors from scripts.

Checking `$LastExitCode` checks the last executable's exit code when the
script specifically does not call `Exit`. This can lead to very
perplexing failures, such as running a successful xcopy that exits with
2 and seeing package failures without understanding why. Since it is
not typically recommended to call `Exit` to return a value from
PowerShell because of issues with different hosts, it's less of a
concern to only look at explicit failures.
  • Loading branch information
ferventcoder committed Oct 5, 2016
1 parent c372fa8 commit 5f62487
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,22 @@ $checksumExe = Join-Path $chocoTools 'checksum.exe'

Write-Debug "Running `'$packageScript`'";
& "$packageScript"

$scriptSuccess = $?
$lastExecutableExitCode = $LASTEXITCODE

if ($lastExecutableExitCode -ne $null -and $lastExecutableExitCode -ne '') {
Write-Debug "The last executable that ran had an exit code of '$lastExecutableExitCode'."
}

if (-not $scriptSuccess) {
Write-Debug "The script exited with a failure."
}

$exitCode = 0
if ($env:ChocolateyCheckLastExitCode -ne $null -and $env:ChocolateyCheckLastExitCode -eq 'true' -and $lastExecutableExitCode -ne $null -and $lastExecutableExitCode -ne '') {
$exitCode = $lastExecutableExitCode
}

$exitCode = $LASTEXITCODE
if ($exitCode -eq 0 -and -not $scriptSuccess) {
$exitCode = 1
}
Expand Down

0 comments on commit 5f62487

Please sign in to comment.