From 39c53636f171f64a46236daef0f43a8df7ef471f Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 26 Feb 2021 18:33:05 -0800 Subject: [PATCH 1/2] Update `Concise` ErrorView to not show line information for errors from script modules --- .../PowerShellCore_format_ps1xml.cs | 3 ++- .../engine/Formatting/ErrorView.Tests.ps1 | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index d9763164679..2f99fa9f838 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -1105,7 +1105,8 @@ function Get-ConciseViewPositionMessage { $message = '' $prefix = '' - if ($myinv -and $myinv.ScriptName -or $myinv.ScriptLineNumber -gt 1 -or $err.CategoryInfo.Category -eq 'ParserError') { + # Don't show line information if script module + if (($myinv -and $myinv.ScriptName -or $myinv.ScriptLineNumber -gt 1 -or $err.CategoryInfo.Category -eq 'ParserError') -and !($myinv.ScriptName.EndsWith('.psm1', [System.StringComparison]::InvariantCultureIgnoreCase))) { $useTargetObject = $false # Handle case where there is a TargetObject and we can show the error at the target rather than the script source diff --git a/test/powershell/engine/Formatting/ErrorView.Tests.ps1 b/test/powershell/engine/Formatting/ErrorView.Tests.ps1 index d77f9eb45f7..b94c4b5fa4c 100644 --- a/test/powershell/engine/Formatting/ErrorView.Tests.ps1 +++ b/test/powershell/engine/Formatting/ErrorView.Tests.ps1 @@ -21,6 +21,7 @@ Describe 'Tests for $ErrorView' -Tag CI { Context 'ConciseView tests' { BeforeEach { $testScriptPath = Join-Path -Path $TestDrive -ChildPath 'test.ps1' + $testModulePath = Join-Path -Path $TestDrive -ChildPath 'test.psm1' } AfterEach { @@ -136,6 +137,19 @@ Describe 'Tests for $ErrorView' -Tag CI { $e = & "$PSHOME/pwsh" -noprofile -file $testScriptPath 2>&1 | Out-String $e.Split("o${newline}t").Count | Should -Be 1 -Because "Error message should not contain newline" } + + It "Script module error should not show line information" { + $testModule = @' + function Invoke-Error() { + throw 'oops' + } +'@ + + Set-Content -Path $testModulePath -Value $testModule + $e = & "$PSHOME/pwsh" -noprofile -command "Import-Module '$testModulePath'; Invoke-Error" 2>&1 | Out-String + $e | Should -Not -BeNullOrEmpty + $e | Should -Not -BeLike "*Line*" + } } Context 'NormalView tests' { From 5fbae4a51d7ac3855096f8dae897cbaf8a15f4de Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Sun, 28 Feb 2021 06:22:31 -0800 Subject: [PATCH 2/2] Update src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs Co-authored-by: Ilya --- .../DefaultFormatters/PowerShellCore_format_ps1xml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 2f99fa9f838..8c2a5bb9899 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -1106,7 +1106,7 @@ function Get-ConciseViewPositionMessage { $prefix = '' # Don't show line information if script module - if (($myinv -and $myinv.ScriptName -or $myinv.ScriptLineNumber -gt 1 -or $err.CategoryInfo.Category -eq 'ParserError') -and !($myinv.ScriptName.EndsWith('.psm1', [System.StringComparison]::InvariantCultureIgnoreCase))) { + if (($myinv -and $myinv.ScriptName -or $myinv.ScriptLineNumber -gt 1 -or $err.CategoryInfo.Category -eq 'ParserError') -and !($myinv.ScriptName.EndsWith('.psm1', [System.StringComparison]::OrdinalIgnoreCase))) { $useTargetObject = $false # Handle case where there is a TargetObject and we can show the error at the target rather than the script source