dotnet/aspnetcore

Unable to use Blazor components in source generators

andrewlock opened this issue · 2 comments

Describe the bug

In a series of posts on my blog, I showed how to create a Source Generator that finds all routable components in a Blazor app and lists them on the home page. I use this approach in a later post to generate a menu component automatically.

The code I used was v. simple - find all classes decorated with the attribute: Microsoft.AspNetCore.Components.RouteAttribute . I then extract the route from this attribute.

This worked in .NET 5.0 because the generated C# component code was available to the source generator, so I could find the C# component from my source generator. After upgrading to .NET 6.0, my source generators no longer work. The Razor tooling (which I believe has switched to source generators?) does not make the generated code available to my source generators.

Is there a known solution to this problem? I believe the current behvaiour is "correct" given the design of source generators, i.e. source generation is a "one hit" thing, you can't feed generated code back in. But what are my options here? Do I really have to re-implement the new Razor source generators (i.e. try and parse .razor files) to "fix" my source generators? Is there any way for me to "hook into" the existing Razor source generators? Are there any other long-term solutions, assuming that Razor will definitely be using source generators going forward?

To Reproduce

My source generators solution from my blog post is here . A branch in which this is upgraded to .NET 6 is shown here. The source generator itself is here.

The only difference between the solutions is upgrading to .NET 6 (I tested with preview 3).

Prior to the change, running the app (dotnet run --project .\BlazorApp1.Server\BlazorApp1.Server.csproj) generates the menu components automatically, and also lists the routes discovered in the app:

image

With the .NET 6.0 branch, when running the same command, the menu and list of routes is empty:

image

Exceptions (if any)

None

Further technical details

  • .NET 6.0 preview 3
  • VS Code
.NET SDK (reflecting any global.json):
 Version:   6.0.100-preview.3.21202.5
 Commit:    aee38a6dd4

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19042
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.100-preview.3.21202.5\

Host (useful for support):
  Version: 6.0.0-preview.3.21201.4
  Commit:  236cb21e3c

.NET SDKs installed:
  5.0.104 [C:\Program Files\dotnet\sdk]
  6.0.100-preview.3.21202.5 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0-preview.3.21201.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-preview.3.21201.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0-preview.3.21201.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

@andrewlock thanks for contacting us.

This is a result of moving the razor compiler to leverage source generators. @pranavkm @captainsafia can you comment on here? I believe there are other issues tracking this.

Thanks for contacting us.
This is a known issue that we track as part of #32172