Fix Set-PSDebug -Trace to display all lines of multiline commands #26537
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Summary
Fix
Set-PSDebug -Traceto display all lines of multiline commands.PR Context
Fixes #8113
When using
Set-PSDebug -Trace 1or-Trace 2, multiline commands (e.g., using backtick line continuation) only showed the first line in debug output. This made it difficult to trace and debug scripts with multiline commands.Before this fix:
Output:
After this fix:
Output:
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerDescription
Root Cause
The
TraceLinemethod in the debugger was callingPositionUtilities.BriefMessage(extent.StartScriptPosition), which only considers the start position of an extent. For multiline commands, this meant only the first line was displayed.Solution
Added a new overload
PositionUtilities.BriefMessage(IScriptExtent extent)that handles multiline extents by:Changed
TraceLineto call the new extent-based overload and write each line separately so each gets theDEBUG:prefix.Files Changed
src/System.Management.Automation/engine/debugger/debugger.cs- ChangedTraceLineto use extent-basedBriefMessageand write each line separatelysrc/System.Management.Automation/engine/parser/Position.cs- Added newBriefMessage(IScriptExtent)overloadtest/powershell/Modules/Microsoft.PowerShell.Core/Set-PSDebug.Tests.ps1- Added tests for multiline command tracing with-Trace 1and-Trace 2Testing
Test Coverage
Added test cases that:
Write-Output) and continuation line (bar") appear in debug output withDEBUG:prefixTests cover both
-Trace 1and-Trace 2options.Automated Tests
All tests pass: