Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 25, 2025

Adds a new analyzer (BL0010) that warns developers when using required or init modifiers on Blazor component parameter properties, addressing issues reported in #47824 and #44974.

Problem

Customers have been attempting to use the required modifier on Blazor component parameters to ensure parameters have values, but this doesn't work as expected because:

  1. Blazor parameters are set via reflection - Component parameters don't get assigned during instantiation but through ParameterView.SetParameterProperties(), which bypasses required restrictions
  2. Misleading behavior - The required modifier makes nullability warnings disappear but provides no actual guarantees
  3. Future breaking changes - When Consider using C#9 Source Generators for implementing SetParametersAsync #29550 is implemented (moving away from reflection), required/init could cause compilation errors

Solution

The new analyzer detects both required and init modifiers on [Parameter] properties and suggests using the [EditorRequired] attribute instead:

public class MyComponent : ComponentBase
{
    // ❌ Warns: BL0010 - Component parameter should not use 'required' modifier
    [Parameter] public required string Name { get; set; }
    
    // ❌ Warns: BL0010 - Component parameter should not use 'init' modifier  
    [Parameter] public string Value { get; init; }
    
    // ✅ Recommended approach
    [Parameter, EditorRequired] public string Title { get; set; }
    
    // ✅ Non-parameter properties are ignored
    public required string InternalProperty { get; set; }
}

Implementation Details

  • Diagnostic ID: BL0010
  • Severity: Warning
  • Category: Usage
  • Detection: Analyzes property syntax trees to identify required/init modifiers on [Parameter] properties
  • Scope: Only targets component parameters, ignores regular properties
  • Message: Suggests using [EditorRequired] attribute as the proper alternative

The analyzer integrates seamlessly with the existing ASP.NET Core analyzer infrastructure and follows established patterns for diagnostic reporting and localization.

Fixes #48051

Original prompt

This section details on the original issue you should resolve

<issue_title>[Blazor] Analyzer to warn when using required or init on Blazor component properties</issue_title>
<issue_description>## Background

We've seen a few issue reports (e.g., #47824, #44974) where customers attempt to use the required modifier on Blazor component parameter properties to ensure that the parameter will definitely have a value assigned to it.

Why this is a problem

This is not a supported pattern (and it likely never will be) because Blazor component parameters do not get assigned upon component instantiation. Instead, a ParameterView object gets passed to the component's SetParametersAsync method, which updates parameter property values accordingly. The required modifier has no bearing on whether the parameter will have an entry in the ParameterView. Furthermore, even for parameters that do have ParameterView entries, their properties won't get values assigned until long after what the required modifier is supposed to guarantee.

Currently, using the required modifier in a Blazor component doesn't break anything, because any component extending ComponentBase (or otherwise manually invoking ParameterView.SetParameterProperties()) gets its properties set via refelction. But the required modifier can mislead component authors into thinking that it will make the property "required", especially considering it makes nullability warnings go away.

Likewise, init has no effect in any component whose parameters are set using ParameterView.SetParameterProperties(), because reflection bypasses the "init-only" restriction.

If we implement #29550, then using required or init could cause compilation errors, since we won't be using reflection to bypass the restrictions that required or init impose. Therefore, for future-proofing reasons, it's in our best interest to ensure that customers avoid using these keywords on Blazor component properties.

Proposed solution

For all these reasons, we should consider implementing an analyzer that emits a warning whenever component properties use required or init. The analyzer message can suggest using the [EditorRequired] attribute instead.</issue_description>

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

@ Thanks for contacting us.

We're moving this issue to the .NET 8 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.</comment_new>
<comment_new>@
Thanks for contacting us.

We're moving this issue to the .NET 9 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.</comment_new>
<comment_new>@
Thanks for contacting us.

We're moving this issue to the .NET 9 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.</comment_new>

Fixes #47864

💡 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.

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!

@Copilot Copilot AI changed the title [WIP] [Blazor] Analyzer to warn when using required or init on Blazor component properties Add analyzer to warn when using required or init on Blazor component parameters Sep 25, 2025
@Copilot Copilot AI requested a review from javiercn September 25, 2025 12:13
Copilot finished work on behalf of javiercn September 25, 2025 12:13
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 to warn when using required or init on Blazor component properties Perf regression: https, single_query
2 participants