XamarinCommunityToolkit icon indicating copy to clipboard operation
XamarinCommunityToolkit copied to clipboard

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

Open cabal95 opened this issue 4 years ago • 26 comments

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.

cabal95 avatar Feb 09 '21 22:02 cabal95

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!

jfversluis avatar Feb 10 '21 14:02 jfversluis

Similar issue here. Build error in my Unit Tests project with TargetFramework netcoreapp3.1.

NETSDK1073: The FrameworkReference 'Microsoft.WindowsDesktop.App' was not recognized

This workaround worked for me: https://github.com/rotorgames/Rg.Plugins.Popup/issues/568#issuecomment-707134962

HeikkiDev avatar Feb 18 '21 15:02 HeikkiDev

Similar issue here. Build error in my Unit Tests project with TargetFramework netcoreapp3.1.

NETSDK1073: The FrameworkReference 'Microsoft.WindowsDesktop.App' was not recognized

This workaround worked for me: rotorgames/Rg.Plugins.Popup#568 (comment)

I have this same issue in any project referencing XCT on Mac. It just won't compile due to the Microsoft.WindowsDesktop.App reference.

akamud avatar Feb 22 '21 12:02 akamud

I have this same issue in any project referencing XCT on Mac. It just won't compile due to the Microsoft.WindowsDesktop.App reference.

@akamud what version of XCT are you running? The any project includes xamarin.forms projects?

pictos avatar Feb 22 '21 12:02 pictos

I have this same issue in any project referencing XCT on Mac. It just won't compile due to the Microsoft.WindowsDesktop.App reference.

@akamud what version of XCT are you running? The any project includes xamarin.forms projects?

Yes, the build fails on my unit test projects and my Xamarin.Forms project referencing XCT. I'm using 1.0.2.

akamud avatar Feb 22 '21 13:02 akamud

~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

TheCodeTraveler avatar Feb 22 '21 23:02 TheCodeTraveler

I recommend closing this issue because it is not a problem in Xamarin.CommunityToolkit, rather it is caused by Xamarin.Forms and then inherited by Xamarin.CommunityToolkit.

Alternatively, @jfversluis or @jsuarezruiz may be able to transfer it to the Xamarin.Forms repo.

Screen Shot 2021-02-22 at 4 01 40 PM

TheCodeTraveler avatar Feb 23 '21 00:02 TheCodeTraveler

I lost my rights to the Forms repo so @jsuarezruiz is now our only hope!

jfversluis avatar Feb 23 '21 08:02 jfversluis

@brminnick Just to be clear, the original issue I posted about ONLY happens if Xamarin Community Toolkit is linked in. If I remove XCT but leave Xam5 in then the warning goes away.

cabal95 avatar Feb 23 '21 15:02 cabal95

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 👍

TheCodeTraveler avatar Feb 23 '21 18:02 TheCodeTraveler

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

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

TheCodeTraveler avatar Feb 23 '21 19:02 TheCodeTraveler

I can't transfer the issue. closing it

StephaneDelcroix avatar Feb 25 '21 19:02 StephaneDelcroix

Please fix this. I run into the same issue. Workaround <GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks> helped.

omghb avatar Mar 01 '21 09:03 omghb

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

Countryen avatar May 25 '21 09:05 Countryen

Has there been an update to this? The workaround everyone has mentioned doesn't work for me.

mgramley avatar Nov 19 '21 20:11 mgramley

@mgramley Ensure your Unit Test project is targeting .NET Core 3.1 and that the Unit Test CSPROJ includes <GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks>.

We do not have a known way to fix this without removing Xamarin Community Toolkit support for WPF.

We are open to pull requests.

TheCodeTraveler avatar Nov 19 '21 20:11 TheCodeTraveler

I already had the GenerateErrorForMissingTargetingPacks flag in my project, it did not resolve the issue for me.

For now, since I only need a single view from the toolkit I will pul the latest source files into my project.

mgramley avatar Nov 19 '21 21:11 mgramley

@brminnick <GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks> and test project targeting .NET Core 3.1 does not work.

pasteusernamehere avatar Mar 17 '22 19:03 pasteusernamehere

This seems to be the root cause for https://github.com/xamarin/XamarinCommunityToolkit/issues/1167 and https://github.com/xamarin/XamarinCommunityToolkit/issues/1482.

vppetrov avatar Apr 07 '22 13:04 vppetrov

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 😅

Axemasta avatar Jun 29 '22 15:06 Axemasta

@jfversluis did you check with the repository owners how they want to untangle the dependency to Xamarin.Forms.Platform.WPF? From my point of view, such a dependency should never have been added. As for now since we move our test projects to net6.0, we had to get rid of XamarinCommunityToolkit. Let me know if we can use it again.

thomasgalliker avatar Aug 10 '22 09:08 thomasgalliker

Any update on this? Sad that we havev to run tests on netcoreapp 3.1. Especially now as net5.0-windows is officially removed from azure images, which forces us to run under netcoreapp 3.1 in Azure Devops.

haavamoa avatar Feb 10 '23 17:02 haavamoa

We're in the same situation as @haavamoa 😫

follesoe avatar Mar 14 '23 13:03 follesoe

Had to remove the community toolkit due to this issue :'(

tNRevan avatar Apr 13 '23 10:04 tNRevan

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.

gtbuchanan avatar May 11 '23 19:05 gtbuchanan

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

softlion avatar Jun 22 '23 15:06 softlion