Skip to content
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

NET 9 Regression: Razor Source Generator False IL2111 Warning with DynamicComponent #58922

Open
ScarletKuro opened this issue Nov 12, 2024 · 1 comment
Labels
area-blazor Includes: Blazor, Razor Components untriaged

Comments

@ScarletKuro
Copy link

ScarletKuro commented Nov 12, 2024

The DynamicComponent has the following signature:

public class DynamicComponent : IComponent
{
    ...
    [Parameter]
    [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
    [EditorRequired]
    public Type Type { get; set; }
    ...
}

Now, if you have a class that uses the DynamicComponent:

internal class SnackbarMessage
{
    [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
    internal Type ComponentType { get; }
    internal Dictionary<string, object>? ComponentParameters { get; }
    internal string? Key { get; }

    internal SnackbarMessage(
        [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type componentType, 
        Dictionary<string, object>? componentParameters = null, 
        string? key = null)
    {
        ComponentType = componentType;
        ComponentParameters = componentParameters;
        Key = key;
    }

    internal string? Text { get; init; }
}

And you use it like this:

<DynamicComponent Type="Message.ComponentType" Parameters="Message.ComponentParameters" />

This will always result in an error / warning:

C:\Users\KuroPC\source\repos\MudBlazor\src\MudBlazor\obj\Debug\net9.0\generated\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Components_Snackbar_MudSnackbarElement_razor.g.cs(236,13,236,98): 
error IL2111: Method 'Microsoft.AspNetCore.Components.DynamicComponent.Type.set' with parameters or return value with `DynamicallyAccessedMembersAttribute` is accessed via reflection. Trimmer can't guarantee availability of the requirements of the method.

However, since both the DynamicComponent and the ComponentType property in SnackbarMessage have the same DAM annotation, why does this result in an error? There are no RequiresUnreferencedCode attributes on DynamicComponent, so I would expect that if the annotations match, there shouldn't be any issues.

Here’s the generated Razor code lines that it complains on:

#nullable restore
#line (37,10)-(39,1) "C:\Users\KuroPC\source\repos\MudBlazor\src\MudBlazor\Components\Snackbar\MudSnackbarElement.razor"
if (Message?.ComponentType != null)
        {

#line default
#line hidden
#nullable disable

            __builder.OpenComponent<global::Microsoft.AspNetCore.Components.DynamicComponent>(22);
            __builder.AddComponentParameter(23, nameof(global::Microsoft.AspNetCore.Components.DynamicComponent.
#nullable restore
#line (39,31)-(39,35) "C:\Users\KuroPC\source\repos\MudBlazor\src\MudBlazor\Components\Snackbar\MudSnackbarElement.razor"
Type

#line default
#line hidden
#nullable disable
            ), global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Type>(
#nullable restore
#line (39,37)-(39,58) "C:\Users\KuroPC\source\repos\MudBlazor\src\MudBlazor\Components\Snackbar\MudSnackbarElement.razor"
Message.ComponentType

#line default
#line hidden
#nullable disable
            ));

NB! This was not an issue with the .NET 8 SDK and it had the same DAM annotation like in NET9.

@davidwengier davidwengier transferred this issue from dotnet/razor Nov 12, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-componentmodel
See info in area-owners.md if you want to be subscribed.

@steveharter steveharter transferred this issue from dotnet/runtime Nov 13, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components untriaged
Projects
None yet
Development

No branches or pull requests

1 participant