How to set OS condition for TargetFramework in MSBuild

Viewed 233

I have few projects in my solution - few for mobile (Xamarin.Forms) and one xUnit. xUnit is build with .NET5 and references mobile project.

There are 2 pipelines on Azure DevOps to build mobile app. One is for Android and runs on Windows, another for iOS and runs on OSX.

xUnit project build started to fail after I added new NuGet to mobile project (Rg.Plugins.Popup).

Initially, setting the TargetFramework for xUnit project looked like this

<PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <IsPackable>false</IsPackable>
</PropertyGroup>

That setup worked on OSX, but did not work on Windows with error message 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.

Changing it to

<PropertyGroup>
    <TargetFramework>net5.0-windows</TargetFramework>
    <IsPackable>false</IsPackable>
</PropertyGroup>

fixes Windows problem, but it starts failing on OSX (with same error message, which is weird, as we are no longer on Windows).

I tried few other options, but could not make it to succeed on both machines.

<PropertyGroup>
    <TargetFramework Condition=" '$(OS)' == 'Windows_NT' ">net5.0-windows</TargetFramework>
    <TargetFramework Condition=" '$(OS)' != 'Windows_NT' ">net5.0</TargetFramework>
    <IsPackable>false</IsPackable>
</PropertyGroup>
<PropertyGroup>
    <TargetFrameworks>net5.0;net5.0-windows</TargetFrameworks>
    <IsPackable>false</IsPackable>
</PropertyGroup>

I also tried with Choose, When and Otherwise, but it looks like you cannot wrap TargetFramework into that at all.

Am I doing it completely wrong or it it just some coincidence that correct code does not work in my case?

1 Answers

Try to place Condition inside PropertyGroup not TargetFramework .

Try the code below


<PropertyGroup Condition=" '$(OS)' != 'Windows_NT' ">
    <TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' "> 
    <TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
</PropertyGroup>
Related