XamarinCommunityToolkit
XamarinCommunityToolkit copied to clipboard
[Bug] Unit Test Compile Error When Targeting .NET 5.0
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 if your unit test target the netCore version, does this error happens?
Apparently netCore 3.1 builds fine. Added that to the repo link
Same problem here
@Hottemax what's the dotnet version that you're using?
@Hottemax what's the dotnet version that you're using?
Also net5.0
Can you try netCore and see if it works for you?
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 ...
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>
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.