Skip to content

Fix route document highlights across partial-class files - #66770

Merged
Youssef1313 merged 6 commits into
mainfrom
copilot/fix-document-highlights-issue
Jul 1, 2026
Merged

Youssef1313 merged 6 commits into
mainfrom
copilot/fix-document-highlights-issue

Conversation

Copilot AI commented May 20, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #64398

In short, the highlighting can only work in the given document. Once we reach a different syntax tree, we shouldn't do anything. We shouldn't also attempt to get the semantic model for the other tree.

Copilot AI linked an issue May 20, 2026 that may be closed by this pull request
1 task done
Copilot AI changed the title [WIP] Fix document highlights internal error in VS2026 Fix route document highlights across partial-class files May 20, 2026
Copilot AI requested a review from danroth27 May 20, 2026 23:18
@github-actions github-actions Bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label May 20, 2026
AspNetCoreDocumentHighlights cannot carry a Document reference, so every
highlight span the route highlighter emits must belong to the active
document. Spans from other syntax trees would be mis-mapped into the
active document and highlight unrelated text.

- Skip parameter/method DeclaringSyntaxReferences whose SyntaxTree
  differs from semanticModel.SyntaxTree. This obsoletes the per-tree
  semantic model branching introduced earlier in this PR.
- Tighten the cross-file regression test to assert exactly the route
  literal span is highlighted (no stray spans from the handler file).
- Rename the test to describe the desired behavior.

Co-authored-by: Copilot <[email protected]>
@danroth27

Copy link
Copy Markdown
Member

Pushed a follow-up commit (14bb9c2) that addresses the latent correctness issue and tightens the test.

Why a follow-up was needed

AspNetCoreDocumentHighlights cannot carry a Document reference (see Roslyn source), so every span returned by RoutePatternHighlighter.GetDocumentHighlights must belong to the active document (the tree that owns semanticModel). The previous diff fixed the GetSymbolInfo crash but still emitted spans for:

  • parameterSyntax.Identifier.Span from the parameter declaration in the handler file
  • reference.Identifier.Span from identifier references inside the handler method body

Both spans are TextSpans in the handler file's tree. Reporting them as highlights in the route file would mis-highlight whatever text happens to sit at those offsets in the route file (or be out of range). The crash got masked behind the bogus highlights.

Change

In HighlightSymbol, skip DeclaringSyntaxReferences whose SyntaxTree != semanticModel.SyntaxTree in both loops. This also obsoletes the methodSemanticModel per-tree branching introduced earlier in this PR — once we filter to same-tree only, the original semanticModel is always correct.

Trade-off: the parameter highlight in the other file is not surfaced. That's a feature gap rather than a bug — the API simply has no way to express cross-document highlights from an embedded language highlighter. The route literal span is still highlighted, and there is no crash. A complete cross-file highlighting experience would require extending Roslyn's embedded-language highlighter contract; out of scope here.

Test

  • Renamed …_DoesNotThrow → …_HighlightsOnlyRouteLiteral to describe the desired behavior.
  • Asserts the result is exactly {routeSpan} rather than Assert.Contains(... routeSpan). The previous assertion would have passed even if a stray (incorrect) span from the handler file leaked into the result. The stricter assertion would have caught the latent bug.

@Youssef1313 Youssef1313 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The fix sounds correct.

The highlighter API mostly return a list of text spans. A text span doesn't carry info about the document or syntax tree, so Roslyn will assume all returned text spans belong to the original given document/tree.

@Youssef1313
Youssef1313 marked this pull request as ready for review June 29, 2026 10:49
Copilot AI review requested due to automatic review settings June 29, 2026 10:49

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 Roslyn document-highlighting crash in the Minimal API route-pattern highlighter when a route mapping and its handler method are split across different files in the same partial class (cross-syntax-tree scenario).

Changes:

  • Prevents cross-tree symbol highlighting work from producing invalid highlight spans (and avoids the prior Syntax node is not within syntax tree failure mode).
  • Adds a regression test that exercises MapPost(..., LoadAsync) where the mapping and handler live in separate partial-class files.

Reviewed changes

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

File Description
src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteEmbeddedLanguage/RoutePatternHighlighter.cs Skips cross-syntax-tree declarations so highlight spans remain valid for the active document and avoids cross-tree semantic lookups.
src/Framework/AspNetCoreAnalyzers/test/RouteEmbeddedLanguage/RoutePatternHighlighterTests.cs Adds a regression test covering the partial-class-across-files scenario to ensure highlights are produced without exceptions.

Comment on lines 84 to +88
foreach (var item in matchingParameter.DeclaringSyntaxReferences)
{
var syntaxNode = item.GetSyntax(cancellationToken);
if (syntaxNode.SyntaxTree != semanticModel.SyntaxTree)
{
Comment on lines 100 to +104
foreach (var item in methodSymbol.DeclaringSyntaxReferences)
{
var methodSyntax = item.GetSyntax(cancellationToken);
if (methodSyntax.SyntaxTree != semanticModel.SyntaxTree)
{
Comment on lines +74 to +78
// AspNetCoreDocumentHighlights cannot carry a Document reference, so
// every highlight span we emit must belong to the active document
// (the tree that owns `semanticModel`). Spans from other syntax trees
// would be mis-mapped into the active document and highlight unrelated
// text. Skip cross-file declarations entirely - users will not see the
@Youssef1313

Copy link
Copy Markdown
Member

/ba-g Quarantined check timed out.

@Youssef1313
Youssef1313 enabled auto-merge (squash) June 29, 2026 16:14
@Youssef1313
Youssef1313 merged commit 424ca14 into main Jul 1, 2026
25 checks passed
@Youssef1313
Youssef1313 deleted the copilot/fix-document-highlights-issue branch July 1, 2026 11:02
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document highlights: Syntax node is not within syntax tree

6 participants