Skip to content

Commit 5f62487

Browse files
committed
(GH-1000) Don't check $LASTEXITCODE by default
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.
1 parent c372fa8 commit 5f62487

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1

+14-2
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,22 @@ $checksumExe = Join-Path $chocoTools 'checksum.exe'
4646

4747
Write-Debug "Running `'$packageScript`'";
4848
& "$packageScript"
49-
5049
$scriptSuccess = $?
50+
$lastExecutableExitCode = $LASTEXITCODE
51+
52+
if ($lastExecutableExitCode -ne $null -and $lastExecutableExitCode -ne '') {
53+
Write-Debug "The last executable that ran had an exit code of '$lastExecutableExitCode'."
54+
}
55+
56+
if (-not $scriptSuccess) {
57+
Write-Debug "The script exited with a failure."
58+
}
59+
60+
$exitCode = 0
61+
if ($env:ChocolateyCheckLastExitCode -ne $null -and $env:ChocolateyCheckLastExitCode -eq 'true' -and $lastExecutableExitCode -ne $null -and $lastExecutableExitCode -ne '') {
62+
$exitCode = $lastExecutableExitCode
63+
}
5164

52-
$exitCode = $LASTEXITCODE
5365
if ($exitCode -eq 0 -and -not $scriptSuccess) {
5466
$exitCode = 1
5567
}

0 commit comments

Comments
 (0)