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.

DotNet 6 Preview 4: CS8785: Generator 'RazorSourceGenerator' failed to generate source

See original GitHub issue

Describe the bug

After upgrading to Net 6 Preview 4 from Net 5, all my projects were successfully built except for one with cshtml and razor files. It was working fine under Net 5 but fails to build under Net 6.

The only useful piece of info I could find is from the build output:

CSC : warning CS8785: Generator 'RazorSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'AggregateException' with message 'One or more errors occurred. (Specified argument was out of the range of valid values. (Parameter 'index'))' 

It does not reference any concrete code file or specific error it encountered in there.

I get the same error whether I build in VS or from dotnet CLI.

Strangely, intellisense in VS does not report any errors for entirely .razor-based components. That is I can easily reference my .razor-based components in .cs code files w/o any issues. Yet, build fails saying that exactly those components / namespaces are undefined (presumably because during build generator fails to generate cs code for those, while it works during design/editing in VS).

Also, in VS there is no output of Microsoft.NET.Sdk.Razor.SourceGenerators: no .cs files are visible under that node.

Exceptions (if any)

CSC : warning CS8785: Generator 'RazorSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'AggregateException' with message 'One or more errors occurred. (Specified argument was out of the range of valid values. (Parameter 'index'))' 

Further technical details

  • ASP.NET Core version:
  • Include the output of dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.100-preview.4.21255.9
 Commit:    950e4949a7

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

Host (useful for support):
  Version: 6.0.0-preview.4.21253.7
  Commit:  bfd6048a60

.NET SDKs installed:
  2.1.520 [C:\Program Files\dotnet\sdk]
  5.0.300-preview.21258.4 [C:\Program Files\dotnet\sdk]
  6.0.100-preview.4.21255.9 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.2 [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.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.2 [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.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0-preview.4.21253.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.24 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.15 [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 6.0.0-preview.4.21253.7 [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.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0-preview.4.21254.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  • The IDE (VS / VS Code/ VS4Mac) you’re running on, and its version: VS 2019 16.11 Preview 1

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
GordonRudmancommented, Jun 10, 2021

Please Please Please fix this as soon as possible.

Over the last few months I’ve wasted many DAYS already because of this issue (since preview 3 launched) - Basically every time I try update VS2019 or .net 6. I’m currently stuck on VS2019 v16.9.7 and .net 6 preview 2 specifically because of this issue, because all the newer versions of .net 6 and VS2019 are incompatible with Blazor (even using the standard templates inside VS0219).

Please also make the error messages more useful - they are completely useless and actually counter productive at the moment. Please also make it throw a proper error instead of warnings - The current behaviour is dangerous! I’m really hard pressed to think of examples where I want a warning instead of an error message when the code I’m expecting to get generated doesn’t. Because the code doesn’t get generated it just leads to confusing errors further down the chain.

This is just one of the many warnings I’m getting because of the RazorSourceGenerator: CSC : warning CS8785: Generator ‘RazorSourceGenerator’ failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type ‘InvalidOperationException’ with message 'TargetPath is not specified for additional file ‘C:!\RazorGeneratorErrors1a\BlazorApp1\App.razor.’

The following repo has a simple Blazor server website in it. It works perfectly in VS2019 v16.9.7 .Net 6 Preview 2, but doesn’t even compile in any of the newer versions of VS2019 with .net 6 (preview 2, 3, or 4). It does however unfortunately compile and get deployed when using the CLI tools (as they treat the problems as warnings instead), but results in a broken website being deployed 😦 and then even more cryptic error messages to wade through on Azure: https://github.com/GordonRudman/RazorGeneratorErrors1

0reactions
msftbot[bot]commented, Oct 4, 2021

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

See our Issue Management Policies for more information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

.net - How to fix C# Source Generators Issue of not found ...
CSC : warning CS8785: Generator 'OpenApiClientServicesGenerator' failed to generate source. It will not contribute to the output and compilation ...
Read more >
Build fails if SourceGenerator project has a package ...
I have created a git repository to reproduce the issue. It uses the official way of adding dependencies to a source generator project....
Read more >
Source Generators
Source Generators is a C# compiler feature that lets C# developers inspect user code as it is being compiled. Source generators create new ......
Read more >
Using Source Generators with Blazor components in .NET 6
Source generators that were working fine in .NET 5 failed to discover the Blazor components in my app, due to changes in the...
Read more >
I need help with source generators. It stops working without ...
I tried to compile with dotnet build but it generates a warning CS8785 saying that the GeneratorClass can't generate the source and won't ......
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