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

Razor.SourceGenerators bug causes compilation error when using generics(.NET 6) #8770

Closed
1 task done
ScarletKuro opened this issue May 27, 2023 · 5 comments
Closed
1 task done
Assignees
Labels
area-compiler Umbrella for all compiler issues
Milestone

Comments

@ScarletKuro
Copy link

ScarletKuro commented May 27, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Greetings,

I wanted to bring your attention to an issue raised by one of the MudBlazor users: MudBlazor/MudBlazor#6886.
Upon examining the reproduction provided in this GitHub repository: https://github.com/ScarletKuro/RazorRepro
It appears to be a bug related to the Razor SourceGenerator.

The issue arises when pinning the SDK to version 6, as the code fails to compile. However, it works seamlessly with SDK .NET 7, indicating that something was not backported to SDK 6.
Considering that .NET 6 will only be out of support in November 2024, it would be beneficial to address this matter promptly.

The problem lies within the following code snippet:

public class MyColumn<TProperty> : PropertyColumn<MyViewModel, TProperty>

When a customer attempts to use it as follows:

<MudDataGrid T="MyViewModel">
    <Columns>
        <MyColumn Property="x => x.MyProperty">
            <CellTemplate>X</CellTemplate>
        </MyColumn>
    </Columns>
</MudDataGrid>

The Razor SourceGenerator generates incorrect code. As a workaround, you can either use two generics in the MyColumn class, such as MyColumn<T, TProperty> : PropertyColumn<T, TProperty>, or remove the CellTemplate portion, which will allow the code to compile successfully.

This code it generates on .NET6:

__builder.OpenComponent<MudBlazor.MudDataGrid<MyViewModel>>(0);
__builder.AddAttribute(1, "Columns", (Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
	__Blazor.Repro.Shared.MyDataGridExample.TypeInference.CreateMyColumn_0(__builder2, 2, 3, 
				x => x.MyProperty
	, 4, (context) => (__builder3) => {
		__builder3.AddContent(5, "X");
	}
	);
}
));

This code it generates on .NET7:

__builder.OpenComponent<global::MudBlazor.MudDataGrid<MyViewModel>>(0);
__builder.AddAttribute(1, "Columns", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
	global::__Blazor.Repro.Shared.MyDataGridExample.TypeInference.CreateMyColumn_0(__builder2, 2, 3, 
				x => x.MyProperty
	, 4, (context) => (__builder3) => {
		__builder3.AddContent(5, "X");
	}
	);
}
));
__builder.CloseComponent();

Exceptions (if any)

MudDataGridRepro\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Shared_MyDataGridExample_razor.g.cs(93,348): error CS0234: The type or namespace name 'CellContext<>' does not exist in the namespace '__Blazor.MudBlazor' (are you missing an assembly reference?)
MudDataGridRepro\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Shared_MyDataGridExample_razor.g.cs(93,366): error CS0234: The type or namespace name 'Data' does not exist in the namespace '__Blazor.Repro' (are you missing an assembly reference?)

.NET Version

6.0.311: build errors
6.0.408: build errors
7.0.203; build succeeded
7.0.302: build succeeded

@ScarletKuro
Copy link
Author

Hello @jjonescz, could you please take a look at this?

@davidwengier davidwengier added the area-compiler Umbrella for all compiler issues label May 27, 2023
@chsienki
Copy link
Contributor

chsienki commented Jun 1, 2023

@jjonescz Do you know which issue fixed this in newer builds?

@chsienki chsienki added this to the 17.7 Planning milestone Jun 1, 2023
@ghost ghost removed the untriaged label Jun 1, 2023
@jjonescz
Copy link
Member

jjonescz commented Jun 8, 2023

I think it's #8247. Can rebase it, but not sure what target branch to use to make it flow into .NET 6. Seems release/dev17.6 doesn't contain that PR, so here's a PR for that: #8815

@ScarletKuro
Copy link
Author

fyi: reproducible in latest 6.0.413 as well

@jjonescz
Copy link
Member

I think this doesn't meet our bar for backporting to .NET 6, sorry for the inconvenience.

@jjonescz jjonescz closed this as not planned Won't fix, can't repro, duplicate, stale Aug 11, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Sep 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-compiler Umbrella for all compiler issues
Projects
None yet
Development

No branches or pull requests

4 participants