Skip to content

Conversation

@jborean93
Copy link
Collaborator

@jborean93 jborean93 commented Dec 31, 2025

PR Summary

Add support for specifying the column position of a parser error when using the TargetObject IDictionary value. This allows PowerShell to provide a nicely formatted error message that shows the exact position in the line provided where the error occurs.

PR Context

The change made in #19239 made it impossible for cmdlets to emit an ErrorRecord that PowerShell will display with the parsing extent information. A cmdlet cannot throw ParentContainsErrorRecordException which means the checks to use the custom parsing formatting is never used. This results in an ErrorRecord with the ParsingError to be seen as the below in the default $ErrorView.

Some-Cmdlet: Error message here

There exists code right now to use the TargetObject to provide a way to produce a more detailed error with the line and script information but doesn't have a way to provide the line position message so it looks just like

Some-Cmdlet: 
Line |
   1 | Some-Cmdlet -Target { param() } -Hook {}
     | Error message here

This PR extends the TargetObject functionality to also support StartColumn and EndColumn which allows the error author to provide enough information to give the line position where the failure actually is.

Some-Cmdlet:
Line |
   1 | Some-Cmdlet -Target { param() } -Hook {}
     |                       ~~~~~~~
     | Error message here

The other benefit is that on consoles that support VT sequences, the problematic position is highlighted

image

There should be no regression here as the StartColumn is only used if present and in a valid format.

PR Checklist

Add support for specifying the column position of a parser error when
using the TargetObject IDictionary value. This allows PowerShell to
provide a nicely formatted error message that shows the exact position
in the line provided where the error occurs.
Copilot AI review requested due to automatic review settings December 31, 2025 01:04
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enhances PowerShell's error formatting by adding support for specifying column positions (StartColumn and EndColumn) when using the TargetObject IDictionary approach for parser errors. This allows cmdlets to emit error records with precise column highlighting, similar to native PowerShell parser errors, even though cmdlets cannot throw ParentContainsErrorRecordException.

Key changes:

  • Adds optional StartColumn and EndColumn support to the TargetObject error formatting
  • Provides visual highlighting of the error position using tildes (~) in the error output
  • Includes comprehensive validation and fallback behavior for edge cases

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs Implements logic to read StartColumn/EndColumn from TargetObject, validate them, and generate position highlighting with tildes
test/powershell/engine/Formatting/ErrorView.Tests.ps1 Adds 11 comprehensive test cases covering normal operation, edge cases, type conversion, and invalid input scenarios

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 1192 to +1223
$line = $_.TargetObject.LineText.Trim()
$offsetLength = 0
$offsetInLine = 0
$startColumn = 0
if (
([System.Collections.IDictionary]$_.TargetObject).Contains('StartColumn') -and
[System.Management.Automation.LanguagePrimitives]::TryConvertTo[int]($_.TargetObject.StartColumn, [ref]$startColumn) -and
$null -ne $startColumn -and
$startColumn -gt 0 -and
$startColumn -le $line.Length
) {
$endColumn = 0
if (-not (
([System.Collections.IDictionary]$_.TargetObject).Contains('EndColumn') -and
[System.Management.Automation.LanguagePrimitives]::TryConvertTo[int]($_.TargetObject.EndColumn, [ref]$endColumn) -and
$null -ne $endColumn -and
$endColumn -gt $startColumn -and
$endColumn -le ($line.Length + 1)
)) {
$endColumn = $line.Length + 1
}
# Input is expected to be 1-based index to match the extent positioning
# but we use 0-based indexing below.
$startColumn -= 1
$endColumn -= 1
$highlightLine = "$(" " * $startColumn)$("~" * ($endColumn - $startColumn))"
$offsetLength = $endColumn - $startColumn
$offsetInLine = $startColumn
}
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The LineText is trimmed before validating and using StartColumn/EndColumn positions. If a caller provides LineText with leading or trailing whitespace and specifies column positions based on the original untrimmed text, the positions will be misaligned after trimming.

For example, if LineText is " abc def" (with 2 leading spaces) and StartColumn is 3 (pointing to 'a' in the original text), after trimming the line becomes "abc def" and position 3 would point to 'c' instead.

This should either be documented that LineText should not have leading/trailing whitespace when using StartColumn/EndColumn, or the code should skip trimming when column information is provided.

Copilot uses AI. Check for mistakes.
@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Dec 31, 2025
@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Jan 7, 2026
@microsoft-github-policy-service
Copy link
Contributor

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Review - Needed The PR is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants