How to upgrade msbuild to C# 6?
Asked Answered
C

3

108

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

Chronogram answered 14/8, 2015 at 10:26 Comment(1)
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.Massimo
S
165

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
Soyuz answered 14/8, 2015 at 10:34 Comment(10)
are there any concerns about this causing bugs since the RyuJIT or whatever have so many bugs?Intra
The compiler and the JIT are 2 separate things. So for compilation it makes no difference.Soyuz
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 cmdTrigg
I also had to install the nuget package Microsoft.Net.Compilers: see https://mcmap.net/q/104809/-teamcity-build-using-c-6Coloquintida
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.Chiropteran
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.Melancholy
Call the right version of msbuild. Then you don't need a nuget package.Soyuz
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 doneChiromancy
Does any one has an answer for @ColeW question?Bardwell
In my case I had to update our build tool - NAnt. Then it worked for every project without installing NuGet packages.Baseman
P
61

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
Phenol answered 14/11, 2015 at 11:33 Comment(4)
IMHO there is a cause, why the question relates on that enviroment. Better do a comment to the question.Rist
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 VS2015Phenol
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]Secessionist
To download and install Microsoft Build Tools 2015 Update 3, please visit older VS downloads and go to Redistributables and Build Tools section.Lanita
R
13

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.

Rexer answered 19/2, 2018 at 22:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.