3

We have a shared .props file throughout a large .NET solution (OSS project) that ships primarily C# libraries with some F# APIs built on top of them. I want to globally set the C# version through the LangVersion property to use C# 10.0 or 11.0, but when I do this it breaks compilation of my F# projects that also depend on the same .props file.

Using separate .props files for each isn't a great option as that .props file is also what's responsible for populating version / package metadata and we want to keep that in one place in order to eliminate duplicative-type errors.

Therefore, what I'm trying to do is use a conditional property inside our .props file that looks like this:


  <!-- Set the language version for C# if we're not inside an F# project -->
    <PropertyGroup Condition=" '$(Language)' == 'CSharp' ">
         <LangVersion>10.0</LangVersion>
    </PropertyGroup>

However, I'm not sure what the right variable / output is to check this. I've gone through the lists mentioned in List of MSBuild built-in variables and have not been able to find it there.

Aaronontheweb
  • 8,224
  • 6
  • 32
  • 61
  • Looks like there is a `Language` property but I can't tell if this has made it into a current version of MSBuild or what its values are https://github.com/dotnet/msbuild/issues/2101 – Aaronontheweb Feb 22 '23 at 16:01
  • 2
    `Language` property is set by `Microsoft..targets` which is imported after `Directory.Build.props` and after project file. It can be used, but only in targets – JL0PD Feb 23 '23 at 01:42

2 Answers2

3

Untested, but perhaps:

Condition=" '$(MSBuildProjectExtension)' == '.csproj' "

Taking MSBuildProjectExtension from here

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • that's a good idea - I'll give that a try – Aaronontheweb Feb 22 '23 at 16:08
  • @Aaronontheweb great! I could have used this a bunch of times myself, but in the end I usually cheat and just go with `latest` plus a `global.json` that sets the min SDK version to something *that has* the language version I want – Marc Gravell Feb 22 '23 at 16:36
2

The Language property is supposed to exist for this purpose. I know C# and VB do, and we set it in the WiX Toolset.

I've also used a trick for language-specific .props by adding the following to the bottom of Directory.Build.props

<Import Project="Directory$(MSBuildProjectExtension).props"
        Condition=" Exists('Directory$(MSBuildProjectExtension).props') " />

Then I add any C# specific content to Directory.csproj.props next to the Directory.Build.props as needed. It scales quite nicely.

Rob Mensching
  • 33,834
  • 5
  • 90
  • 130