How to upgrade msbuild to C# 6?

59,239

Solution 1

Make sure you call:

C:\Program Files (x86)\MSBuild\14.0\Bin\MsBuild.exe

That's the version of MsBuild that ships with Visual Studio 2015 and calls the C# compiler that understands this. You can get this version of MsBuild on your system by installing any edition of Visual Studio 2015 or by installing the stand-alone Microsoft Build Tools 2015.

Adding a reference to the following NuGet package will also force use of the new compiler:

Install-Package Microsoft.Net.Compilers

Please note Install-Package will pick the latest available version which may not be the one you are looking for. Before you install, please check the release notes and dependencies to resolve the underlying issue with the version being dealt with, which in this case, was more specific to VS 2015.

So for Visual Studio 2015:

Install-Package Microsoft.Net.Compilers -Version 1.0.0

Solution 2

You can by the way also install the "Microsoft Build Tools 2015" instead of VS2015 on your build server.

https://www.microsoft.com/en-us/download/details.aspx?id=48159

It installs MSBuild to the same path:

C:\Program Files (x86)\MSBuild\14.0\Bin\MsBuild.exe

Solution 3

You probably already have this working, but this might help someone else in the future. I came across this question recently and it got me moving in the right direction and ultimately led to a solution.

Another possible solution to this is manually updating your project files to target the MSBuild version you want your projects to be built with.

I've recently gone through a TeamCity build server update and I've already installed the Microsoft Build Tools 2015 on it. I thought I had everything in place on the build server, I had my solution targeting C# 6.0, and I had every project targeting .net 4.6.1. Like you, everything with C# 6.0-specific code built just fine in my local environment, but my TeamCity build server didn't like any of it.

As mentioned by others, I tried using the Microsoft.Net.Compilers NuGet package. The latest version of it allowed the build to work on my build server, but it wouldn't let me publish my code locally (a requirement of mine). Earlier versions of that NuGet package would let me publish, but the build wouldn't work.

What I found that I had to do was ultimately modify each project file in my solution to specifically target the MSBuild version that could handle C# 6.0 code. In each of my project files, I found a line similar to the following line:

<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

with the key component of that line being the ToolsVersion portion of it. I simply changed this line on my project files to read the following:

<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

The difference here was that I was targeting version 14, not 4. Version 14.0 corresponds with Build Tools 2015. By changing this, my TeamCity build server used the correct MSBuild version and was able to build my C# 6.0 code.

I also had to manually update the TargetFrameworkVersion xml node of this to use 4.6.1 because VS2015 wasn't doing something right and messed up my local build, but that's not relevant here.

Please, someone correct me if I'm wrong, but just for reference, I think the version numbers go something like this:

4.0 = VS2012

12.0 = VS2013

14.0 = VS2015

15.0 = VS2017

I believe if you wanted to use .net 4.7, you'd have to have the Build Tools 2017 installed and have your projects targeting 15.0 instead of 14.0, but I haven't verified this.

Share:
59,239

Related videos on Youtube

Anna Prosvetova
Author by

Anna Prosvetova

Updated on August 24, 2021

Comments

  • Anna Prosvetova
    Anna Prosvetova almost 3 years

    I want to use C# 6 in my project (null propagation, other features).

    I've installed VS 2015 on my PC and it works brilliantly and builds test code like

    var user = new SingleUserModel(); //all model fields are null
    var test = user.User?.Avatar?["blah"];
    

    But when I push my project to the repo and CI starts to build it, build fails because of unsupported ?.

    I've installed VS2015 on CI server too but looke like it doesn't use it. What can I do?

    CI - CruiseControl .NET Builds with C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe

    • bdrajer
      bdrajer almost 7 years
      To anyone downloading MS Build Tools 2015 from the links given below, they are for an old (pre-update) version that has bugs... I found a link here to 2015 Update 3.
  • Nicolas Fall
    Nicolas Fall almost 9 years
    are there any concerns about this causing bugs since the RyuJIT or whatever have so many bugs?
  • jessehouwing
    jessehouwing almost 9 years
    The compiler and the JIT are 2 separate things. So for compilation it makes no difference.
  • jogo
    jogo over 8 years
    IMHO there is a cause, why the question relates on that enviroment. Better do a comment to the question.
  • MuhKuh
    MuhKuh over 8 years
    As stackoverflow does not allow me to comment on questions, I posted it the only way the systems allows me to. And if you do not find it useful, just ignore it. For me while searching for the solution on my build server this was the way to go, instead of installing VS2015
  • TheWebGuy
    TheWebGuy over 8 years
    Just a note about this, if you are updating your PATH env variable, be sure to remove the old location (ex: C:\Windows\Microsoft.NET\Framework\v4.0.30319) because it might still call that when executing "msbuild" from cmd
  • DrGriff
    DrGriff about 8 years
    I also had to install the nuget package Microsoft.Net.Compilers: see stackoverflow.com/a/36774876/584714
  • Chris Marisic
    Chris Marisic almost 8 years
    In my build script i updated the msbulid element to use 14, got tons of errors for c#6 stuff. Installed the Compilers package into every project, and jenkins was happy afterward.
  • Cole W
    Cole W almost 8 years
    Is there a solution to this without have to install a nuget package into every project? I have 100's of projects that would need this and it seems unnecessary to me. Seems like a bug somewhere.
  • jessehouwing
    jessehouwing almost 8 years
    Call the right version of msbuild. Then you don't need a nuget package.
  • Paolo Vigori
    Paolo Vigori over 7 years
    How do you call the right version if you have a build server (TFS) and not the command line? I cannot access the server directly so I have to give clear instructions on what needs to be done
  • Zinov
    Zinov about 7 years
    Does any one has an answer for @ColeW question?
  • Alex Sanséau
    Alex Sanséau over 6 years
    Once you have installed "Microsoft Build Tools 2015" (or "Microsoft Build Tools 2017") you also have control over which version of Visual Studio you want to use to build the solution: MSBuild.exe /t:Build YourSolution.sln /p:VisualStudioVersion=14.0 [set it to 10.0 for VS2010, to 14.0 for VS2015 and 15.0 for VS2017]
  • MDave
    MDave over 6 years
    In my case I had to update our build tool - NAnt. Then it worked for every project without installing NuGet packages.
  • RBT
    RBT over 4 years
    To download and install Microsoft Build Tools 2015 Update 3, please visit older VS downloads and go to Redistributables and Build Tools section.