Add Blazor analyzer to detect layout components that reference themselves #63824
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Adds a new Blazor analyzer (
LayoutCycleAnalyzer
) to detect when layout components create infinite rendering loops by referencing themselves through the[Layout]
attribute.Problem
When a layout component references itself via the
[Layout]
attribute, it creates an infinite rendering loop at runtime. This commonly occurs when:_Imports.razor
file contains@layout MyLayout
MyLayout.razor
is defined in a subfolder and extendsLayoutComponentBase
This results in generated code like:
The
LayoutView.Render()
method follows the layout chain indefinitely, causing a stack overflow at runtime.Solution
This PR introduces compile-time detection via a new analyzer:
BL0010
(Error severity)LayoutComponentBase
that haveLayoutAttribute
pointing to themselvesExample
Before (runtime failure):
After (compile-time error):
Changes
LayoutCycleAnalyzer
with comprehensive cycle detection logicComponentSymbols
andComponentsApi
to support layout-related typesBL0010
diagnostic descriptor with localized resourcesTesting
All existing tests pass (64/64) with no regressions. Added 4 new tests covering:
Fixes the infinite rendering loop issue by moving detection from runtime to compile-time, providing early feedback to developers.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.