Steps to Reproduce:
Create a Blazor application or a Razor component library and add a source generator.
You can check my sample project for the workaround you need:
Currently, the file that uses the generated attribute must be located in a separate library.
Expected Behavior:
Source generator is also called for Blazor applications or Razor component libraries. This should also work for code behind components.
Actual Behavior:
The source code of the generator is not called, so using the generated attribute leads to compiler errors.
I'm hitting the same problem - Source Generators don't work with Blazor applications.
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 3 projects:
An interesting point is that the source generators _do_ appear to be called and executed, but this is happening _after_ the main Blazor application compilation. You can see this by viewing the dll produced by the Blazor project, which shows that it contains the HelloWorld class (screenshot taken using Rider):

FYI, I also posted this in the ASP.NET Core repo, as wasn't sure where the best place for it was: https://github.com/dotnet/aspnetcore/issues/24526
Hmm, I wonder if this is having the same issue that we are having with wpf. In that case there is a temp assembly that is used in the build process and they're explicitly excluding all analyzers. Than means that they also exclude source generators.
Thanks for taking a look @jaredpar馃檪 That description would make sense given what I'm seeing, namely that I can't reference anything created by source generators in the Blazor library, but the final assembly _does_ contain the source generator output.
@andrewlock
Can you grab a binary log of that build and upload it somewhere I can access? Essentially just add /bl to your dotnet build command it will produce an msbuild.binlog file. That will be enough for me to verify that is what is going on here.
Can you grab a binary log of that build and upload it somewhere I can access
Thanks @jaredpar. This is the log for the Web project in this small poc: https://github.com/andrewlock/blazor-with-source-generators-poc
Thanks @andrewlock, looking now
It looks like blazor isn't passing the analyzers to the compiler at all: https://github.com/dotnet/aspnetcore/blob/4e01bbe4cfeb8d63cf5ac40fcf1a6a7c77c7e911/src/Razor/Microsoft.NET.Sdk.Razor/src/build/netstandard2.0/Microsoft.NET.Sdk.Razor.Component.targets#L185
I'll open an issue with them about it.
Closing as the issue is in the razor tooling side, not the compiler. Will let the issue over their track the fix (which is in PR).
Thanks @andrewlock for the awesome repro here! Made it really easy to see the issue.
Brilliant, thanks @jaredpar and @chsienki , appreciate it! 鈾ワ笍