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

Implementing a generic interface IFoo<T> in both the .razor.cs and .razor partial classes for a component can lead to build errors. #9837

Closed
1 task done
rorymcd98 opened this issue Jan 18, 2024 · 8 comments · Fixed by dotnet/roslyn#73797
Labels
area-blazor area-compiler Umbrella for all compiler issues

Comments

@rorymcd98
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Hopefully this minimal example is clear. This is a blazor wasm component. There is no error appearing in VS or Rider until we attempt to build with either the net6.0.413 or net8.0.0 sdk.

When implementing a generic (must be generic) interrface on both .razor component and .razor.cs partial class we get a build time error.

Removing either : IFoo<Ti> or @implements IFoo<Ti> it builds.

Expected Behavior

This should build without a problem.

Steps To Reproduce

Minimal example - implement an interface, which accepts at least one generic type, in both .razor and .razor.cs:

Removing either : IFoo<Ti> or @implements IFoo<Ti> it builds.

using System.Collections.Generic;

namespace Path.Components;

internal interface IFoo<T>
{
    
}

public partial class DummyComponent<Ti> : IFoo<Ti>
{
    public static void Foo()
    {
        // This will not work
        var a = new List<DummyComponent<Ti>>();
        //Neither would some generic method our team was using
    }

    // This line would be fine though
    //[Inject] public ILogger<DummyComponent<Ti>> Thing { get; set; } = default!;
}

With

@typeparam Ti

@implements IFoo<Ti>

<h3>DummyComponent</h3>

@code {
    
}

Exceptions (if any)

18>DummyComponent.razor.cs(17,26): Error CS0648 : 'DummyComponent' is a type not supported by the language

.NET Version

6.0.413

Anything else?

.NET SDK (reflecting any global.json):
Version: 6.0.413
Commit: 10710f7d8e

Runtime Environment:
OS Name: Windows
OS Version: 10.0.20348
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\6.0.413\

Host:
Version: 8.0.0-preview.7.23375.6
Architecture: x64
Commit: 65b696cf5e
RID: win-x64

.NET SDKs installed:
6.0.413 [C:\Program Files\dotnet\sdk]
7.0.101 [C:\Program Files\dotnet\sdk]
8.0.100-preview.7.23376.3 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.0-preview.7.23375.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.0-preview.7.23375.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.21 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.23 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.0-preview.7.23376.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
Not set

global.json file:
C:\Users\rory.mcdonald\global.json

Learn more:
https://aka.ms/dotnet/info

Download .NET:
https://aka.ms/dotnet/download

@javiercn javiercn transferred this issue from dotnet/aspnetcore Jan 19, 2024
@ghost ghost added the untriaged label Jan 19, 2024
@davidwengier davidwengier added the area-compiler Umbrella for all compiler issues label Jan 19, 2024
@jjonescz
Copy link
Member

Hi @rorymcd98, thanks for taking the time to report this. However, I am unable to reproduce the problem from the information provided. I have tried your example with both SDK version 8.0.100 and 6.0.413 in a blazorwasm project, it compiles successfully (using dotnet build). Can you share more details? Ideally a self-contained repository with minimal example.

Note: the error CS0648 is something I would not expect razor compiler to produce, it's something produced usually when you reference a wrongly constructed or corrupted DLL, for example.

@rorymcd98
Copy link
Author

rorymcd98 commented Jan 22, 2024

Thanks for the reply @jjonescz, interesting, it must be part of our team's environment then. Several members of our team have reproduced this across Rider and VS. I will try to reproduce it in a fresh project when I get the time.

@ghost ghost removed the untriaged label Jan 23, 2024
@ghost ghost added the no recent activity label Jan 30, 2024
@ghost
Copy link

ghost commented Jan 30, 2024

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.

@ghost ghost closed this as completed Feb 6, 2024
@jjonescz jjonescz closed this as not planned Won't fix, can't repro, duplicate, stale Feb 6, 2024
@richardhauer
Copy link

richardhauer commented Apr 24, 2024

@davidwengier FWIW I can reproduce this problem in VS 17.10.0 Preview 5 on net 8

It appears to be a problem in the editor, though, as we can still compile and run fine. When opening a razor file in the editor the errors display as described. The setup to make this happen is a bit more complicated for me, but I can make it happen at will now.

  1. Create component Widget.razor as follows:
@typeparam TComponent where TComponent : ComponentBase
@typeparam TState where TState : new()
@inherits WidgetBase
@implements IWidget<TComponent, TState>
...
@code {}
  1. Create a code-behind file Widget.razor.cs as follows:
public partial class Widget<TComponent, TState> : WidgetBase, IWidget<TComponent, TState> where TComponent : ComponentBase where TState : new()
{
    ...
}
  1. Create a page that contains the widget, something like this MyPage.razor:
@page "/"

<Widget TComponent="MyPage" TState="MyState" />

For me, if the inheritance instructions appear in both the Widget.razor and Widget.razor.cs files, I will start to see an error CS0648 in the razor editor while MyPage.razor is open. The solution still compiles and runs fine.

To make the error go away, one must remove the inheritance instructions and parameter constraints from the .cs file, so Widget.razor.cs looks as follows:

public partial class Widget<TComponent, TState>
{
    ...
}

The error is no longer displayed and the solution still compiles and runs fine.

As I understand it, one can specify inheritance and generic parameter constraints in multiple partial class files, as long as they match. This does not seem to be the case in the razor editor.

I'm not sure if the issue is the base class, the interface, the generic type params or what, but specifying the inheritance and type parameters in the .razor file is the only way to avoid the errors and still have the app run ... in my case.

@davidwengier davidwengier reopened this Apr 24, 2024
@davidwengier
Copy link
Contributor

FYI @jjonescz

@jjonescz
Copy link
Member

Thanks @richardhauer, with your example I can reproduce even in command line builds, investigating.

@jjonescz
Copy link
Member

jjonescz commented Apr 24, 2024

Looks like it boils down to a difference in nullability contexts, not sure why it gives such an error, probably a bug in Roslyn. (EDIT: Found a bug that matches this: dotnet/roslyn#40538.)

#nullable enable
class C
{
    void M()
    {
        var d = new D<int>(); // error: D<int> is unsupported by the language
    }
}

// user's custom partial declaration
public partial class D<T> : I<T> { }

// this is generated by the razor compiler, interfaces are in `#nullable disable` block
public partial class D<T> :
#nullable disable
    I<T>
{
}

public interface I<T> { }

@rorymcd98
Copy link
Author

@jjonescz Thank you for investigating and finding a fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor area-compiler Umbrella for all compiler issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants