Sergio0694/PolySharp

C# 12 need net8 sdk to be installed in Ubuntu 22.04, but success in Windows using visual studio 2022.

moh-hassan opened this issue · 1 comments

A multi target project net6, net472 with c# 12 is developed to support both windows and Linux.
<LangVersion>12</LangVersion>

i use global.json that allow sdk6:

{
  "sdk": {
    "version": "6.0.419",
    "rollForward": "latestFeature"
  }
}

When the project is built in windows 11 using visual studio v17.9.2 , it is compiled without errors.

Note: visual studio 2022 v 17.9.2 auto install net8 (that support c# 12). So it is expected to success compilation

When the project is built in In Ubuntu 22.04 (with Mono 6 installed), using the command dotnet build , the compiler raise error:

CSC : error CS1617: Invalid option '12' for /langversion. Use '/langversion:?' to list supported values. 

Resolving the issue:

I had to install net8 sdk (8.0.200) with runtime in Ubuntu 22.04 to build the project and modify the global.json.
Then rebuild. The build success without errors:

{
  "sdk": {
    "version": "8.0.200",
    "rollForward": "latestFeature"
  }
}

It seems that c# 12 support need net8 to be installed in UbUntu 22.04. (or windows)

I suggest, this limitation can be documented.

This issue raised an important point for supporting c# features for old framework:
The dotnet SDK version that support the c# langVer in csproj file should be installed

it means:

for c# 12, net8 SDK
for c# 10, net6 SDK

For complete reference, see C# language versioning Targeting and support rules

When using Visual studio 2022, there are typically more copies of compilers are installed:

  1. One is at C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn.

This copy is used by Visual Studio, and contains .NET Framework entry points.
In every vs2022 , the corresponding dotnet SDK is auto installed (in my case net8 with c# 12 support.

  1. The other one (for net6) is at C:\Program Files\dotnet\sdk\6.0.400\Roslyn\bincore.
    This one is used from command line. It contains .NET Core entry points. When executing, it's actually doing dotnet.exe csc.dll.
    This compilere support at max c# 10
  2. The third one (for net8) is at C:\Program Files\dotnet\sdk\8.0.200\Roslyn\bincore.
    This compilere support at max c# 12
    We can find by command:
>where csc
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\csc.exe
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe

The workflow of how the .NET 6.0 CLI builds a C# assembly is as follows:

a) dotnet run calls msbuild
b) msbuild calls csc
c) csc compiles the sources files, using the necessary references.

Conclusion
To support c# 12 feature , we should:

  1. Configure language <LangVersion>12</LangVersion>
  2. install the corresponding dotnet SDK that support the language. e,g net 8 for c# 12