Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to create XUnit Project referencing .NET 8 MAUI #21138

Closed
owenashurst opened this issue Mar 11, 2024 · 8 comments
Closed

Unable to create XUnit Project referencing .NET 8 MAUI #21138

owenashurst opened this issue Mar 11, 2024 · 8 comments
Labels
area-testing Unit tests, device tests platform/android 🤖 platform/iOS 🍎 platform/macOS 🍏 macOS / Mac Catalyst platform/windows 🪟 s/needs-info Issue needs more info from the author s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working

Comments

@owenashurst
Copy link

Description

Whenever I try and create an XUnit project targeting my .NET 8 MAUI project, the IDE shows the following error:

Severity Code Description Project File Line Suppression State Details
Error NU1201 Project MyMauiApp is not compatible with net8.0 (.NETCoreApp,Version=v8.0). Project MyMauiApp supports:

  • net8.0-android34.0 (.NETCoreApp,Version=v8.0)
  • net8.0-ios17.2 (.NETCoreApp,Version=v8.0)
  • net8.0-maccatalyst17.2 (.NETCoreApp,Version=v8.0)
  • net8.0-windows10.0.19041 (.NETCoreApp,Version=v8.0) MyMauiApp.UnitTests C:\dev\MyMauiApp\MyMauiApp .UnitTests\MyMauiApp.UnitTests.csproj 1

Steps to Reproduce

1.) Create a .NET 8 MAUI project
2.) Create an XUnit test project
3.) Reference the .NET MAUI project from the XUnit Project
4.) Observe the error

Link to public reproduction project repository

No response

Version with bug

8.0.6 SR1

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, Windows, macOS, Other (Tizen, Linux, etc. not supported by Microsoft directly)

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

@owenashurst owenashurst added the t/bug Something isn't working label Mar 11, 2024
@drasticactions
Copy link
Contributor

drasticactions commented Mar 12, 2024

This is not a bug with MAUI, the error you're getting is correct for what you're doing. See #17791 (comment)

The XUnit project is net8.0. MAUI Projects are platform projects (net8.0-android;net8.0-ios...). This is unrelated to MAUI, you would see the same error if you tried referencing other platform-specific .NET projects to a non platform specific project. This isn't a bug, it has always and will always do this.

If you want to test platform specific code, you would need an XUnit test runner that would run the code on the platform in question. If you want to test generic cross-platform code and don't need it running on the platform, then you would need to move that into another class library so you could reference it inside the generic XUnit (or whatever .NET Test framework) you have.

You could also change your MAUI csproj to add the target framework

<TargetFrameworks>net8.0;net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->
<OutputType Condition="'$(TargetFramework)' != 'net8.0'">Exe</OutputType>

The key is making sure that the generic code doesn't have the output type of an exe, since it can't be run that way.

@PureWeen @BretJohnson Do you have any other advice?

@jsuarezruiz jsuarezruiz added the area-testing Unit tests, device tests label Mar 12, 2024
@mattleibow mattleibow added the s/needs-info Issue needs more info from the author label Mar 12, 2024
@RoiChen001
Copy link
Collaborator

RoiChen001 commented Mar 13, 2024

Can repro this issue on the latest 17.10 Preview 2.
Untitled picture

@RoiChen001 RoiChen001 added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Mar 13, 2024
@owenashurst
Copy link
Author

This is not a bug with MAUI, the error you're getting is correct for what you're doing. See #17791 (comment)

The XUnit project is net8.0. MAUI Projects are platform projects (net8.0-android;net8.0-ios...). This is unrelated to MAUI, you would see the same error if you tried referencing other platform-specific .NET projects to a non platform specific project. This isn't a bug, it has always and will always do this.

If you want to test platform specific code, you would need an XUnit test runner that would run the code on the platform in question. If you want to test generic cross-platform code and don't need it running on the platform, then you would need to move that into another class library so you could reference it inside the generic XUnit (or whatever .NET Test framework) you have.

You could also change your MAUI csproj to add the target framework

<TargetFrameworks>net8.0;net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->
<OutputType Condition="'$(TargetFramework)' != 'net8.0'">Exe</OutputType>

The key is making sure that the generic code doesn't have the output type of an exe, since it can't be run that way.

@PureWeen @BretJohnson Do you have any other advice?

Thank you. Adding net8.0 to the target frameworks does indeed work and adding a conditional output, however it then breaks visual studio for me. If I use Rider, I'm able to compile successfully and run my tests. The downside is I obviously lose visual studio and its UI for publishing ad hoc builds.

I have however found the command line to publish, so it's a bit of a manual task which I'm willing to do if I can actually test my business logic which is more important.

@PureWeen
Copy link
Member

Can you elaborate on "breaks visual studio"?

@owenashurst
Copy link
Author

owenashurst commented Mar 13, 2024

Can you elaborate on "breaks visual studio"?

Sorry, was a rushed reply.

I get the following errors in the Errors tab which prevents me from building, however Rider is fine.

Project MyProject is not compatible with net8.0 (.NETCoreApp,Version=v8.0). Project MyProject does not support any target frameworks. MyProject .UnitTests C:\dev\MyProject\MyProject .UnitTests\MyProject .UnitTests.csproj

And then several errors which are the same:

Unable to read project information for 'MyProject': The property PackageVersion was expected to have a single value across all target frameworks, but instead had the following values: 0.23, 1.0.0 MyProject C:\dev\MyProject\MyProject\MyProject.csproj

If i then look at the Dependencies in the solution explorer, all of them except net8.0-android shows a yellow triangle. No matter how many times I run Clean and Rebuild, I get the same issues.

I've even tried running dotnet workload restore and I get the same issues.

@mattleibow
Copy link
Member

Duplicate of #17791

@mattleibow mattleibow marked this as a duplicate of #17791 Mar 15, 2024
@PureWeen
Copy link
Member

@owenashurst if you use devkit with vscode do you get the same exception?

@mattleibow
Copy link
Member

This is because the app TFMs are setting a project version property using the ApplicationVersion, however, plain .net 8 does not have this concept. You need to also add a <Version Condition="'$(TargetFramework)' == 'net8.0">0.23</Version>

However, this still does not mean that this is the best way to do testing. It is much better to create a shared project that just has testable items in it - pages, VMs and models. Then make that be multi-targeted.

Both the app and unit test project can then reference this singe shared project.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-testing Unit tests, device tests platform/android 🤖 platform/iOS 🍎 platform/macOS 🍏 macOS / Mac Catalyst platform/windows 🪟 s/needs-info Issue needs more info from the author s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants