Skip to content

[SourceGen] Report correct location for expression parse errors - #36165

Merged
jfversluis merged 2 commits into
net11.0from
fix-36158-expression-diagnostic-location
Jul 8, 2026
Merged

jfversluis merged 2 commits into
net11.0from
fix-36158-expression-diagnostic-location

Conversation

@StephaneDelcroix

Copy link
Copy Markdown
Contributor

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Fixes #36158

Summary

When a markup / XAML C# Expression fails to parse (e.g. a missing closing }), the source generator reported MAUIG1003: Expression must end with '}' (and the sibling MAUIG1001 parse error) at the file root (1,1) instead of the actual line/column of the offending expression.

Root cause

ExpandMarkupsVisitor.ParseExpression built the diagnostic location from an empty TextSpan / LinePositionSpan in three places (each marked //FIXME fix location), ignoring the xmlLineInfo parameter it already receives. new LinePositionSpan() defaults to line 0 / char 0, which renders as (1,1).

Fix

Derive the location from xmlLineInfo using the existing LocationHelpers.LocationCreate helper (the same helper already used throughout this file and the rest of the SourceGen), via a small CreateExpressionLocation method. It falls back gracefully to the previous behavior when line info or the relative path is unavailable.

The diagnostic now points at the expression's actual line, e.g. MainPage.xaml(33,…) instead of MainPage.xaml(1,1).

Tests

Added Maui36158Tests (in Controls.SourceGen.UnitTests) with two cases:

  • The issue's exact repro — an unclosed XEPR ternary {IsActive ? 'Status: Active' : 'Status: Idle' — now reports MAUIG1003 on the expression's line.
  • An unclosed markup extension {StaticResource MissingBrace — likewise reports on the correct line.

Both tests fail without the fix (reported line 0) and pass with it. The full Controls.SourceGen.UnitTests suite (218 tests) passes with no regressions.

Notes

This addresses only the diagnostic-location bug. The related issues #36157 (stale MAUIG1003 under dotnet watch), #36156 (rude-edit hot reload), and #36155 (XEPR nested-string / ternary parsing) are out of scope.

Copilot AI review requested due to automatic review settings June 26, 2026 13:33
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 36165

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 36165"

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes a SourceGen diagnostic-location bug where expression/markup parse errors (e.g., missing closing }) were incorrectly reported at the file root (1,1) instead of the offending expression’s actual line/column.

Changes:

  • Capture the original markup text and derive diagnostic Location from IXmlLineInfo when reporting MAUIG1003 / MAUIG1001 in ExpandMarkupsVisitor.ParseExpression.
  • Add unit tests validating MAUIG1003 reports on the expression’s line (and not column 0) for both XEPR-style expressions and markup extensions.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Controls/src/SourceGen/Visitors/ExpandMarkupsVisitor.cs Uses line info to create accurate diagnostic locations for expression/markup parse errors.
src/Controls/tests/SourceGen.UnitTests/Maui36158Tests.cs Adds regression tests asserting diagnostics point to the expression’s line/column rather than (1,1).

Fixes #36158

When a markup / XAML C# Expression failed to parse (e.g. a missing
closing '}'), the source generator reported MAUIG1003 / MAUIG1001 at the
file root (1,1) instead of the line/column of the offending expression.

ExpandMarkupsVisitor.ParseExpression built the diagnostic location from
an empty TextSpan / LinePositionSpan (three '//FIXME fix location'
spots), ignoring the xmlLineInfo parameter it already receives. This
derives the location from xmlLineInfo via the existing
LocationHelpers.LocationCreate helper, with a safe fallback when line
info or the relative path is unavailable.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@StephaneDelcroix
StephaneDelcroix force-pushed the fix-36158-expression-diagnostic-location branch from 9c42b2e to bb51712 Compare July 7, 2026 08:34
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jfversluis
jfversluis merged commit c1c0fc7 into net11.0 Jul 8, 2026
31 checks passed
@jfversluis
jfversluis deleted the fix-36158-expression-diagnostic-location branch July 8, 2026 09:06
@github-actions github-actions Bot added this to the .NET 11.0-preview7 milestone Jul 8, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 7, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-xaml XAML, CSS, Triggers, Behaviors

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants