From 5f624876ca966a35118162c0b660e5000e4307a6 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Wed, 5 Oct 2016 08:15:23 -0500 Subject: [PATCH] (GH-1000) Don't check $LASTEXITCODE by default Starting with 33b628525c, 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. --- .../helpers/chocolateyScriptRunner.ps1 | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1 b/src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1 index cbb96c7e21..a27e771fb3 100644 --- a/src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1 +++ b/src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1 @@ -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 }