Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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]::OrdinalIgnoreCase))) {
$useTargetObject = $false

# Handle case where there is a TargetObject and we can show the error at the target rather than the script source
Expand Down
14 changes: 14 additions & 0 deletions test/powershell/engine/Formatting/ErrorView.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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' {
Expand Down