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 3fdff3e13c..09cc323b93 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -1151,12 +1151,10 @@ namespace System.Management.Automation.Runspaces # replace newlines in message so it lines up correct $message = $message.Replace($newline, ' ').Replace(""`t"", ' ') - try { - $windowWidth = [Console]::WindowWidth - } - catch { - # fails if there is no console - $windowWidth = 120 + + $windowWidth = 120 + if ($Host.UI.RawUI -ne $null) { + $windowWidth = $Host.UI.RawUI.WindowSize.Width } if ($windowWidth -gt 0 -and ($message.Length - $prefixVTLength) -gt $windowWidth) { @@ -1168,9 +1166,17 @@ namespace System.Management.Automation.Runspaces while (($remainingMessage.Length + $prefixLength) -gt $windowWidth) { $subMessage = $prefix + $remainingMessage $substring = Get-TruncatedString -string $subMessage -length ($windowWidth + $prefixVtLength) - $null = $sb.Append($substring) - $null = $sb.Append($newline) - $remainingMessage = $remainingMessage.Substring($substring.Length - $prefix.Length).Trim() + + if ($substring.Length - $prefix.Length -gt 0) + { + $null = $sb.Append($substring) + $null = $sb.Append($newline) + $remainingMessage = $remainingMessage.Substring($substring.Length - $prefix.Length).Trim() + } + else + { + break + } } $null = $sb.Append($prefix + $remainingMessage.Trim()) $message = $sb.ToString() diff --git a/test/powershell/engine/Formatting/ErrorView.Tests.ps1 b/test/powershell/engine/Formatting/ErrorView.Tests.ps1 index 10e8b5136c..112d45fc17 100644 --- a/test/powershell/engine/Formatting/ErrorView.Tests.ps1 +++ b/test/powershell/engine/Formatting/ErrorView.Tests.ps1 @@ -87,6 +87,18 @@ Describe 'Tests for $ErrorView' -Tag CI { # validate line number is shown $e | Should -BeLike '* 2 *' } + + It "Long exception message gets rendered" { + + $msg = "1234567890" + while ($msg.Length -le $Host.UI.RawUI.WindowSize.Width) + { + $msg += $msg + } + + $e = { throw "$msg" } | Should -Throw $msg -PassThru | Out-String + $e | Should -BeLike "*$msg*" + } } Context 'NormalView tests' {