Fix Razor parsing for adjacent comment delimiters in code blocks#84277
Fix Razor parsing for adjacent comment delimiters in code blocks#84277davidwengier with Copilot wants to merge 6 commits into
Conversation
Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
|
@copilot can you please add an integration test that asserts test baselines so we can check the syntax tree and code gen are exactly what we'd expect. See tests that call |
Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
Added the integration baseline coverage in f39ff06, asserting both the Razor IR and generated C# for the adjacent comment delimiter case. |
Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes a Razor legacy C# parsing edge case where bracket balancing could accidentally consume the token following an embedded Razor comment, breaking parsing for adjacent Razor comment delimiters inside code blocks and potentially causing source-generation/compilation failures. It also adds regression coverage at both the source-generator and MVC integration-test layers to ensure adjacent comment boundaries are preserved in the Razor syntax tree and not emitted into generated C#.
Changes:
- Update
CSharpCodeParser.Balanceto restart the balancing loop immediately after parsing an embedded transition/comment, preventing the next token from being consumed as C#. - Add a shared integration-test helper to baseline-verify Razor syntax trees (
.stree.txt). - Add source-generator + MVC integration tests (with new
.cshtml,.ir.txt,.stree.txt,.codegen.csbaselines) for the adjacent-comment-delimiter scenario.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/Razor/src/Shared/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/IntegrationTestBase.cs | Adds AssertSyntaxTreeMatchesBaseline to enable .stree.txt baselines for integration tests. |
| src/Razor/src/Compiler/test/Microsoft.NET.Sdk.Razor.SourceGenerators.UnitTests/RazorSourceGeneratorTests.cs | Adds a source-generator regression test ensuring generation succeeds and comment delimiters don’t leak into generated C#. |
| src/Razor/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/CSharpCodeParser.cs | Fixes balancing behavior by continuing the loop after parsing embedded transitions/comments. |
| src/Razor/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorComment_AdjacentCommentsInCodeBlock.stree.txt | New Razor syntax tree baseline asserting adjacent Razor comments are distinct nodes. |
| src/Razor/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorComment_AdjacentCommentsInCodeBlock.ir.txt | New Razor IR baseline for the regression scenario. |
| src/Razor/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorComment_AdjacentCommentsInCodeBlock.cshtml | New integration-test input view reproducing adjacent comment delimiters in a code block. |
| src/Razor/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/RazorComment_AdjacentCommentsInCodeBlock.codegen.cs | New generated C# baseline verifying comment delimiters are not emitted into output. |
| src/Razor/src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/IntegrationTests/CodeGenerationIntegrationTest.cs | Adds an MVC integration test that asserts syntax tree + IR + generated C# baselines for the scenario. |
Razor views containing adjacent comment boundaries like
*@@*could makeRazorSourceGeneratorfail withCS8785, preventing generated views from contributing to output. The failure did not identify the source view, making the broken file hard to diagnose.Parser
Regression coverage