The default output of script-terminating errors that appears in the integrated console of VSCode's PowerShell extension, is not very helpful for debugging. It only shows the error message and the line where the error originated from (in case of module functions, not even the actual line, but just the caller's location).
What I actually want to see is a full script stacktrace in addition to the error message, without having to add exception-handling code to every script.
Example:
Function Test-Me {
Test-You
}
Function Test-You {
throw 'foo error'
}
Test-Me
Actual output:
Exception: C:\Test.ps1:6:2
Line |
6 | throw 'foo error'
| ~~~~~~~~~~~~~~~~~
| foo error
Desired output:
Exception: C:\Test.ps1:6:2
Line |
6 | throw 'foo error'
| ~~~~~~~~~~~~~~~~~
| foo error
at Test-You, C:\Test.ps1: line 6
at Test-Me, C:\Test.ps1: line 2
at <ScriptBlock>, C:\Test.ps1: line 10
at <ScriptBlock>, <No file>: line 1
While an output like this can be trivially achieved by exception-handling code as follows, it would be too much hassle to add this boilerplate code to each small code snippet I need to debug.
try {
Test-Me
}
catch {
$_ | Out-Host
Write-Debug "`n$($_.ScriptStackTrace)" -Debug
}
While there is a simple way to (also) show a script stacktrace, it causes an error output that is too messy, showing way more information than needed:
# At the top of the script
$ErrorView = 'DetailedView'
Is there a way to configure VSCode's PowerShell extension to produce the desired output?
A simple solution is to add a "launch.json" file. This works well for me, because I keep all my small snippets within a single folder.
script
property to wrap the script call in an exception handler. My customized launch.json looks like this:After following these steps, every script launched via F5, produces the desired output when a script-terminating error occurs. You can even Ctrl+click on a line of the stacktrace to directly jump to the referred code location.