diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index 371e1ff00ff..714bf8f2d4f 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -133,7 +133,7 @@ internal ProcessOutputObject(object data, MinishellStream stream) } } - #nullable enable +#nullable enable /// /// This exception is used by the NativeCommandProcessor to indicate an error /// when a native command returns a non-zero exit code. @@ -187,7 +187,7 @@ internal NativeCommandExitException(string path, int exitCode, int processId, st public int ProcessId { get; } } - #nullable restore +#nullable restore /// /// Provides way to create and execute native commands. @@ -972,11 +972,17 @@ internal override void Complete() } const string errorId = nameof(CommandBaseStrings.ProgramExitedWithNonZeroCode); +#if UNIX + string hexFormatStr = "0x{0:X2}"; +#else + string hexFormatStr = "0x{0:X8}"; +#endif string errorMsg = StringUtil.Format( CommandBaseStrings.ProgramExitedWithNonZeroCode, NativeCommandName, - _nativeProcess.ExitCode); + _nativeProcess.ExitCode, + string.Format(CultureInfo.InvariantCulture, hexFormatStr, _nativeProcess.ExitCode)); var exception = new NativeCommandExitException( Path, diff --git a/src/System.Management.Automation/resources/CommandBaseStrings.resx b/src/System.Management.Automation/resources/CommandBaseStrings.resx index 0ff1aac4716..e869e98e742 100644 --- a/src/System.Management.Automation/resources/CommandBaseStrings.resx +++ b/src/System.Management.Automation/resources/CommandBaseStrings.resx @@ -163,7 +163,7 @@ however that is used for ApplicationFailedExceptions thrown when the NativeCommandProcessor fails in an unexpected way. In this case, we have a more specific error for the native command scenario, so the two are not conflated. --> - Program "{0}" ended with non-zero exit code: {1}. + Program "{0}" ended with non-zero exit code: {1} ({2}). Performing the operation "{0}" on target "{1}". diff --git a/test/powershell/engine/Basic/NativeCommandErrorHandling.Tests.ps1 b/test/powershell/engine/Basic/NativeCommandErrorHandling.Tests.ps1 index 8122e598d2c..62696ec1692 100644 --- a/test/powershell/engine/Basic/NativeCommandErrorHandling.Tests.ps1 +++ b/test/powershell/engine/Basic/NativeCommandErrorHandling.Tests.ps1 @@ -48,7 +48,7 @@ Describe 'Native command error handling tests' -Tags 'CI' { $error[0].FullyQualifiedErrorId | Should -BeExactly 'ProgramExitedWithNonZeroCode' $error[0].TargetObject | Should -BeExactly $exePath - $stderr[1].Exception.Message | Should -BeExactly "Program `"$exeName`" ended with non-zero exit code: 1." + $stderr[1].Exception.Message | Should -BeExactly ("Program `"$exeName`" ended with non-zero exit code: 1 ({0})." -f ($IsWindows ? '0x00000001' : '0x01')) } It "Non-boolean value should not cause type casting error when the native command exited with non-zero code" { @@ -61,7 +61,7 @@ Describe 'Native command error handling tests' -Tags 'CI' { $error[0].FullyQualifiedErrorId | Should -BeExactly 'ProgramExitedWithNonZeroCode' $error[0].TargetObject | Should -BeExactly $exePath - $stderr[1].Exception.Message | Should -BeExactly "Program `"$exeName`" ended with non-zero exit code: 1." + $stderr[1].Exception.Message | Should -BeExactly ("Program `"$exeName`" ended with non-zero exit code: 1 ({0})." -f ($IsWindows ? '0x00000001' : '0x01')) } It 'Non-zero exit code generates a non-teminating error for $ErrorActionPreference = ''SilentlyContinue''' {