-
Notifications
You must be signed in to change notification settings - Fork 221
Fix @section directive incorrectly allowed in .razor files #12478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Modified SectionDirective.cs to only register for RazorFileKind.Legacy - Added tests to verify section directive works in .cshtml but not in .razor files Co-authored-by: chsienki <[email protected]>
|
@dotnet/razor-tooling for reviews please :) |
davidwengier
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot can you add a compiler integration test for a component with @section in it, so we can see the code-gen and ensure nothing regresses. Perhaps something like:
@{ var section = "Section"; }
@section One:
<div>
This is @section one.
</div>
|
@chsienki you should also take this out of draft if you're happy with it |
Added SectionDirective_NotAllowed integration test to verify code generation when @section is used in .razor files. The test confirms that @section is no longer recognized as a directive and is instead treated as regular content, preventing compilation errors. Co-authored-by: davidwengier <[email protected]>
... Added the compiler integration test The test shows that Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
| // Act | ||
| var generated = CompileToCSharp(""" | ||
| @{ var section = "Section"; } | ||
| @section One { <p>Content</p> } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider also verifying what happens if section variable is not defined; for completness
| // The section directive should NOT be recognized in component files. | ||
| // Verify that no RazorDirective node for 'section' exists in the syntax tree | ||
| var directiveNodes = syntaxTree.Root.DescendantNodes() | ||
| .OfType<Microsoft.AspNetCore.Razor.Language.Syntax.RazorDirectiveSyntax>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we similarly verify that such node exists in the test above?
| Children(section, c => Html(" <p>Hello World</p> ", c)); | ||
| } | ||
|
|
||
| [Fact] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding WorkItem attributes
Summary of the changes
The
@sectiondirective was incorrectly registered for both.cshtml(MVC/Razor Pages) and.razor(Blazor components) files. Per documentation,@sectionis only valid in MVC/Razor Pages. Using it in.razorfiles produces compilation errors.Changes:
SectionDirective.cs: RemovedRazorFileKind.Componentfrom directive registrationSectionDirectivePassTest.cs: Added tests verifying directive availability per file kindComponentCodeGenerationTestBase.cs: Added integration testSectionDirective_NotAllowedto verify code generation behavior and prevent regressionsBefore:
After:
Code Generation Verification:
The integration test confirms that when
@sectionis used in a.razorfile, it is no longer recognized as a directive and is instead treated as regular content, preventing compilation errors. Baseline files demonstrate the expected code generation output for both runtime and design-time scenarios.Fixes: #11273
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.