Razor components break source generators
See original GitHub issueDescribe the bug
Source generators do not work correctly in any project project that contains a .razor
component. If the compilation depends on the generator output, then compilation fails.
To Reproduce
I have uploaded a proof of concept, using the “Hello world” source generator and the latest .NET 5 preview (5.0.0-preview.7.20364.11): https://github.com/andrewlock/blazor-with-source-generators-poc
The solution includes 4 projects:
- The Hello World Source Generator from here
- A basic .NET 5 Console application, demonstrating usage of the source generator. This project compiles, builds, and runs perfectly.
- A basic Blazor WASM application, demonstrating an identical usage, which doesn’t compile, due to using the output of the generator in Program.cs
- An empty Web project (
dotnet new web
), with the addition of a single basic Razor component. Without the component, the project builds. With the component, the build fails due to using the output of the generator in Program.cs
Program.cs(16,13): error CS0103: The name 'HelloWorldGenerated' does not exist in the current context [C:\repos\source-generator-poc\src\Web\Web.csproj]
Program.cs(17,13): error CS0103: The name 'HelloWorldGenerated' does not exist in the current context [C:\repos\source-generator-poc\src\BlazorApp1\BlazorApp1.csproj]
An interesting point is that the source generators do appear to be called and executed, but this is happening after the main application compilation. You can see this by viewing the dll produced by the Blazor/Web project, which shows that it contains the HelloWorld
class (screenshot taken using Rider):
Further technical details
ASP.NET Core version: 5.0.0-preview.7.20364.11 IDE: None (building using .NET CLI) dotnet --info .NET SDK (reflecting any global.json): Version: 5.0.100-preview.7.20366.6 Commit: 0684df3a5b
Runtime Environment: OS Name: Windows OS Version: 10.0.18363 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\5.0.100-preview.7.20366.6\
Host (useful for support): Version: 5.0.0-preview.7.20364.11 Commit: 53976d38b1
.NET SDKs installed: 1.1.14 [C:\Program Files\dotnet\sdk] 2.1.602 [C:\Program Files\dotnet\sdk] 2.1.604 [C:\Program Files\dotnet\sdk] 2.1.801 [C:\Program Files\dotnet\sdk] 2.2.203 [C:\Program Files\dotnet\sdk] 3.1.302 [C:\Program Files\dotnet\sdk] 5.0.100-preview.7.20366.6 [C:\Program Files\dotnet\sdk]
.NET runtimes installed: Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.2.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.2.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.0-preview.7.20365.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 1.0.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 1.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.2.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.0-preview.7.20364.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.0-preview.7.20366.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (6 by maintainers)
I opened https://github.com/dotnet/aspnetcore/pull/25292 with a proposed fix.
Thanks for taking care of this, @chsienki!