Skip to content

Fix Razor parsing for adjacent comment delimiters in code blocks#84277

Open
davidwengier with Copilot wants to merge 6 commits into
mainfrom
copilot/fix-razor-view-code-generation
Open

Fix Razor parsing for adjacent comment delimiters in code blocks#84277
davidwengier with Copilot wants to merge 6 commits into
mainfrom
copilot/fix-razor-view-code-generation

Conversation

Copilot AI commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Razor views containing adjacent comment boundaries like *@@* could make RazorSourceGenerator fail with CS8785, preventing generated views from contributing to output. The failure did not identify the source view, making the broken file hard to diagnose.

  • Parser

    • Avoid consuming the token after an embedded Razor comment during C# bracket balancing.
    • This lets adjacent comment boundaries parse as distinct Razor comments instead of leaking into generated C#.
  • Regression coverage

    • Added a source-generator test for the MVC view pattern from the issue.
    • Added MVC integration test baselines that assert the Razor syntax tree, Razor IR, and generated C# for the adjacent comment delimiter case.
    • Verifies generation succeeds, Razor comment nodes are preserved in the syntax tree, and Razor comment delimiters are not emitted into generated C#.
@class = ""
@*                @class = ""
*@@*                @class = ""
*@            }))

Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
Copilot AI and others added 2 commits June 25, 2026 06:56
Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix razor view generation issue with *@@* characters Fix Razor parsing for adjacent comment delimiters in code blocks Jun 25, 2026
Copilot AI requested a review from davidwengier June 25, 2026 07:00
@davidwengier

Copy link
Copy Markdown
Member

@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 AssertDocumentNodeMatchesBaseline and AssertCSharpDocumentMatchesBaseline for example.

Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>

Copilot AI commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

@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 `AssertDocumentNodeMatchesBas...

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>
Copilot AI requested a review from davidwengier June 25, 2026 11:17
@davidwengier davidwengier marked this pull request as ready for review June 25, 2026 11:24
@davidwengier davidwengier requested a review from a team as a code owner June 25, 2026 11:24
Copilot AI review requested due to automatic review settings June 25, 2026 11:24

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

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.Balance to 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.cs baselines) 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.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants