XamarinCommunityToolkit icon indicating copy to clipboard operation
XamarinCommunityToolkit copied to clipboard

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

Open Siphonophora opened this issue 4 years ago • 9 comments

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

  • Version with issue: 1.1.0
  • Last known good version: Unknown
  • IDE: VS Community 16.9.2
  • Platform Target Frameworks: Sort of N/A since it blocks build of the unit test project only. But this project would has Android and iOS projects.
  • Nuget Packages:
  • Affected Devices: N/A

Workaround

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

Reproduction Link

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

Siphonophora avatar Apr 03 '21 18:04 Siphonophora

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

pictos avatar Apr 05 '21 20:04 pictos

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

Siphonophora avatar Apr 05 '21 21:04 Siphonophora

Same problem here

StepKie avatar Apr 13 '21 12:04 StepKie

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

pictos avatar Apr 13 '21 17:04 pictos

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

Also net5.0

StepKie avatar Apr 13 '21 18:04 StepKie

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

pictos avatar Apr 13 '21 19:04 pictos

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 ...

StepKie avatar Apr 14 '21 08:04 StepKie

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>

TheCodeTraveler avatar May 06 '21 18:05 TheCodeTraveler

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.

vppetrov avatar Apr 07 '22 13:04 vppetrov