Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor Server: Hot reload issue with "code behind" with non-standard extension when run from Visual Studio. #11244

Closed
1 task done
CRidge opened this issue Nov 22, 2024 · 3 comments

Comments

@CRidge
Copy link

CRidge commented Nov 22, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

In a Blazor Server project, we experience that a component which has an abstract base component, fails to hot reload if the "code behind" implements abstract properties from the base class when it has a .razor.g.cs extension instead of a standard .razor.cs extension.

We want to have both a .razor.g.cs and .razor.cs partial class in some cases, as the .razor.g.cs is generated code.

The errors we see, look like this:

[Error] c:\Temp\AbstractPartialBugRepro\AbstractPartialBugRepro\obj\Debug\net9.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Components_BuggedComponent_MyComponent_razor.g.cs (line 75): error CS0534: 'MyComponent' does not implement inherited abstract member 'BaseComponent.ComponentType.get'

If we also have the .razor.cs as a partial class, the error points to that class, and not Components_BuggedComponent_MyComponent_razor.g.cs as in this case, but the wording is the same.

It works just fine on initial start, but as soon as we edit the component, hot reload crashes.

If I rename the file from .razor.g.cs to .razor.cs, all works as expected.

Expected Behavior

Hot reload should find the property defined in MyComponent.razor.g.cs and hot reload should work as intended.

Steps To Reproduce

AbstractPartialBugRepro.zip

Apart from adding the component to Home.razor, all custom code in the repo project is in the BuggedComponent folder. All other files are from the default Blazor Server project.

To begin with, there is an abstract base component class:

public abstract class BaseComponent : ComponentBase
{
    protected abstract MyEnum ComponentType { get; }
}

Then there is a partial class for a component (MyComponent.razor.g.cs):

public partial class MyComponent : BaseComponent
{
    protected override MyEnum ComponentType => MyEnum.MyComponent;
}

Finally there is a MyComponent.razor which defines the GUI for the component (MyComponent.razor):

@inherits BaseComponent

<h3>My Component</h3>

This all works fine when I first start the app (from Visual Studio), but when I make a change in the Razor file, the hot reload fails with:

[Error] c:\Temp\AbstractPartialBugRepro\AbstractPartialBugRepro\obj\Debug\net9.0\Microsoft.CodeAnalysis.Razor.Compiler\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Components_BuggedComponent_MyComponent_razor.g.cs (line 75): error CS0534: 'MyComponent' does not implement inherited abstract member 'BaseComponent.ComponentType.get'

If I stop and start the app, it works again.

If I rename the partial class to MyComponent.razor.cs all works as expected.

If I do "dotnet watch run" it also works as expected.

Exceptions (if any)

No response

.NET Version

9.0.100

Anything else?

dotnet --info
.NET SDK:
Version: 9.0.100
Commit: 59db016f11
Workload version: 9.0.100-manifests.3068a692
MSBuild version: 17.12.7+5b8665660

Runtime Environment:
OS Name: Windows
OS Version: 10.0.26120
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\9.0.100\

Visual Studio version info:

Image

@javiercn javiercn assigned tmat and unassigned tmat Nov 22, 2024
@javiercn javiercn transferred this issue from dotnet/aspnetcore Nov 22, 2024
@phil-allen-msft
Copy link
Contributor

@CRidge , it sounds like you have a file ending in .g.cs, which matches what is in the zip file. There is definitely some code in tooling that assumes .g.cs files are generated; what is your goal in choosing that as the name for an on-disk file in your project?

@CRidge
Copy link
Author

CRidge commented Nov 26, 2024

@phil-allen-msft We're just separating code we generate from code the devs can edit. We're not using source generation - just plain old save-text-to-file in a custom tool. So if it is the specific .g.cs which is the problem, it would be no problem switching to another variant like .gen.cs or something.

@CRidge
Copy link
Author

CRidge commented Nov 26, 2024

@phil-allen-msft I changed the naming convention to .gen.cs and now I'm not able to reproduce the behavior I saw with .g.cs 👍 Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants