question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Razor components break source generators

See original GitHub issue

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]

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
chsienkicommented, Aug 26, 2020
0reactions
mkArtakMSFTcommented, Aug 28, 2020

Thanks for taking care of this, @chsienki!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Source Generators with Blazor components in .NET 6
In this post I describe a problem where upgrading my Blazor app to .NET 6 broke my source generators that relied on Blazor...
Read more >
Source generator for a razor component not working
1 Answer. Currently source generators only support outputting C# or VB, i.e. source the compiler is building.
Read more >
ASP.NET Core Razor components
Learn how to create and use Razor components in Blazor apps, ... useful when working with automatically-generated code or source generators.
Read more >
Unleashing the power of Source Generators in .NET
Source Generators provide a way to inject our own code-generation logic ... Source Generators can also be used in Blazor or Razor components...
Read more >
ASP.NET Community Standup - Blazorators - YouTube
David Pine introduces Blazorators, Blazor C# Source Generators on this month's Blazor Community Standup. A C# source generator that creates ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found