XamarinCommunityToolkit: [Bug] Unit Test projects now reference Xamarin.Forms.Platform.WPF

Description

I’m not sure if this is a bug per-se, but I couldn’t find any previous discussion of it so thought I would mention it as it doesn’t “feel right”. For our Xamarin Forms project, we have some unit tests that verify some of the business logic works and doesn’t break between various upgrades. These are not UI tests. Just a standard netcoreapp3.1 project type for unit testing.

Today I installed Xamarin Community Toolkit and noticed the following warnings appeared:

warning NU1701: Package 'OpenTK 3.0.1' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETCoreApp,Version=v3.1'. This package may not be fully compatible with your project.

warning NU1701: Package 'OpenTK.GLControl 3.0.1' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETCoreApp,Version=v3.1'. This package may not be fully compatible with your project.

My tests still execute and work fine, but it doesn’t seem right that my unit tests project would now be forced to reference a platform lib. I downloaded the latest code from here and verified that the unit test project here does the same.

Is this actually a bug or something we have to live with due to no other way to differentiate WPF from a “generic non-platform” project?

Steps to Reproduce

  1. Create a shared library and install Xamarin Forms 5.0.0 and Community Toolkit 1.0.2 via NuGet.
  2. Create a unit test library that has a project reference to the first project.
  3. Compile, see warnings when NuGet packages are restored.

Expected Behavior

No reference to a platform I’m not using and is not valid for unit testing.

Actual Behavior

Xamarin.Forms.Platform.WPF gets added as a dependency of my Unit Tests project, which chains to the OpenTK package as well.

Basic Information

  • Version with issue: 1.0.2
  • Last known good version: n/a
  • IDE: Visual Studio 2019 16.8.4 Windows 10
  • Platform Target Frameworks:
    • iOS:
    • Android:
    • UWP:
  • Android Support Library Version:
  • Nuget Packages: Xamarin Forms 5.0.0
  • Affected Devices:

Workaround

None so far.

Reproduction imagery

image

Reproduction Link

Let me know if I need to add one, since the unit tests in XCF exhibit the same behavior.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 9
  • Comments: 26 (10 by maintainers)

Commits related to this issue

Most upvoted comments

I was running into this issue with my net6.0-android sdk-style app project rather than a unit test project. The GenerateErrorForMissingTargetingPacks workaround did not work for me either but I found this one that does:

  <Target BeforeTargets="_CheckForTransitiveWindowsDesktopDependencies" Name="_FixStupidSdkError_NETSDK1136">
    <ItemGroup>
      <TransitiveFrameworkReference Remove="Microsoft.WindowsDesktop.App" />
      <TransitiveFrameworkReference Remove="Microsoft.WindowsDesktop.App.WPF" />
      <TransitiveFrameworkReference Remove="Microsoft.WindowsDesktop.App.WindowsForms" />
    </ItemGroup>
  </Target>

It’s obviously not ideal if you actually develop for these platforms but that wasn’t a problem in my case.

I originally tried something like this to force the use of a different target framework without the WPF dependency but it appears the aforementioned target still runs regardless of the PackageReference.ExcludeAssets attribute.

~This is a bug caused by the Xamarin.Forms NuGet Package.~ Edit: This statement is incorrect. See correction, below

Workaround

The current workaround is to add the following line to the Unit Test CSPROJ file:

<GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks>

Here an example of I implement this workaround in my app GitTrends: https://github.com/brminnick/GitTrends/blob/efe0f6e06c1e22c444b2fa35646797c64e82f235/GitTrends.UnitTests/GitTrends.UnitTests.csproj#L9

Its nice we have a workaround but this doesn’t work for .net5.0 projects and thinking about .net6.0 compatibility when we eventually migrate XF projects to MAUI I’d rather be using a newer .net version than netcoreapp3.1 😅

Apologies, @cabal95 - you are absolutely correct!

This is caused by Xamarin.CommunityToolkit’s dependency on Xamarin.Forms.Platform.WPF for .NETCoreApp 3.1:


I was 100% mistaken.

It turns out Xamarin.Forms.PancakeView has the same problem (it also references Xamarin.Forms.Platform.WPF on .NETCoreApp 3.1), and when I was digging into the root cause of this bug, I mistook Xamarin.Forms.PancakeView for Xamarin.Forms.

I’ll hide my earlier comment to avoid any confusion 👍

Ty @gtbuchanan finally a workaround that fixes it. It was really a pain !

The workaround with <GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks> doesn’t work for my .NET 5 test projects. Also I don’t want to disable all (unknown) errors. You can, however, disable them more specifically, if you include the packages yourself, like this:

Project.Tests.csproj:

<ItemGroup>
    <PackageReference Include="OpenTK" Version="3.0.1">
	<NoWarn>NU1701</NoWarn>
    </PackageReference>
    <PackageReference Include="OpenTK.GLControl" Version="3.0.1">
	<NoWarn>NU1701</NoWarn>
    </PackageReference>
</ItemGroup>

But you should check the status of this issue and remove the workaround as soon as possible (or update the package manually, whenever XCT does).

@jsuarezruiz When you get a chance, could you move this back to Xamarin.CommunityToolkit? (Explaination, above)

I’m so sorry for the mistake! 🤦‍♂️

Hey @cabal95 that is definitely not behavior I would want to force upon anyone.

We need to check our dependencies here. Thanks for letting us know!