How do I set csc langversion when building in VSTS?
Asked Answered
C

2

9

I have a simple test console app. The only code it has is

public class Class1
{
    private protected int _value = 0b_1001_0110;
}

which is just to test whether these C# 7.2 features will build or not.

In Visual Studio 2017 15.5.2 they build fine if I set the language version like this

Advanced build settings

If I now check the project into VSTS and run a build it fails with various errors, e.g.

Test\CSharp72Test\CSharp72Test\Class1.cs(7,17): Error CS0107: More than one protection modifier
Test\CSharp72Test\CSharp72Test\Class1.cs(7,40): Error CS1013: Invalid number

because it knows nothing about C#7.

If I now add 'Microsoft.NET.Compilers 2.6.1' NuGet package and rebuild it works fine locally but fails in VSTS with

Test\CSharp72Test\CSharp72Test\Class1.cs(7,40): Error CS8107: Feature 'leading digit separator' is not available in C# 7.0. Please use language version 7.2 or greater.

It's now using the correct version of CSC.exe but not passing /langversion:latest.

How do I get this passed correctly per project in a multi project solution?

Crandell answered 1/1, 2018 at 15:1 Comment(3)
Are you using the hosted build queue or do you manage your own build queue? It sounds like your build agent(s) don't have the VS 2017 updates installed.Fierce
The hosted one. Using the NuGet compiler package would work if the langversion was being set.Crandell
On a build agent machine, there can be multiple MSBuild versions, blog.lextudio.com/the-rough-history-of-msbuild-cc72a217fa98 So merely setting the language version in your project file is useless, and you must configure the build agent (your pipeline definition) to pick up the right the right MSBuild version. What I did for Obfuscar is to call the proper PowerShell cmdlet to help github.com/obfuscar/obfuscar/blob/master/release.ps1Phaeton
N
9

Visual Studio Build task is also calling MSBuild.exe command to run the build.

You could directly pass MSBuild Argument and build succeed through the command line locally such as below:

msbuild "C:\Users\Admin\Source\repos\ClassLibrary2\ClassLibrary2\ClassLibrary2.csproj" /property:langversion=latest 

So you just need to add /property:langversion=latest in MsBuild Argument of the Visual Studio Build task in VSTS or directly change the value of <LangVersion>xx</LangVersion> to latest in the project file.

However, according to VSTS Hosted VS2017 image, the host agent only have Visual Studio 2017 Enterprise * Version: 15.4.0 installed.

C# 7.2 ships with the 15.5 release of Visual Studio 2017.

There also have been a related user voice : Hosted agent with VS 15.5 to enable C# 7.2 support

As a workaround, you could set up your owner build agent and with Visual Studio 2017 15.5.2 installed.

Newspaper answered 2/1, 2018 at 3:36 Comment(3)
1) Setting the language version in the advanced build settings updates the project file. 2) The issue is that the langversion is used when building with VS, but isn't used by MSBuild. So updating the hosted version won't fix the problem because CSC.exe uses langversion:default by default which equals C#7.0. I think this is a bug. I'll create an issue against nuget.org/packages/Microsoft.Net.Compilers, which has the same issue.Crandell
Issue created here github.com/dotnet/roslyn/issues/24000#issuecomment-354882800Crandell
Just FYI, nearly a year and a half later, this still seems to be an issue in Azure DevOps builds. I tried adding the /property:langversion=latest argument in DevOps build, but it yelled at me Error CS1617: Invalid option 'latest' for /langversion; must be ISO-1, ISO-2, Default or an integer in range 1 to 6.Cheery
K
1

You need to set the language version to C# latest minor version (latest) for all build configurations not just Debug. See here for how to do it.

  • Right-click YourProject, click Properties

  • Click Build if it's not already selected

  • Change Configuration to All Configurations

  • Click Advanced...

  • Change the language version

Kingsley answered 26/9, 2018 at 23:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.