31

To use the new C# 7.1 language features with Visual Studio 2017, you add the setting <LangVersion>latest</LangVersion> to your project file(s).

However, building such projects from MSBuild (version 15.3.409.57025, located at C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin) results in an error:

CSC : error CS1617: Invalid option 'latest' for /langversion;
must be ISO-1, ISO-2, Default or an integer in range 1 to 6.

Is this feature just not yet supported by MSBuild, or is it possible to get this working?

This covers 200+ projects that were originally created variously in Visual Studio 2013 and 2015. They were all re-targeted to .NET 4.7 using the Target Framework Migrator tool (which saved lots of clicking and appears - based on inspecting .csproj file changes - to do the job correctly).

The projects all build successfully from Visual Studio 2017.

poke
  • 369,085
  • 72
  • 557
  • 602
Richard Ev
  • 52,939
  • 59
  • 191
  • 278

6 Answers6

28

Nuget packages

  • Microsoft.Net.Compilers nuget package does not work and needn't to be installed.

Set the following project/build settings

  • Set at least C# 7.1 or higher in the Debug and Release build properties. (via: Project menu > [ProjectName] Properties > Build tab > [Advanced] button > Language Version).

  • Setting it to latest does not work.

preview

Also make sure that you are running the latest MSBuild version.

S.Serpooshan
  • 7,608
  • 4
  • 33
  • 61
Dominic Jonas
  • 4,717
  • 1
  • 34
  • 77
8

Add a reference to the Microsoft.Net.Compilers package (version 2.3.x, to get C# 7.1).

A short description of the package:

Microsoft.Net.Compilers

This package not only includes the C# and Visual Basic compilers, it also modifies MSBuild targets so that the included compiler versions are used rather than any system-installed versions. Once installed, this package requires Microsoft Build Tools 2015.

Julien Couvreur
  • 4,473
  • 1
  • 30
  • 40
  • 1
    This did the trick for me. I had the package in my main project, but today I updated a referenced project to C# 7.1 and forgot it didn't have this NuGet package. I received this exact issue - builds in VS but fails on the build server. Thanks for the tip! – Ian Yates Oct 16 '17 at 11:59
  • 5
    Actually... My problem was also caused by me changing the value in DEBUG project property settings. Visiting the RELEASE settings found it was still C# (latest major version). Changing that, checking in and doing another build. Happy now :) – Ian Yates Oct 16 '17 at 12:15
  • Thats what fixed it for me @Ian Yates. Thanks. – John Mills Mar 28 '18 at 04:08
  • Only this solution helped me to fix it – Ivan Doroshenko Apr 26 '22 at 13:40
4

Make sure you have changed for "All Configuration" and not just "Debug"

enter image description here

enter image description here

else you will be baffling why it is failing at production.

Raghav
  • 8,772
  • 6
  • 82
  • 106
3

In case you land here because you get the error as the OP mentioned, running msbuild via command line (e.g. from a build agent such as jenkins), the solution may be as easy as to upgrade Microsoft Build Tools 2015.

You can do that via choco install microsoft-build-tools or manually via the official Microsoft Build Tools 2015 or by updating your Visual Studio 2017 installation.

firepol
  • 1,731
  • 22
  • 39
2

I've got a solution with a C# console app using C# 7.1 here.

Using the VS 2017 command line (and thus MSBuild 15.3.409.57025) it worked fine. (The .csproj does contain <LangVersion>latest</LangVersion>.)

Is this feature just not yet supported by MSBuild, or is it possible to get this working?

Yes it is.

Which instance of csc.exe is being run and what's its version? Because it looks like, despite quoting the version, you have the wrong version of csc.exe (the error message says 1-6 so not even C# 7 would work).

Richard
  • 106,783
  • 21
  • 203
  • 265
2

We discovered that our MVC projects were triggering this issue.

To fix, we updated the Microsoft.CodeDom.Providers.DotNetCompilerPlatform NuGet package used by these projects from version 1.0.0 to 1.0.7.

Richard Ev
  • 52,939
  • 59
  • 191
  • 278