Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set csc langversion when building in VSTS?

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?

like image 303
Phil Avatar asked Jan 01 '18 15:01

Phil


People also ask

Where is csc.exe located?

The csc.exe executable file is usually located in the Microsoft.NET\Framework\<Version> folder under the Windows directory.

How do I use MSBuild exe?

To run MSBuild at a command prompt, pass a project file to MSBuild.exe, together with the appropriate command-line options. Command-line options let you set properties, execute specific targets, and set other options that control the build process.


2 Answers

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.

like image 118
PatrickLu-MSFT Avatar answered Sep 25 '22 10:09

PatrickLu-MSFT


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

like image 35
ta.speot.is Avatar answered Sep 25 '22 10:09

ta.speot.is