Repository navigation
Fix route document highlights across partial-class files - #66770
Conversation
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]>
|
Pushed a follow-up commit (14bb9c2) that addresses the latent correctness issue and tightens the test. Why a follow-up was needed
Both spans are Change In 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
|
Youssef1313
left a comment
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 treefailure 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. |
| foreach (var item in matchingParameter.DeclaringSyntaxReferences) | ||
| { | ||
| var syntaxNode = item.GetSyntax(cancellationToken); | ||
| if (syntaxNode.SyntaxTree != semanticModel.SyntaxTree) | ||
| { |
| foreach (var item in methodSymbol.DeclaringSyntaxReferences) | ||
| { | ||
| var methodSyntax = item.GetSyntax(cancellationToken); | ||
| if (methodSyntax.SyntaxTree != semanticModel.SyntaxTree) | ||
| { |
| // 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 |
|
/ba-g Quarantined check timed out. |
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.