dotnet / razor

Compiler and tooling experience for Razor ASP.NET Core apps in Visual Studio, Visual Studio for Mac, and VS Code.
https://asp.net
MIT License
498 stars 191 forks source link

Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator throws a lot of errors while compiling a cshtml file #10935

Open wattengard opened 3 weeks ago

wattengard commented 3 weeks ago

I seem to be hitting the same issue as https://github.com/dotnet/razor/issues/10186 I am maintaining an old internal application so I was lifting it to latest .NET 8 (8.0.402 as of yesterday when I was trying this) and ran in to this error, it would not compile.

C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1964,87): error CS0116: A namespace cannot directly contain members such as fields, methods or statements [C:\Code\Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1968,60): error CS0116: A namespace cannot directly contain members such as fields, methods or statements [C:\Code\Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1972,70): error CS0116: A namespace cannot directly contain members such as fields, methods or statements [C:\Code\Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1976,71): error CS0116: A namespace cannot directly contain members such as fields, methods or statements [C:\Code\Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1980,71): error CS0246: The type or namespace name 'MalInfoViewModel' could not be found (are you missing a using directive or an assembly reference?) [C:\Code\Avonova\Mak
esense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1718,86): error CS0246: The type or namespace name '__tagHelperAttribute_2' could not be found (are you missing a using directive or an assembly reference?) [C:\Code\Avono
va\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1721,82): error CS0246: The type or namespace name '__tagHelperAttribute_46' could not be found (are you missing a using directive or an assembly reference?) [C:\Code\Avon
ova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1723,13): error CS1520: Method must have a return type [C:\Code\Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1723,13): error CS0501: '<invalid-global-code>.<invalid-global-code>(__tagHelperExecutionContext, ?, ?, HtmlAttributeValueStyle.DoubleQuotes)' must declare a body because
it is not marked abstract, extern, or partial [C:\Code\Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1724,13): error CS1520: Method must have a return type [C:\Code\Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1724,13): error CS0501: '<invalid-global-code>.<invalid-global-code>(?, ?, Model.Mal.EgendefinerteSamtykker, ?, ?, ?)' must declare a body because it is not marked abstrac
t, extern, or partial [C:\Code\Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1733,13): error CS1520: Method must have a return type [C:\Code\Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1733,13): error CS0501: '<invalid-global-code>.<invalid-global-code>(__tagHelperExecutionContext)' must declare a body because it is not marked abstract, extern, or partia
l [C:\Code\Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1736,27): error CS0106: The modifier 'new' is not valid for this item [C:\Code\Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1736,27): error CS1520: Method must have a return type [C:\Code\Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1723,41): error CS0246: The type or namespace name '__tagHelperExecutionContext' could not be found (are you missing a using directive or an assembly reference?) [C:\Code\
Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1723,154): error CS0426: The type name 'DoubleQuotes' does not exist in the type 'HtmlAttributeValueStyle' [C:\Code\Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1736,53): error CS0246: The type or namespace name 'InvalidTagHelperIndexerAssignment' could not be found (are you missing a using directive or an assembly reference?) [C:
\Code\Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1718,86): error CS0538: '__tagHelperAttribute_2' in explicit interface declaration is not an interface [C:\Code\Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1718,109): error CS0501: '<invalid-global-code>.Value()' must declare a body because it is not marked abstract, extern, or partial [C:\Code\Avonova\Makesense\Makesense\Mak
esense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1721,82): error CS0538: '__tagHelperAttribute_46' in explicit interface declaration is not an interface [C:\Code\Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1721,106): error CS0501: '<invalid-global-code>.Value()' must declare a body because it is not marked abstract, extern, or partial [C:\Code\Avonova\Makesense\Makesense\Mak
esense.csproj]
C:\Code\Avonova\Makesense\Makesense\Areas\Undersøkelse\Views\Mal\_MalInfo.cshtml(294,37): error CS0246: The type or namespace name 'Model' could not be found (are you missing a using dire
ctive or an assembly reference?) [C:\Code\Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1733,39): error CS0246: The type or namespace name '__tagHelperExecutionContext' could not be found (are you missing a using directive or an assembly reference?) [C:\Code\
Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1682,17): error CS1520: Method must have a return type [C:\Code\Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1682,17): error CS0501: 'Areas_Undersøkelse_Views_Mal__MalInfo.Areas_Undersøkelse_Views_Mal__MalInfo()' must declare a body because it is not marked abstract, extern, or p
artial [C:\Code\Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1698,17): error CS1520: Method must have a return type [C:\Code\Avonova\Makesense\Makesense\Makesense.csproj]
C:\Code\Avonova\Makesense\Makesense\obj\Debug\net8.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Areas_Undersøkelse_Views_Mal__MalI
nfo_cshtml.g.cs(1698,17): error CS0501: 'Areas_Undersøkelse_Views_Mal__MalInfo.Areas_Undersøkelse_Views_Mal__MalInfo()' must declare a body because it is not marked abstract, extern, or p
artial [C:\Code\Avonova\Makesense\Makesense\Makesense.csproj]

Works: 8.0.108 [C:\Program Files\dotnet\sdk] Throws: 8.0.400 [C:\Program Files\dotnet\sdk] Throws: 8.0.402 [C:\Program Files\dotnet\sdk] Throws: 9.0.100-rc.1.24452.12 [C:\Program Files\dotnet\sdk]

I had to go back to 8.0.108 to make it work again.

This also happens in VS2022, both latest regular (17.11.4) and latest preview. It works in VS also when I use global.json to force it to 8.0.108.

When opening the _MalInfo.cshtml file in Visual studio it doesn't show any errors in the editor itself. Syntax highlighting works properly etc. But the tooltip shows all errors in the compiler output if global.json sdk is set to one of the above with error.

It's not critical since I can get around it by using 8.0.108, but I wanted to report it anyway.

The software is proprietary so I'm not sure if I'm able to post the cshtml file itself, but I'll check into it? Is there something I should look for to try to identify what triggers it? The file itself uses both Microsoft taghelpers (asp-for et al) and my own taghelpers. But there should be nothing special in exactly this file that is not used other places in the solution.

Machine info: Windows 11, 23H2, 22631.4169

❯ dotnet --list-sdks
6.0.425 [C:\Program Files\dotnet\sdk]
7.0.410 [C:\Program Files\dotnet\sdk]
8.0.108 [C:\Program Files\dotnet\sdk]
8.0.400 [C:\Program Files\dotnet\sdk]
8.0.402 [C:\Program Files\dotnet\sdk]
9.0.100-rc.1.24452.12 [C:\Program Files\dotnet\sdk]
❯ dotnet --list-runtimes
Microsoft.AspNetCore.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.0-rc.1.24452.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.0-rc.1.24431.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.0-rc.1.24452.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
jjonescz commented 2 weeks ago

Is there something I should look for to try to identify what triggers it?

The location of the errors in the generated C# code should hint where the error is in the razor file. You can also try commenting out pieces of the razor code until you figure out which line is the culprit.

We have fixed a couple of issues around @ and attributes in razor recently. Usually there were syntax errors in user code that the razor compiler incorrectly ignored. Some fixes also changed how pragmas (#) behave - in general they are not supported in razor, so make sure you don't have any #region, #pragmas, etc.

dotnet-policy-service[bot] commented 1 week ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 7 days. It will be closed if no further activity occurs within 7 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.

Please see our Issue Management Policies for more information.

ebaby-elsevier commented 4 days ago

I am also getting this same issue after upgrading from dotnet core 6 to 8.

in my case its happening when i use component as tag helpers from some other project under the same solution.

jjonescz commented 4 days ago

in my case its happening when i use component as tag helpers from some other project under the same solution.

Can you share your source code or create a minimal reproducible example we could investigate?