Recently I was migrating a CI script from VS 2015 tools to VS 2017 tools. The script was calling to vstest.console.exe to run integration tests, somewhat like this:
I’ve omitted the error handling code here for simplicity. The call itself is quite simple, it relies on the fact that when you install Visual Studio build tools on the CI machine it adds an environment variable VS140COMNTOOLS
that points to the Visual Studio tools folder, usually it is C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\
. To my surprise for VS 2017 there is no such environment variable available. The reason is that VS 2017 supports multiple side-by-side installations, which apparently makes it difficult to create environment variables for every single version installed. I started to look for a way to detect where exactly Visual Studio is installed on the machine and came across a tool that is shipped with the VS installer and does just that. This tool is vswhere. It is basically a substitution for former environment variables and allows to detect instances of the Visual Studio installation on the machine. The usage is quite simple, if I run it in powershell like this:
It will give me a lot of information about my installation of the VS:
But besides getting all this info at once, I can get only certain properties that I’m interested in, for example by adding -property installationPath
as an argument, I’ll get this one line C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise
, which is exactly what I need for the script. The tool can do even more, if I need a specific component to be present in the installation I can add -requires <componentid>
to the arguments list and get only those instances that have the component installed. The list of available components can be found here. For example there is an MSBuild component (Microsoft.Component.MSBuild) that you can use to query when search from build script as well as Microsoft.VisualStudio.Component.TestTools.Core
component that indicates the presence of vstest.console.
So for my script I ended up with this:
Here I ask vswhere to find latest installation of VS that has a component Microsoft.VisualStudio.Component.TestTools.Core
(Testing tools core features) and return me a path to it’s installation folder.
As a side note, I would love to have an environment variable that will point to the vswhere folder, because right now I need to do something like this "${Env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
to find it.
Happy scripting!