Skip to content

Commit b94dd91

Browse files
authored
Specify exe path as TargetObect for non-zero exit code ErrorRecord (PowerShell#16108)
1 parent 9a63f41 commit b94dd91

2 files changed

Lines changed: 6 additions & 5 deletions

File tree

src/System.Management.Automation/engine/NativeCommandProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ internal override void Complete()
884884
errorMsg,
885885
errorId);
886886

887-
var errorRecord = new ErrorRecord(exception, errorId, ErrorCategory.NotSpecified, targetObject: NativeCommandName);
887+
var errorRecord = new ErrorRecord(exception, errorId, ErrorCategory.NotSpecified, targetObject: Path);
888888
this.commandRuntime._WriteErrorSkipAllowCheck(errorRecord);
889889
}
890890
}

test/powershell/engine/Basic/NativeCommandErrorHandling.Tests.ps1

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Describe 'Native command error handling tests' -Tags 'CI' {
1414
}
1515

1616
$exeName = $IsWindows ? 'testexe.exe' : 'testexe'
17+
$exePath = @(Get-Command $exeName -Type Application)[0].Path
1718

1819
$errorActionPrefTestCases = @(
1920
@{ ErrorActionPref = 'Stop' }
@@ -43,7 +44,7 @@ Describe 'Native command error handling tests' -Tags 'CI' {
4344

4445
$error.Count | Should -Be 1
4546
$error[0].FullyQualifiedErrorId | Should -BeExactly 'ProgramExitedWithNonZeroCode'
46-
$error[0].TargetObject | Should -BeExactly $exeName
47+
$error[0].TargetObject | Should -BeExactly $exePath
4748
}
4849

4950
It 'Non-zero exit code outputs a non-teminating error for $ErrorActionPreference = ''Continue''' {
@@ -52,7 +53,7 @@ Describe 'Native command error handling tests' -Tags 'CI' {
5253
$stderr = testexe -returncode 1 2>&1
5354

5455
$error[0].FullyQualifiedErrorId | Should -BeExactly 'ProgramExitedWithNonZeroCode'
55-
$error[0].TargetObject | Should -BeExactly $exeName
56+
$error[0].TargetObject | Should -BeExactly $exePath
5657
$stderr[1].Exception.Message | Should -BeExactly "Program `"$exeName`" ended with non-zero exit code: 1."
5758
}
5859

@@ -65,7 +66,7 @@ Describe 'Native command error handling tests' -Tags 'CI' {
6566
$stderr = testexe -returncode 1 2>&1
6667

6768
$error[0].FullyQualifiedErrorId | Should -BeExactly 'ProgramExitedWithNonZeroCode'
68-
$error[0].TargetObject | Should -BeExactly $exeName
69+
$error[0].TargetObject | Should -BeExactly $exePath
6970
$stderr[1].Exception.Message | Should -BeExactly "Program `"$exeName`" ended with non-zero exit code: 1."
7071
}
7172

@@ -76,7 +77,7 @@ Describe 'Native command error handling tests' -Tags 'CI' {
7677

7778
$error.Count | Should -Be 1
7879
$error[0].FullyQualifiedErrorId | Should -BeExactly 'ProgramExitedWithNonZeroCode'
79-
$error[0].TargetObject | Should -BeExactly $exeName
80+
$error[0].TargetObject | Should -BeExactly $exePath
8081
}
8182

8283
It 'Non-zero exit code does not generates an error record for $ErrorActionPreference = ''Ignore''' {

0 commit comments

Comments
 (0)