Unable to use Blazor components in source generators #32949
Labels
area-blazor
Includes: Blazor, Razor Components
feature-razor-sdk
✔️ Resolution: Duplicate
Resolved as a duplicate of another issue
Status: Resolved
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:With the .NET 6.0 branch, when running the same command, the menu and list of routes is empty:
Exceptions (if any)
None
Further technical details
The text was updated successfully, but these errors were encountered: