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
491 stars 190 forks source link

Microsoft.NET.Sdk.Razor.SourceGenerators can't find interface when building MAUI Hybrid app with generic component using the interface as type constraint #7066

Closed bit-wrangler closed 1 year ago

bit-wrangler commented 2 years ago

Is there an existing issue for this?

Describe the bug

Project fails to build Razor pages/components with Razor component when building a MAUI hybrid project for a net6.0-android target. (EDIT: Issue doesn't seem to be android target specific, android was just the first target built. I tried all targets and they all failed.)

The issue seems to be related to having an interface constraint on a generic Razor component's @typeparam.

Expected Behavior

Project builds successfully.

Steps To Reproduce

Reference project is attached below for reference.

  1. Create a MAUI Blazor hybrid project.
  2. Create an interface ITest. It can be blank.
  3. Create an implementation of the interface class Test : ITest, it can also be blank.
  4. Create a generic Razor component with @typeparam TItem where TItem : ITest and add a component parameter that uses TItem.
  5. Use the generic component in another Razor page/component.
  6. IDE will show no errors while editing, but will fail to build on the net6.0-android target.

android-interface-test.zip The reference

Exceptions (if any)

The error I get at build time:

Severity    Code    Description Project File    Line    Suppression State
Error   CS0246  The type or namespace name 'IDataItem' could not be found (are you missing a using directive or an assembly reference?) AndroidInterfaceTest (net6.0-android)   G:\projects\android-interface-test\AndroidInterfaceTest\AndroidInterfaceTest\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Pages_Index_razor.g.cs  125 Active

.NET Version

6.0.400

Anything else?

Microsoft Visual Studio Community 2022 Version 17.3.3 VisualStudio.17.Release/17.3.3+32825.248 Microsoft .NET Framework Version 4.8.04084

Installed Version: Community

Visual C++ 2022 00482-90000-00000-AA565 Microsoft Visual C++ 2022

Add Multiple Projects To Solution V2 2.0.0 Visual Studio extension that allows adding multiple existing projects into the solution. Optionally creates the solution folder structure reflecting to the physical hierarchy on disk.

ADL Tools Service Provider 1.0 This package contains services used by Data Lake tools

ASA Service Provider 1.0

ASP.NET and Web Tools 17.3.376.3011 ASP.NET and Web Tools

Azure App Service Tools v3.0.0 17.3.376.3011 Azure App Service Tools v3.0.0

Azure Data Lake Tools for Visual Studio 2.6.5000.0 Microsoft Azure Data Lake Tools for Visual Studio

Azure Functions and Web Jobs Tools 17.3.376.3011 Azure Functions and Web Jobs Tools

Azure Stream Analytics Tools for Visual Studio 2.6.5000.0 Microsoft Azure Stream Analytics Tools for Visual Studio

C# Tools 4.3.0-3.22423.10+b35bb0baca1071d50cc4f8cf5a1a89e7cc112461 C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Extensibility Message Bus 1.2.6 (master@34d6af2) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

Microsoft Azure Hive Query Language Service 2.6.5000.0 Language service for Hive query

Microsoft Azure Stream Analytics Language Service 2.6.5000.0 Language service for Azure Stream Analytics

Microsoft Azure Tools for Visual Studio 2.9 Support for Azure Cloud Services projects

Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Mono Debugging for Visual Studio 17.3.20 (3f4cb00) Support for debugging Mono processes with Visual Studio.

NCrunch
Continuous Testing Tool for .NET Copyright � 2010-2022 Remco Software Ltd

NuGet Package Manager 6.3.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

Razor (ASP.NET Core) 17.0.0.2232702+e1d654e792aa2fe6646a6935bcca80ff0aff4387 Provides languages services for ASP.NET Core Razor.

SQL Server Data Tools 17.0.62207.04100 Microsoft SQL Server Data Tools

Syncfusion ASP.NET Core Project Templates 20.1.0.47 Provides project templates for creating an ASP.NET Core Web Application using Syncfusion ASP.NET Core components.

Syncfusion ASP.NET Core Project Templates 16.1.0.32 Provides project templates for creating an ASP.NET Core Web Application using Syncfusion ASP.NET Core components.

Syncfusion Xamarin Project Templates 20.2.0.43 A multi-project template for building iOS, Android, and Windows apps using Syncfusion Xamarin components.

Syncfusion Xamarin Toolbox 20.2.0.43 Syncfusion Essential Studio for Xamarin Toolbox Visual Studio extension.

SyncfusionCodeGeneratorMenuCommand Extension 16.1.0.32 SyncfusionCodeGeneratorMenuCommand Visual Studio Extension Detailed Info

SyncfusionMenu Extension 20.2.0.43 SyncfusionMenu Visual Studio Extension Detailed Info

Syntax Visualizer 1.0 An extension for visualizing Roslyn SyntaxTrees.

Test Adapter for Boost.Test 1.0 Enables Visual Studio's testing tools with unit tests written for Boost.Test. The use terms and Third Party Notices are available in the extension installation directory.

Test Adapter for Google Test 1.0 Enables Visual Studio's testing tools with unit tests written for Google Test. The use terms and Third Party Notices are available in the extension installation directory.

ToolWindowHostedEditor 1.0 Hosting json editor into a tool window

TypeScript Tools 17.0.10701.2001 TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools 4.3.0-3.22423.10+b35bb0baca1071d50cc4f8cf5a1a89e7cc112461 Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools 17.1.0-beta.22363.4+1b94f89d4d1f41f20f9be73c76f4b229d4e49078 Microsoft Visual F# Tools

Visual Studio IntelliCode 2.2 AI-assisted development for Visual Studio.

Visual Studio Tools for Unity 17.3.0.0 Visual Studio Tools for Unity

VisualStudio.DeviceLog 1.0 Information about my package

VisualStudio.Mac 1.0 Mac Extension for Visual Studio

Web Compiler 1.14.8 Compiler for LESS, Sass and CoffeeScript files

WiX Toolset Visual Studio Extension 1.0.0.18 WiX Toolset Visual Studio Extension version 1.0.0.18 Copyright (c) .NET Foundation and contributors. All rights reserved.

Xamarin 17.3.0.296 (d17-3@a0ff24b) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer 17.3.0.208 (remotes/origin/d17-3@e4f67afef) Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin.Android SDK 13.0.0.0 (d17-3/030cd63) Xamarin.Android Reference Assemblies and MSBuild support. Mono: dffa5ab Java.Interop: xamarin/java.interop/d17-3@7716ae53 SQLite: xamarin/sqlite/3.38.5@df4deab Xamarin.Android Tools: xamarin/xamarin-android-tools/main@14076a6

bit-wrangler commented 2 years ago

I also tried putting the type constraint in a separate file with partial class public partial class MyComponent<TItem> where TItem : ITest and that fails to build as well.

bit-wrangler commented 2 years ago

I must have searched for the wrong text the first time around, but I found this issue dotnet/aspnetcore#37295 that was marked as "resolution: by design" and in the comments the fix was to explicitly specify the generic type in component usages. <MyComponent Item="item" TItem="IItem"/>. Documentation (https://docs.microsoft.com/en-us/aspnet/core/blazor/components/?view=aspnetcore-6.0#generic-type-parameter-support) could be a little clearer IMO as to the requirement of specifying the type parameter - language is somewhat observational rather than instructional:

The following GenericTypeExample1 component renders two ListGenericTypeItems1 components:
- String or integer data is assigned to the ExampleList parameter of each component.
- Type string or int that matches the type of the assigned data is set for the type parameter (TExample) of each component.

IMO, it could have been worded more along the lines of "When passing a parameter to a component that has a generic parameter, you must also pass the type parameter" but that's just me.

javiercn commented 2 years ago

@bit-wrangler thanks for contacting us.

I think this is something that we might have fixed in .NET 7.0, I would recommend you check that.

Hunter1pro commented 2 years ago

@bit-wrangler I'm checked your project, just have some issue, in you case you are forgot set type to component: (screenshop) изображение

Upd: Bellow not your problem but also can be: Check your namespacess its regular problem in razor on Blazor, if you have some things on Project.Data and you are store razor component on Project.Component.Data (fix to Project.Component.DataComp for example), its will be conflicted. Try change folder by recreation of razorComponent on new folder (not copy/move) and try use unique end namespace.

Also sometimes even names has sense if you use it on RazorComponent Example wrong namespace: class Data : IData razor component: Data

Example Right namespace and naming: class: Data:IData razor component: DataComponent

chsienki commented 1 year ago

Closing this as by-design. Feel free to comment further if you disagree. Thanks!