From 9fe760218a00a0cb4bdb0088b802b3b0a52af8a5 Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Tue, 11 Nov 2025 23:07:22 +0000
Subject: [PATCH 1/3] Initial plan
From c8a4e637465107e8ffd5a986297cdbd4bc2c8659 Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Tue, 11 Nov 2025 23:33:41 +0000
Subject: [PATCH 2/3] Fix @section directive to only be legal in .cshtml files
- 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 <16246502+chsienki@users.noreply.github.com>
---
.../Extensions/SectionDirectivePassTest.cs | 44 +++++++++++++++++++
.../test/Legacy/CSharpSectionTest.cs | 1 -
.../Language/Extensions/SectionDirective.cs | 2 +-
3 files changed, 45 insertions(+), 2 deletions(-)
diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Extensions/SectionDirectivePassTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Extensions/SectionDirectivePassTest.cs
index a50302a5802..2d149e5702f 100644
--- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Extensions/SectionDirectivePassTest.cs
+++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Extensions/SectionDirectivePassTest.cs
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
+using System.Linq;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Xunit;
using static Microsoft.AspNetCore.Razor.Language.Intermediate.IntermediateNodeAssert;
@@ -76,4 +77,47 @@ public void Execute_WrapsStatementInSectionNode()
Assert.Equal("Header", section.SectionName);
Children(section, c => Html("
Hello World
", c));
}
+
+ [Fact]
+ public void SectionDirective_IsRecognizedInLegacyFiles()
+ {
+ // Arrange
+ var content = "@section Header { Hello World
}";
+ var source = TestRazorSourceDocument.Create(content, filePath: "test.cshtml", relativePath: "test.cshtml");
+
+ // Act
+ var codeDocument = ProjectEngine.CreateCodeDocument(source, RazorFileKind.Legacy);
+ ProjectEngine.Engine.Process(codeDocument);
+
+ // Assert
+ var syntaxTree = codeDocument.GetSyntaxTree();
+ Assert.NotNull(syntaxTree);
+
+ // The section directive should be recognized without errors
+ var diagnostics = syntaxTree.Diagnostics;
+ Assert.Empty(diagnostics);
+ }
+
+ [Fact]
+ public void SectionDirective_IsNotRecognizedInComponentFiles()
+ {
+ // Arrange
+ var content = "@section Header { Hello World
}";
+ var source = TestRazorSourceDocument.Create(content, filePath: "test.razor", relativePath: "test.razor");
+
+ // Act
+ var codeDocument = ProjectEngine.CreateCodeDocument(source, RazorFileKind.Component);
+ ProjectEngine.Engine.Process(codeDocument);
+
+ // Assert
+ var syntaxTree = codeDocument.GetSyntaxTree();
+ Assert.NotNull(syntaxTree);
+
+ // 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();
+
+ Assert.DoesNotContain(directiveNodes, d => d.DirectiveDescriptor?.Directive == "section");
+ }
}
diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpSectionTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpSectionTest.cs
index be35c04c2d3..8f1345d3b75 100644
--- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpSectionTest.cs
+++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpSectionTest.cs
@@ -300,5 +300,4 @@ public void _WithDoubleTransition2()
{
ParseDocumentTest("@section s {}", [SectionDirective.Directive]);
}
-
}
diff --git a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Extensions/SectionDirective.cs b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Extensions/SectionDirective.cs
index 8a078b433bc..4636c8b2d55 100644
--- a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Extensions/SectionDirective.cs
+++ b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Extensions/SectionDirective.cs
@@ -26,7 +26,7 @@ public static void Register(RazorProjectEngineBuilder builder)
throw new ArgumentNullException(nameof(builder));
}
- builder.AddDirective(Directive, RazorFileKind.Legacy, RazorFileKind.Component);
+ builder.AddDirective(Directive, RazorFileKind.Legacy);
builder.Features.Add(new SectionDirectivePass());
builder.AddTargetExtension(new SectionTargetExtension());
}
From 985a0a4957af4d594e2e8c65ded02a76707b01ed Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Wed, 12 Nov 2025 05:34:52 +0000
Subject: [PATCH 3/3] Add compiler integration test for @section directive in
components
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 <754264+davidwengier@users.noreply.github.com>
---
.../ComponentCodeGenerationTestBase.cs | 16 +++++++
.../TestComponent.codegen.cs | 45 +++++++++++++++++++
.../TestComponent.ir.txt | 27 +++++++++++
.../TestComponent.mappings.txt | 10 +++++
.../TestComponent.codegen.cs | 42 +++++++++++++++++
.../TestComponent.ir.txt | 16 +++++++
.../TestComponent.mappings.txt | 10 +++++
7 files changed, 166 insertions(+)
create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.codegen.cs
create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.ir.txt
create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.mappings.txt
create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.codegen.cs
create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.ir.txt
create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.mappings.txt
diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs
index 3f328189d33..577bb12c14c 100644
--- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs
+++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs
@@ -12968,5 +12968,21 @@ @inject float Value5
CompileToAssembly(generated);
}
+ [IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/11273")]
+ public void SectionDirective_NotAllowed()
+ {
+ // Verify that @section is not recognized in components and produces appropriate code-gen
+ // Act
+ var generated = CompileToCSharp("""
+ @{ var section = "Section"; }
+ @section One { Content
}
+ """);
+
+ // Assert
+ AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
+ AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
+ CompileToAssembly(generated);
+ }
+
#endregion
}
diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.codegen.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.codegen.cs
new file mode 100644
index 00000000000..04a0de9e6dc
--- /dev/null
+++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.codegen.cs
@@ -0,0 +1,45 @@
+//
+#pragma warning disable 1591
+namespace Test
+{
+ #line default
+ using global::System;
+ using global::System.Collections.Generic;
+ using global::System.Linq;
+ using global::System.Threading.Tasks;
+ using global::Microsoft.AspNetCore.Components;
+ #line default
+ #line hidden
+ #nullable restore
+ public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
+ #nullable disable
+ {
+ #pragma warning disable 219
+ private void __RazorDirectiveTokenHelpers__() {
+ }
+ #pragma warning restore 219
+ #pragma warning disable 0414
+ private static object __o = null;
+ #pragma warning restore 0414
+ #pragma warning disable 1998
+ protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
+ {
+#nullable restore
+#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
+ var section = "Section";
+
+#line default
+#line hidden
+#nullable disable
+#nullable restore
+#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
+__o = section;
+
+#line default
+#line hidden
+#nullable disable
+ }
+ #pragma warning restore 1998
+ }
+}
+#pragma warning restore 1591
diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.ir.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.ir.txt
new file mode 100644
index 00000000000..2546d98f9e3
--- /dev/null
+++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.ir.txt
@@ -0,0 +1,27 @@
+Document -
+ NamespaceDeclaration - - Test
+ UsingDirective - (3:1,1 [20] ) - global::System
+ UsingDirective - (26:2,1 [40] ) - global::System.Collections.Generic
+ UsingDirective - (69:3,1 [25] ) - global::System.Linq
+ UsingDirective - (97:4,1 [36] ) - global::System.Threading.Tasks
+ UsingDirective - (136:5,1 [45] ) - global::Microsoft.AspNetCore.Components
+ ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
+ DesignTimeDirective -
+ CSharpCode -
+ IntermediateToken - - CSharp - #pragma warning disable 0414
+ CSharpCode -
+ IntermediateToken - - CSharp - private static object __o = null;
+ CSharpCode -
+ IntermediateToken - - CSharp - #pragma warning restore 0414
+ MethodDeclaration - - protected override - void - BuildRenderTree
+ CSharpCode - (2:0,2 [26] x:\dir\subdir\Test\TestComponent.cshtml)
+ LazyIntermediateToken - (2:0,2 [26] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - var section = "Section";
+ CSharpExpression - (32:1,1 [7] x:\dir\subdir\Test\TestComponent.cshtml)
+ LazyIntermediateToken - (32:1,1 [7] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - section
+ HtmlContent - (39:1,8 [7] x:\dir\subdir\Test\TestComponent.cshtml)
+ LazyIntermediateToken - (39:1,8 [7] x:\dir\subdir\Test\TestComponent.cshtml) - Html - One {
+ MarkupElement - (46:1,15 [14] x:\dir\subdir\Test\TestComponent.cshtml) - p
+ HtmlContent - (49:1,18 [7] x:\dir\subdir\Test\TestComponent.cshtml)
+ LazyIntermediateToken - (49:1,18 [7] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Content
+ HtmlContent - (60:1,29 [2] x:\dir\subdir\Test\TestComponent.cshtml)
+ LazyIntermediateToken - (60:1,29 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - }
diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.mappings.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.mappings.txt
new file mode 100644
index 00000000000..30f6624dbad
--- /dev/null
+++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.mappings.txt
@@ -0,0 +1,10 @@
+Source Location: (2:0,2 [26] x:\dir\subdir\Test\TestComponent.cshtml)
+| var section = "Section"; |
+Generated Location: (987:28,2 [26] )
+| var section = "Section"; |
+
+Source Location: (32:1,1 [7] x:\dir\subdir\Test\TestComponent.cshtml)
+|section|
+Generated Location: (1141:35,6 [7] )
+|section|
+
diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.codegen.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.codegen.cs
new file mode 100644
index 00000000000..1ee576f73ae
--- /dev/null
+++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.codegen.cs
@@ -0,0 +1,42 @@
+//
+#pragma warning disable 1591
+namespace Test
+{
+ #line default
+ using global::System;
+ using global::System.Collections.Generic;
+ using global::System.Linq;
+ using global::System.Threading.Tasks;
+ using global::Microsoft.AspNetCore.Components;
+ #line default
+ #line hidden
+ #nullable restore
+ public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
+ #nullable disable
+ {
+ #pragma warning disable 1998
+ protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
+ {
+#nullable restore
+#line (1,3)-(1,29) "x:\dir\subdir\Test\TestComponent.cshtml"
+ var section = "Section";
+
+#line default
+#line hidden
+#nullable disable
+
+#nullable restore
+#line (2,2)-(2,9) 24 "x:\dir\subdir\Test\TestComponent.cshtml"
+__builder.AddContent(0, section
+
+#line default
+#line hidden
+#nullable disable
+ );
+ __builder.AddContent(1, " One { ");
+ __builder.AddMarkupContent(2, "Content
}");
+ }
+ #pragma warning restore 1998
+ }
+}
+#pragma warning restore 1591
diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.ir.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.ir.txt
new file mode 100644
index 00000000000..e964c486204
--- /dev/null
+++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.ir.txt
@@ -0,0 +1,16 @@
+Document -
+ NamespaceDeclaration - - Test
+ UsingDirective - (3:1,1 [20] ) - global::System
+ UsingDirective - (26:2,1 [40] ) - global::System.Collections.Generic
+ UsingDirective - (69:3,1 [25] ) - global::System.Linq
+ UsingDirective - (97:4,1 [36] ) - global::System.Threading.Tasks
+ UsingDirective - (136:5,1 [45] ) - global::Microsoft.AspNetCore.Components
+ ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
+ MethodDeclaration - - protected override - void - BuildRenderTree
+ CSharpCode - (2:0,2 [26] x:\dir\subdir\Test\TestComponent.cshtml)
+ LazyIntermediateToken - (2:0,2 [26] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - var section = "Section";
+ CSharpExpression - (32:1,1 [7] x:\dir\subdir\Test\TestComponent.cshtml)
+ LazyIntermediateToken - (32:1,1 [7] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - section
+ HtmlContent - (39:1,8 [7] x:\dir\subdir\Test\TestComponent.cshtml)
+ LazyIntermediateToken - (39:1,8 [7] x:\dir\subdir\Test\TestComponent.cshtml) - Html - One {
+ MarkupBlock - - Content
}
diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.mappings.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.mappings.txt
new file mode 100644
index 00000000000..3f1bbf943f0
--- /dev/null
+++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentRuntimeCodeGenerationTest/SectionDirective_NotAllowed/TestComponent.mappings.txt
@@ -0,0 +1,10 @@
+Source Location: (2:0,2 [26] x:\dir\subdir\Test\TestComponent.cshtml)
+| var section = "Section"; |
+Generated Location: (735:21,0 [26] )
+| var section = "Section"; |
+
+Source Location: (32:1,1 [7] x:\dir\subdir\Test\TestComponent.cshtml)
+|section|
+Generated Location: (922:29,24 [7] )
+|section|
+