Aspnetcore: Visual Studio - Remove compilation errors in .g.cs files

Created on 18 Oct 2018  路  23Comments  路  Source: dotnet/aspnetcore

When a component fails to compile, the error list shows the {componentname}.g.cs files, which are not clickable in VS. Is it possible to hide these files?

Resolved affected-all area-blazor bug severity-minor

Most helpful comment

@elementalpete Check the .cshtml and fix the error, so simple it's embarrassing I didn't think to do it before reading your post :-).

All 23 comments

Unfortunately I don't have an example at hand but I was able to open those .g.cs files a few times and I was thinking that I'm working in my own source code! Very annoying. I'm not sure if it is still possible. I think I have seen this in Blazor 0.5.1 or earlier.

If it helps somehow those files you can find in obj folder.

I have a feeling this is rather a Razor issue rather than being Blazor specific, but not entirely sure.

Can you give an example of an error like this? (What was your code and what was the error) - ideally if you can include the snipped of the generated code (.g.cs) that would be more helpful.

The reason I ask is that the emit #line directives in generated code to remap your code to the original file name and line numbers for debugging and also reporting of errors. If there are cases where this isn't working properly we might be able to fix it.

I've definitely come across these as well, mostly when renaming things in the code-behind component class and/or @functions scope or just generally making the C# side of things have compile errors. I'll make sure to keep a copy next time around.

Can you give an example of an error like this? (What was your code and what was the error) - ideally if you can include the snipped of the _generated_ code (.g.cs) that would be more helpful.

The reason I ask is that the emit #line directives in generated code to remap your code to the original file name and line numbers for debugging and also reporting of errors. If there are cases where this isn't working properly we might be able to fix it.

It doesn't happen all the time so as @ransagy mentioned, I'll try to catch it when it happends again and link it here.

This happens to me quite frequently in a complex project. However, it can be easily reproduced in a project created from the Blazor ASP.NET Core hosted template -

  1. Create a new project from the Blazor (ASP.NET Core hosted) project template
  2. Build, run the application
  3. In VS, close all source code windows
  4. Open WeatherForecast.cs and rename the "Summary" field to "xSummary". You will get the following error in the "Error list" window:

Severity Code Description Project File Line
Error CS1061 'WeatherForecast' does not contain a definition for 'Summary' and no accessible extension method 'Summary' accepting a first argument of type 'WeatherForecast' could be found (are you missing a using directive or an assembly reference?) WebApplication3.Client C:\inetpub\AppRoot\tests\WebApplication3\WebApplication3.Client\obj\Debug\netstandard2.0\Pages\FetchData.g.cs 75

image

My environment: VS 2017 Pro 15.8.8, latest Win 10 Pro, Blazor 0.6.0

@radek7210 that鈥檚 the one!

Ok thanks that's something I can investigate! What I expect is that we're not producing a line-mapping for the usage of WeatherForecast.Summary in the generated code, which is why it's showing up for the wrong file.

Yet another very simple example.

  1. Create a new Blazor project (simple, not hosted, not server).
  2. Open Counter.cshtml file.
  3. Change IncrementCount() function to IncrementCountx().
  4. Save the file. You will see: The name 'IncrementCount' does not exist in the current context" in Counter.cshtml. This is expected.
  5. Press F6 to compile solution. You will see exactly the same error in Counter.g.cs in line 36

Counter.g.cs file
```c#

pragma checksum "D:\Blazor\xxx\xxx\Pages\Counter.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "63d9076174be474b399323b5e7236e6d0c63e775"

//

pragma warning disable 1591

namespace xxx.Pages
{
#line hidden
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Blazor;
using Microsoft.AspNetCore.Blazor.Components;
using System.Net.Http;
using Microsoft.AspNetCore.Blazor.Layouts;
using Microsoft.AspNetCore.Blazor.Routing;
using Microsoft.JSInterop;
using xxx;
using xxx.Shared;
[Microsoft.AspNetCore.Blazor.Layouts.LayoutAttribute(typeof(MainLayout))]

[Microsoft.AspNetCore.Blazor.Components.RouteAttribute("/counter")]
public class Counter : Microsoft.AspNetCore.Blazor.Components.BlazorComponent
{
    #pragma warning disable 1998
    protected override void BuildRenderTree(Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeBuilder builder)
    {
        base.BuildRenderTree(builder);
        builder.AddMarkupContent(0, "<h1>Counter</h1>\n\n");
        builder.OpenElement(1, "p");
        builder.AddContent(2, "Current count: ");
        builder.AddContent(3, currentCount);
        builder.CloseElement();
        builder.AddContent(4, "\n\n");
        builder.OpenElement(5, "button");
        builder.AddAttribute(6, "class", "btn btn-primary");
        builder.AddAttribute(7, "onclick", Microsoft.AspNetCore.Blazor.Components.BindMethods.GetEventHandlerValue<Microsoft.AspNetCore.Blazor.UIMouseEventArgs>(IncrementCount));
        builder.AddContent(8, "Click me");
        builder.CloseElement();
    }
    #pragma warning restore 1998

line 9 "D:\Blazor\xxx\xxx\Pages\Counter.cshtml"

int currentCount = 0;

void IncrementCountx()
{
    currentCount++;
}

line default

line hidden

}

}

pragma warning restore 1591

```

Is there any possible workaround?

Hi.
I am getting the same thing just running the getting started Blazor tutorial on the Microsoft site:
Here's the cli output:
```$ dotnet run
obj\Debug\netstandard2.0\RazorDeclaration\App.razor.g.cs(54,92): error CS0234: The type or namespace name 'RenderTreeBuilder' does not exist in the namespace 'Microsoft.AspNetCore.Components.RenderTree' (are you missing an assembly reference?) [R:\Dev\Blazor\blazordemo\blazordemo.csproj]
obj\Debug\netstandard2.0\RazorDeclaration\Pages\Counter.razor.g.cs(55,92): error CS0234: The type or namespace name 'RenderTreeBuilder' does not exist in the namespace 'Microsoft.AspNetCore.Components.RenderTree' (are you missing an assembly reference?) [R:\Dev\Blazor\blazordemo\blazordemo.csproj]
obj\Debug\netstandard2.0\RazorDeclaration\Shared\MainLayout.razor.g.cs(54,92): error CS0234: The type or namespace name 'RenderTreeBuilder' does not exist in the namespace 'Microsoft.AspNetCore.Components.RenderTree' (are you missing an assembly reference?) [R:\Dev\Blazor\blazordemo\blazordemo.csproj]
obj\Debug\netstandard2.0\RazorDeclaration\Pages\FetchData.razor.g.cs(55,92): error CS0234: The type or namespace name 'RenderTreeBuilder' does not exist in the namespace 'Microsoft.AspNetCore.Components.RenderTree' (are you missing an assembly reference?) [R:\Dev\Blazor\blazordemo\blazordemo.csproj]
obj\Debug\netstandard2.0\RazorDeclaration\Shared\NavMenu.razor.g.cs(54,92): error CS0234: The type or namespace name 'RenderTreeBuilder' does not exist in the namespace 'Microsoft.AspNetCore.Components.RenderTree' (are you missing an assembly reference?) [R:\Dev\Blazor\blazordemo\blazordemo.csproj]
obj\Debug\netstandard2.0\RazorDeclaration\Shared\SurveyPrompt.razor.g.cs(54,92): error CS0234: The type or namespace name 'RenderTreeBuilder' does not exist in the namespace 'Microsoft.AspNetCore.Components.RenderTree' (are you missing an assembly reference?) [R:\Dev\Blazor\blazordemo\blazordemo.csproj]
obj\Debug\netstandard2.0\RazorDeclaration\Pages\Index.razor.g.cs(55,92): error CS0234: The type or namespace name 'RenderTreeBuilder' does not exist in the namespace 'Microsoft.AspNetCore.Components.RenderTree' (are you missing an assembly reference?) [R:\Dev\Blazor\blazordemo\blazordemo.csproj]

The build failed. Fix the build errors and run again.
```

Any ideas as to how to fix the build errors?

This seems to be a general Razor issue - I run into frequently in standard Asp.Net Core MVC projects. In the case of Asp.Net Core MVC, these errors correspond to errors in the corresponding .cshtml file. The solution is to find and fix the error in the .cshtml and do a rebuild or clean and build. Usually, I find that when I open the .cshtml file, there is a red squiggly line showing me where the error is, but it never gets populated to the Error List. So in neilRGS's case, I am guessing that @using Microsoft.AspNetCore.Components (or using a fully-qualified class name for Microsoft.AspNetCore.Components.RenderTree) in the Razor file would fix it, although I don't have an easy way to verify.

The main time that this causes me issues is if I am refactoring code, say by deleting a model file and then deleting the corresponding view, and then attempting to build and run. Even though the .cshtml file has been deleted, and the .cshtml.g.cs file wouldn't have to be recreated, I have run into instances where the .cshtml.g.cs file already exists, it can't find the model file, and so it throws a missing reference error and compilation fails before VS realizes that the .cshtml.g.cs file is no longer relevant. It can be tricky to fix at that point - I've had to cut and paste in old versions of the .cshtml file, "fix the error" by deleting all or most of the file contents, rebuild, and then redelete the .cshtml file to clear the error.

@elementalpete Check the .cshtml and fix the error, so simple it's embarrassing I didn't think to do it before reading your post :-).

Closing as we haven't received any recent customer reports in this area. Presumably this is fixed already.

@NTaylorMullen let me know if this shouldn't be closed yet.

@mkArtakMSFT I am also experiencing this issue.

I changed the name of a class used in the Razor page, and it is now throwing this exception on build. If I manually change the class name in those files, it seems to build correctly.

@mkArtakMSFT

While I think some semblance of the issue might still be present, what I noticed is that the references to class names in .cshtml files (e.g. @model Namespace.Models.EditProfile) are not being refactored when they are changed globally. I'm not sure if that's intentional, or if it's a bug. Nevertheless, I wanted to reply that it doesn't seem to be an issue for me at the moment, before you go down any unnecessary rabbit holes. Thanks.

I've just hit this error - again. A 'solution' I have used before is to close the solution, delete the bin and obj folders and the vs file then reload. This time VS is refusing to clear down 5 errors that are all in one line in a component called Schedule.razor.g.cs. Nothing I do clears this.
Anyone any ideas please?

Success - I've juts cleared it. I noticed it seemed to relate to some css so took a chance and commented out the