When building a project or solution using a specific version of msbuild
I can select an earlier .net toolchain by using the /toolsversion
or /tv
switch:
"C:\Program Files (x86)\MSBuild\14.0\bin\msbuild" /tv:12.0 amazing.sln
This Just Works for all versions of msbuild
, and the version of csc.exe
etc. is correctly chosen based on the above:
> "C:\Program Files (x86)\MSBuild\14.0\bin\msbuild" /tv:4.0 amazing.sln ... CoreCompile: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe ... ... > "C:\Program Files (x86)\MSBuild\14.0\bin\msbuild" /tv:12.0 amazing.sln ... CoreCompile: C:\Program Files (x86)\MSBuild\12.0\bin\Csc.exe ... ...
If I don't specify /tv
, then depending on which version of msbuild I'm using and a number of environment variables, I may get any of:
msbuild.exe
I'm usingmsbuild.exe.config
(See the different versions of the Overriding ToolsVersion Settings page on MSDN).
So, in order to have builds that have consistent results on the build server and on my local machine, I use /tv
when running msbuild.exe
(in fact, this is enforced in a psake
script, which also ensures it uses the corresponding version of msbuild.exe
).
However I cannot use the /tv
switch when building with Visual Studio. Instead, Visual Studio 2013 and up will use the .net toolchain that shipped with that version of Visual Studio unless:
MSBUILDLEGACYDEFAULTTOOLSVERSION
is set and...This is so baroque that I cannot believe anyone is actually doing it. My questions are thus:
MSBUILDLEGACYDEFAULTTOOLSVERSION
thing?And lastly:
(My prejudice is that I should care, since:
Perhaps I want too much...)
For a concrete example of the problem, please see my msbuild-vs-vs2015-toolsversion repository on github.
Some background: I'm asking this because we recently had a CI build error when one of my colleagues submitted C# 6.0 code that compiled fine with Roslyn on their copy of Visual Studio 2015, but failed in CI because that uses the previous release of the .net toolchain (they'd used an automatic property with no setter, which is fine in Roslyn but not in earlier versions). We will be updating the CI build to Roslyn, but I wanted to see if we could prevent this sort of thing happening in the future.
On the start window, select Open a project or solution. Visual Studio opens an instance of File Explorer, where you can browse to your solution or project, and then select it to open it. If you've opened the project or solution recently, select it from the Open recent section to quickly open it again. Start coding!
Visual Studio 2013 uses a ToolsVersion of 12.0. Visual Studio 2015 uses ToolsVersion 14.0, and Visual Studio 2017 uses ToolsVersion 15.0.
A newer version of Visual Studio might not support certain projects at all, or it might require that you update a project so that it's no longer backwards-compatible. For current status on migration issues, refer to the Visual Studio Developer Community.
I solved this by writing a Visual Studio extension that temporarily sets the environment variable MSBUILDDEFAULTTOOLSVERSION
for the duration of a build; the value to be used is read from a file .toolsversion
in the same directory as the .sln
file. The psake script reads the same .toolsversion
file and passes the value to the /tv
switch.
The code for the extension can be found here: https://github.com/guyboltonking/set-toolsversion-extension. Sadly, I'm not working with C++, or indeed with Visual Studio, at the moment, so I can't provide any support for it (but I can tell you I used it with no issues at all for several months).
Kudos to @efaruk for reminding me about the existence of MSBUILDDEFAULTTOOLSVERSION
.
Edit: Thanks to @mbadawi23, it's now possible to use the extension with both VS2015 and VS2017.
To force a specific C# version in Visual Studio 2015, you can go into the project properties -> Build -> Advanced -> Language Version.
If you set this to 5, the compiler will complain about C# 6 features with: Feature '...' is not available in C# 5. Please use language version 6 or greater.
Alternativly ReSharper also has some tools for this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With