Aspnetcore: Razor components break source generators

Created on 3 Aug 2020  路  6Comments  路  Source: dotnet/aspnetcore

Describe 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):

image

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]

Done area-razor.tooling bug

Most helpful comment

I opened https://github.com/dotnet/aspnetcore/pull/25292 with a proposed fix.

All 6 comments

Thanks for contacting us.
We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We will evaluate the request when we are planning the work for the next milestone. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@pranavkm

This appears to be caused by not passing the @(Analyzer) item group to the compiler.

We recently added a switch that allows you to pass the group, but disable the actual analyzers themselves; that means you generators will run, but you don't waste cycles running unneeded analyzers https://github.com/dotnet/roslyn/pull/46669

馃憖

I opened https://github.com/dotnet/aspnetcore/pull/25292 with a proposed fix.

Thanks for taking care of this, @chsienki!

Was this page helpful?
0 / 5 - 0 ratings