Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 25, 2025

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:

  1. An _Imports.razor file contains @layout MyLayout
  2. MyLayout.razor is defined in a subfolder and extends LayoutComponentBase

This results in generated code like:

[Layout(typeof(MyLayout))]
public class MyLayout : LayoutComponentBase
{
}

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:

  • Diagnostic ID: BL0010 (Error severity)
  • Detection: Identifies types extending LayoutComponentBase that have LayoutAttribute pointing to themselves
  • Message: "Layout component '{0}' has a [Layout] attribute that references itself, which will cause an infinite rendering loop."

Example

Before (runtime failure):

[Layout(typeof(MyLayout))]
public class MyLayout : LayoutComponentBase
{
    // This compiles but crashes at runtime
}

After (compile-time error):

error BL0010: Layout component 'MyLayout' has a [Layout] attribute that references itself, which will cause an infinite rendering loop.

Changes

  • Added LayoutCycleAnalyzer with comprehensive cycle detection logic
  • Extended ComponentSymbols and ComponentsApi to support layout-related types
  • Added BL0010 diagnostic descriptor with localized resources
  • Added test coverage for all scenarios (self-reference, valid references, non-layout components)

Testing

All existing tests pass (64/64) with no regressions. Added 4 new tests covering:

  • ✅ Self-referencing layout component (reports diagnostic)
  • ✅ Layout component without attribute (no diagnostic)
  • ✅ Layout component referencing different layout (no diagnostic)
  • ✅ Non-layout component with layout attribute (no diagnostic)

Fixes the infinite rendering loop issue by moving detection from runtime to compile-time, providing early feedback to developers.

Original prompt

This section details on the original issue you should resolve

<issue_title>[Blazor] Analyzer for detecting basic cycles in Layout components</issue_title>
<issue_description>If you put a @Layout MyLayout directive inside an _Imports.razor and MyLayout.razor is defined in a subfolder of that folder, you end up with something like this:

[Layout(MyLayout)]
public class MyLayout : LayoutBase
{
}

Creating an infinite rendering loop. We don't want to anything at runtime, but we can do something at compile time to detect this scenario.</issue_description>

<agent_instructions>The analyzer will simply check that no type extending LayoutBase has a Layout attribute applied that points to itself.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

Fixes #63519

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copy link
Contributor

Greetings human! You've submitted a PR that modifies code that is shared with https://github.com/dotnet/runtime . Please make sure you synchronize this code with the changes in that repo!

…omponents

Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] [Blazor] Analyzer for detecting basic cycles in Layout components Add Blazor analyzer to detect layout components that reference themselves Sep 25, 2025
@Copilot Copilot AI requested a review from javiercn September 25, 2025 12:08
Copilot finished work on behalf of javiercn September 25, 2025 12:08
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.

[Blazor] Analyzer for detecting basic cycles in Layout components
2 participants