novotnyllc / MSBuildSdkExtras

Extra properties for MSBuild SDK projects
MIT License
348 stars 42 forks source link

.NET 5 / C# 9 multi targeting on macOS issue #255

Closed mfkl closed 3 years ago

mfkl commented 3 years ago

Hi,

Given

<Project Sdk="MSBuild.Sdk.Extras">

  <PropertyGroup>

    <TargetFrameworks>net5.0;xamarin.ios10</TargetFrameworks>
    <LangVersion>9.0</LangVersion>
  </PropertyGroup>

</Project>

and

{
    "msbuild-sdks": {
        "MSBuild.Sdk.Extras": "3.0.23"
    },
    "sdk": {
        "version": "5.0.101"
    }
}

Invoking msbuild gives

CSC : error CS1617: Invalid option '9.0' for /langversion. Use '/langversion:?' to list supported values
(_CheckForUnsupportedNETCoreVersion target) ->
  /Library/Frameworks/Mono.framework/Versions/6.12.0/lib/mono/msbuild/Current/bin/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.TargetFrameworkInference.targets(127,5): error NETSDK1045: The current .NET SDK does not support targeting .NET Core 5.0.  Either target .NET Core 3.1 or lower, or use a version of the .NET SDK that supports .NET Core 5.0.

It builds fine on Windows, interestingly. I've made sure it builds fine on macOS with a single target and without MSBuildSdkExtras.

repro: https://github.com/mfkl/test-net5-mac-multitarget

clairernovotny commented 3 years ago

Based on the error, it looks like you need a newer version of Mono?

mfkl commented 3 years ago

Apologies, you are correct. C# 9 is supported in yesterday's nightly mono build, but not in the current stable / VS version.

mfkl commented 3 years ago

Building C# 9 single target works, but with your SDK and multitarget it gives

(CoreCompile target) ->
  CSC : error CS1900: Warning level must be in the range 0-4

Your SDK is likely not at fault here, just FYI.

jmarolf commented 3 years ago

CSC : error CS1900: Warning level must be in the range 0-4

This error means you are trying to build a net5.0 with an older (pre C# 9) compiler. You can manually specify <WarningLevel>4</WarningLevel> in your project file to make it go away but some C# 9 features may not work.

mfkl commented 3 years ago

csc -langversion:? does return a list that includes C# 9. csc --version returns 3.7.0-5.20367.1 which should include full C# 9 support. I think this is a bug and I can't think of another place to open an issue as the default SDK with single target builds the same csproj fine.

https://github.com/mfkl/mac-multitarget-net5-cs9

mfkl commented 3 years ago

You can manually specify <WarningLevel>4</WarningLevel> in your project file to make it go away

Interestingly, specifying this in the csproj seems to get ignored. Building with msbuild. @jmarolf

jmarolf commented 3 years ago

@mfkl I can no longer access your repro project. If properties you set in your project file do not get picked up you have a very broken build environment. I do not know if that is due to msbuildsdkextras, mono, the .NET sdk, or some combination of all three.

If you feel the issue lies with the sdk I would open a new issue here

If you feel this is an issue with mono's build environment I would open a new issues here

mfkl commented 3 years ago

@jmarolf https://github.com/mfkl/mac-multitarget-net5-cs9. Thanks, will open an issue at mono.

jmarolf commented 3 years ago

quick question: do you actually need MSBuildSDKExtras? Does this not work for you?

<TargetFrameworks>net5.0;net5.0-ios</TargetFrameworks>
clairernovotny commented 3 years ago

IOS currently requires the Extras. This is something that .NET 6 should be addressing.

jmarolf commented 3 years ago

gotcha, thanks!