xamarin / XamarinCommunityToolkit

The Xamarin Community Toolkit is a collection of Animations, Behaviors, Converters, and Effects for mobile development with Xamarin.Forms. It simplifies and demonstrates common developer tasks building iOS, Android, and UWP apps with Xamarin.Forms.
MIT License
1.59k stars 474 forks source link

[Bug] Unit Test Compile Error When Targeting .NET 5.0 #1167

Open Siphonophora opened 3 years ago

Siphonophora commented 3 years ago

Description

When adding XCTK to a fresh shell project (so no UWP support), an existing unit test project will fail to build. This seems like it might be the same underlying issue as #985 but I'm getting a build failure instead of a warning.

Steps to Reproduce

See sample repo for steps: https://github.com/Siphonophora/XCTC_TargetWindowsBug/commits/main

Expected Behavior

No error.

Actual Behavior

Fails to build.

Error NETSDK1136 The target platform must be set to Windows (usually by including '-windows' in the TargetFramework property) when using Windows Forms or WPF, or referencing projects or packages that do so. TestProject1 C:\Program Files\dotnet\sdk\5.0.201\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.targets 362 NETSdkError

Basic Information

Workaround

Changing the test project from .Net 5 to netCore 3.1 allows it to build.

Reproduction Link

https://github.com/Siphonophora/XCTC_TargetWindowsBug/

pictos commented 3 years ago

@Siphonophora if your unit test target the netCore version, does this error happens?

Siphonophora commented 3 years ago

Apparently netCore 3.1 builds fine. Added that to the repo link

StepKie commented 3 years ago

Same problem here

pictos commented 3 years ago

@Hottemax what's the dotnet version that you're using?

StepKie commented 3 years ago

@Hottemax what's the dotnet version that you're using?

Also net5.0

pictos commented 3 years ago

Can you try netCore and see if it works for you?

StepKie commented 3 years ago

Can you try netCore and see if it works for you?

Well, I am using C#9 language features (record etc), so I can't easily use .NET Core 3.1 ...

brminnick commented 3 years ago

This is a known bug with targeting Windows from a .NET 5.0 project.

The solution is to use .NET Core 3.1 (or lower) for the Unit Test project and set GenerateErrorForMissingTargetingPacks to false in the Unit Test CSPROJ file.

https://stackoverflow.com/a/65161916/5953643

<PropertyGroup>
  <TargetFramework>netcoreapp3.1</TargetFramework>
  <GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks>
</PropertyGroup>
vppetrov commented 2 years ago

Same issue exists with .net 6.0. The cause seems to be that XCTK references a Windows only library OpenTK through Xamarin.CommunityToolkit -> Xamarin.Forms.Platform.WPF -> OpenTK.GLControl -> OpenTK. Explained in detail in https://github.com/xamarin/XamarinCommunityToolkit/issues/985.