Is there an easy way to get Visual Studio 2015 to use a specific ToolsVersion utility?

When creating a project or solution using a specific version of msbuild I can select an earlier .net tool chain using the /toolsversion or /tv switch:

 "C:\Program Files (x86)\MSBuild\14.0\bin\msbuild" /tv:12.0 amazing.sln 

This is Just Works for all versions of msbuild , and the version is csc.exe , etc. correctly selected 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 do not specify /tv , then depending on which version of msbuild I am using and a number of environment variables, I can get any of:

  • ToolsVersion tool specified in the top-level element in the project file
  • ToolsVersion tool corresponding to msbuild.exe version I am using
  • Value from msbuild.exe.config
  • Value from the registry

(see the various versions of the Tool Parameter Override Settings page on MSDN ).

So, in order to have assemblies that have consistent results on the build server and on my local machine, I use /tv when running msbuild.exe (actually this is enforced in the psake script, which also ensures that it uses the appropriate version of msbuild.exe ).

However, I cannot use the /tv switch when creating using Visual Studio. Instead, Visual Studio 2013 and above will use the .net toolchain that ships with this version of Visual Studio if:

  • The environment variable MSBUILDLEGACYDEFAULTTOOLSVERSION and ...
  • ... all project files have the ToolsVersion attribute set for the version I want to use.

It is so baroque that I can’t believe that someone is actually doing it. My questions are as follows:

  • Does anyone do the MSBUILDLEGACYDEFAULTTOOLSVERSION thing?
  • If not, is there another way to force Visual Studio to use a specific ToolsVersion utility that does not use the version of Visual Studio that ships with this ToolsVersion utility? Something that could be saved in version control (for example, in a project or in some other settings file) would be ideal.

And finally:

  • I do not care? Given that each subsequent version of the C # compiler should be able to process the input of previous versions, and I can set the target .net structure and the C # language level in the project file, is this enough to ensure repeatability of the assembly?

(My prejudice is that I should take care because:

  • I want the builds in the IDE and on the build server to be the same (of course)
  • I want to be able to use VS2015 (and future versions) because it is a better IDE than previous ones, but I do not want to be required to use a new toolchain until I decide.

Maybe I want too much ...)

For a specific example of the problem, see my msbuild-vs-vs2015-toolsversion repository on github.




Some suggestion: I am asking about this because we recently had a CI build error when one of my colleagues presented C # 6.0 code that was compiled with Roslyn on their copy of Visual Studio 2015 but failed in CI because it uses the previous one release of the .net toolchain (they used an automatic property without a setter, which is fine in Roslyn, but not in earlier versions). We will upgrade the CI build to Roslyn, but I wanted to see if we could prevent this from happening in the future.

+43
c # visual-studio-2015 msbuild
Dec 03 '15 at 12:04
source share
4 answers

I solved this by writing a Visual Studio extension that temporarily sets the MSBUILDDEFAULTTOOLSVERSION environment MSBUILDDEFAULTTOOLSVERSION at build time; the value to be used is read from the .toolsversion file in the same directory as the .sln file. Psake script reads the same .toolsversion file and transfers the value to /tv .

The extension code can be found here: https://github.com/guyboltonking/set-toolsversion-extension . Unfortunately, at the moment I am not working with C ++ or even with Visual Studio, so I can not provide him any support (but I can say that I used it without any problems for several months).

Kudos to @efaruk for reminding the existence of MSBUILDDEFAULTTOOLSVERSION .

Edit: Thanks to @ mbadawi23, you can now use the extension with both VS2015 and VS2017.

+4
Aug 11 '16 at 2:00
source share

Note. You can always create an msbuild file to create your project using it or change your project yourself, and you can conditionally determine the version of your tool ( https://msdn.microsoft.com/en-us/library/7z253716.aspx ) ( . csproj is also a structured msbuild script with a different extension , and it will also be compatible with VS).

Respectfully...

Edit:

https://msdn.microsoft.com/en-us/library/bb383985.aspx

by setting the $(ProjectToolsVersion) property in the project as part of the solution. This allows you to create a project in a solution with a toolbox version that is different from the version of other projects.

So, I think you have your answer;)

+2
Jan 26 '16 at 14:09
source share

To force a specific version of C # to be used in Visual Studio 2015, you can go into project properties -> Build -> Advanced -> Language Version.

If you set the value to 5, the compiler will complain about C # 6 functions: The "..." function is not available in C # 5. Use language version 6 or higher.

Alternativly ReSharper also has some tools for this.

+2
Dec 09 '16 at 10:28
source share

What you see in Visual Studio (tools, etc.) and the code behind them is not included in the compiled data, they are just a visual / readable representation when compiling them as an earlier version of VS that you create executable from this version.

Please keep in mind that when compiling as a previous version of .NET, you may lose functionality such as asynchronous functions.

-2
Dec 14 '15 at 12:59
source share



All Articles