Skip to content

Commit 33b6285

Browse files
committed
(GH-512) Exit with same code as installer
When running native installers, allow choco to exit with the same exit code as the installer. Additionally, detect valid exit codes and do not error when receiving those exit codes. Pass the exit code to the package so that result can be passed back.
1 parent cfa8e2f commit 33b6285

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1

+15-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,18 @@ $ShimGen = Join-Path $chocoTools 'shimgen.exe'
4545
$checksumExe = Join-Path $chocoTools 'checksum.exe'
4646

4747
Write-Debug "Running `'$packageScript`'";
48-
& "$packageScript"
48+
& "$packageScript"
49+
50+
$scriptSuccess = $?
51+
52+
$exitCode = $LASTEXITCODE
53+
if ($exitCode -eq 0 -and -not $scriptSuccess) {
54+
$exitCode = 1
55+
}
56+
57+
if ($env:ChocolateyExitCode -ne $null -and $env:ChocolateyExitCode -ne '') {
58+
$exitCode = $env:ChocolateyExitCode
59+
}
60+
61+
62+
Exit $exitCode

src/chocolatey.resources/helpers/functions/Install-ChocolateyInstallPackage.ps1

+4-4
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,16 @@ param(
9595
$msiArgs = "$msiArgs $silentArgs $additionalInstallArgs";
9696
}
9797

98-
Start-ChocolateyProcessAsAdmin "$msiArgs" 'msiexec' -validExitCodes $validExitCodes
98+
$env:ChocolateyExitCode = Start-ChocolateyProcessAsAdmin "$msiArgs" 'msiexec' -validExitCodes $validExitCodes
9999
#Start-Process -FilePath msiexec -ArgumentList $msiArgs -Wait
100100
}
101101

102102
if ($fileType -like 'exe') {
103103
if ($overrideArguments) {
104-
Start-ChocolateyProcessAsAdmin "$additionalInstallArgs" $file -validExitCodes $validExitCodes
104+
$env:ChocolateyExitCode = Start-ChocolateyProcessAsAdmin "$additionalInstallArgs" $file -validExitCodes $validExitCodes
105105
write-host "Overriding package arguments with `'$additionalInstallArgs`'";
106106
} else {
107-
Start-ChocolateyProcessAsAdmin "$silentArgs $additionalInstallArgs" $file -validExitCodes $validExitCodes
107+
$env:ChocolateyExitCode = Start-ChocolateyProcessAsAdmin "$silentArgs $additionalInstallArgs" $file -validExitCodes $validExitCodes
108108
}
109109
}
110110

@@ -115,7 +115,7 @@ param(
115115
} else {
116116
$msuArgs = "$file $silentArgs $additionalInstallArgs"
117117
}
118-
Start-ChocolateyProcessAsAdmin "$msuArgs" 'wusa.exe' -validExitCodes $validExitCodes
118+
$env:ChocolateyExitCode = Start-ChocolateyProcessAsAdmin "$msuArgs" 'wusa.exe' -validExitCodes $validExitCodes
119119
}
120120

121121
write-host "$packageName has been installed."

src/chocolatey.resources/helpers/functions/Start-ChocolateyProcessAsAdmin.ps1

+3
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,11 @@ Elevating Permissions and running [`"$exeToRun`" $wrappedStatements]. This may t
126126

127127
Write-Debug "Command [`"$exeToRun`" $wrappedStatements] exited with `'$exitCode`'."
128128
if ($validExitCodes -notcontains $exitCode) {
129+
Set-PowerShellExitCode $exitCode
129130
throw "Running [`"$exeToRun`" $statements] was not successful. Exit code was '$exitCode'. See log for possible error messages."
130131
}
131132

132133
Write-Debug "Finishing 'Start-ChocolateyProcessAsAdmin'"
134+
135+
return $exitCode
133136
}

src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ private void ensure_bad_package_path_is_clean(ChocolateyConfiguration config, Pa
875875

876876
private void handle_unsuccessful_operation(ChocolateyConfiguration config, PackageResult packageResult, bool movePackageToFailureLocation, bool attemptRollback)
877877
{
878-
Environment.ExitCode = 1;
878+
if (Environment.ExitCode == 0 ) Environment.ExitCode = 1;
879879

880880
foreach (var message in packageResult.Messages.Where(m => m.MessageType == ResultType.Error))
881881
{

src/chocolatey/infrastructure.app/services/PowershellService.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,27 @@ public bool run_action(ChocolateyConfiguration configuration, PackageResult pack
323323
`choco -h` for details.");
324324
}
325325

326+
326327
if (result.ExitCode != 0)
328+
{
329+
Environment.ExitCode = result.ExitCode;
330+
packageResult.ExitCode = result.ExitCode;
331+
}
332+
333+
// 0 - most widely used success exit code
334+
// MSI valid exit codes
335+
// 1605 - (uninstall) - the product is not found, could have already been uninstalled
336+
// 1614 (uninstall) - the product is uninstalled
337+
// 1641 - restart initiated
338+
// 3010 - restart required
339+
var validExitCodes = new List<int> { 0, 1605, 1614, 1641, 3010 };
340+
if (!validExitCodes.Contains(result.ExitCode))
327341
{
328342
failure = true;
329343
}
330344

331345
if (failure)
332346
{
333-
Environment.ExitCode = result.ExitCode;
334347
packageResult.Messages.Add(new ResultMessage(ResultType.Error, "Error while running '{0}'.{1} See log for details.".format_with(chocoPowerShellScript, Environment.NewLine)));
335348
}
336349
packageResult.Messages.Add(new ResultMessage(ResultType.Note, "Ran '{0}'".format_with(chocoPowerShellScript)));

0 commit comments

Comments
 (0)