Skip to content

Commit 07277ac

Browse files
committed
(GH-709) Get Exit code from PowerShell Host
When interacting with a PowerShell host, ensure it returns the proper exit code by calling the right method that will set it for both system powershell and the internal host.
1 parent 2272358 commit 07277ac

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/chocolatey.resources/chocolatey.resources.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@
137137
<ItemGroup>
138138
<EmbeddedResource Include="helpers\ChocolateyTabExpansion.ps1" />
139139
</ItemGroup>
140+
<ItemGroup>
141+
<EmbeddedResource Include="helpers\functions\Set-PowerShellExitCode.ps1" />
142+
</ItemGroup>
140143
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
141144
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
142145
Other similar extension points exist, see Microsoft.Common.targets.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright © 2011 - Present RealDimensions Software, LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
#
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
Function Set-PowerShellExitCode {
17+
param (
18+
[int]$exitCode
19+
)
20+
21+
$host.SetShouldExit($exitCode);
22+
$env:ChocolateyExitCode = $exitCode;
23+
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,11 @@ private PowerShellExecutionResults run_host(ChocolateyConfiguration config, stri
564564
case PipelineState.Failed:
565565
case PipelineState.Stopping:
566566
case PipelineState.Stopped:
567-
host.SetShouldExit(1);
567+
if (host.ExitCode == 0) host.SetShouldExit(1);
568568
host.HostException = pipeline.PipelineStateInfo.Reason;
569569
break;
570570
case PipelineState.Completed:
571-
host.SetShouldExit(0);
571+
if (host.ExitCode == -1) host.SetShouldExit(0);
572572
break;
573573
}
574574

0 commit comments

Comments
 (0)