Roslyn: Source Generators: Make it work with Blazor

Created on 13 May 2020  路  10Comments  路  Source: dotnet/roslyn

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:

BlazorSourceGeneratorTests

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.

Area-Compilers Bug New Feature - Source Generators

All 10 comments

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:

  • 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.

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

image

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

msbuild.zip

Thanks @andrewlock, looking now

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! 鈾ワ笍

Was this page helpful?
0 / 5 - 0 ratings