msbuild: msbuild nuget package unable to open vs2017 csproj files
I’m referencing the nuget packages Microsoft.Build
and Microsoft.Build.Tasks.Core
. It doesn’t matter if I chose stable or prerelease, both versions run into the same error.
I’m trying to open a csproj file for evaluation:
var projectFile = @"C:\projects\test\test.csproj";
var project = new Microsoft.Build.Evaluation.Project(projectFile);
var projectName = Path.GetFileNameWithoutExtension(projectFile);
var outputFile = project.GetPropertyValue("TargetPath");
var outputName = Path.GetFileName(outputFile);
I’m getting Microsoft.Build.Exceptions.InvalidProjectFileException: 'The tools version "15.0" is unrecognized. Available tools versions are "12.0", "14.0", "2.0", "3.5", "4.0".'
When searching the web for this error message I’m only coming up with things related to the VS 2017 installation and how to look up the msbuild packaged with it; unfortunately this doesn’t help me with my instance of the exception because I’m invoking msbuild as a library and not as a process.
Do I have to tell the nuget assemblies where the VS 2017 installation is? How do I do this? (I was assuming the nuget assemblies can work stand alone, but if a VS installation is required that works too, it’s just not discoverable for me what to do here.)
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 14
- Comments: 72 (23 by maintainers)
Commits related to this issue
- Fix for https://github.com/Microsoft/msbuild/issues/2369 and unlucky souls who are running VS2017 15.3 — committed to IntelliTect/Coalesce by adamskt 7 years ago
- Check only the major version when finding VS Fixes #2369 by relaxing the check to find any Visual Studio 15.* install. This is required because Visual Studio 2017 Update 3 reports as 15.3.something, ... — committed to rainersigwald/msbuild by rainersigwald 7 years ago
- Merge pull request #2454 from rainersigwald/find-any-msbuild-15 Fixes #2369 by relaxing the check to find any Visual Studio 15.* install. This is required because Visual Studio 2017 Update 3 reports... — committed to dotnet/msbuild by rainersigwald 7 years ago
- Check only the major version when finding VS Fixes #2369 by relaxing the check to find any Visual Studio 15.* install. This is required because Visual Studio 2017 Update 3 reports as 15.3.something, ... — committed to AndyGerlicher/msbuild by rainersigwald 7 years ago
- HACK Machine-specfic workaround for VS version finding problem https://github.com/Microsoft/msbuild/issues/2369#issuecomment-323135859 — committed to GrahamTheCoder/RefactoringEssentials by GrahamTheCoder 7 years ago
- HACK Machine-specfic workaround for VS version finding problem https://github.com/Microsoft/msbuild/issues/2369#issuecomment-323135859 — committed to GrahamTheCoder/RefactoringEssentials by GrahamTheCoder 7 years ago
- HACK Machine-specfic workaround for VS version finding problem https://github.com/Microsoft/msbuild/issues/2369#issuecomment-323135859 — committed to GrahamTheCoder/RefactoringEssentials by GrahamTheCoder 7 years ago
- HACK Machine-specfic workaround for VS version finding problem https://github.com/Microsoft/msbuild/issues/2369#issuecomment-323135859 — committed to GrahamTheCoder/RefactoringEssentials by GrahamTheCoder 7 years ago
- HACK Machine-specfic workaround for VS version finding problem https://github.com/Microsoft/msbuild/issues/2369#issuecomment-323135859 — committed to GrahamTheCoder/RefactoringEssentials by GrahamTheCoder 7 years ago
- HACK Machine-specfic workaround for VS version finding problem https://github.com/Microsoft/msbuild/issues/2369#issuecomment-323135859 — committed to GrahamTheCoder/RefactoringEssentials by GrahamTheCoder 7 years ago
- HACK Machine-specfic workaround for VS version finding problem https://github.com/Microsoft/msbuild/issues/2369#issuecomment-323135859 — committed to GrahamTheCoder/RefactoringEssentials by GrahamTheCoder 7 years ago
- HACK Machine-specfic workaround for VS version finding problem https://github.com/Microsoft/msbuild/issues/2369#issuecomment-323135859 — committed to GrahamTheCoder/RefactoringEssentials by GrahamTheCoder 7 years ago
- HACK Machine-specfic workaround for VS version finding problem https://github.com/Microsoft/msbuild/issues/2369#issuecomment-323135859 — committed to GrahamTheCoder/RefactoringEssentials by GrahamTheCoder 7 years ago
- HACK Machine-specfic workaround for VS version finding problem https://github.com/Microsoft/msbuild/issues/2369#issuecomment-323135859 — committed to GrahamTheCoder/RefactoringEssentials by GrahamTheCoder 7 years ago
- HACK Machine-specfic workaround for VS version finding problem https://github.com/Microsoft/msbuild/issues/2369#issuecomment-323135859 — committed to GrahamTheCoder/RefactoringEssentials by GrahamTheCoder 7 years ago
- HACK Machine-specfic workaround for VS version finding problem https://github.com/Microsoft/msbuild/issues/2369#issuecomment-323135859 — committed to GrahamTheCoder/RefactoringEssentials by GrahamTheCoder 7 years ago
- HACK Machine-specfic workaround for VS version finding problem https://github.com/Microsoft/msbuild/issues/2369#issuecomment-323135859 — committed to GrahamTheCoder/RefactoringEssentials by GrahamTheCoder 7 years ago
@weltkante Yes, if you have that information already, there’s an easy workaround.
Set the environment variables
VSINSTALLDIR
andVisualStudioVersion
before calling into MSBuild APIs.I just confirmed on my machine with this hardcoded value on top of your example code:
The problem is arising because MSBuild attempts to build from the installed version of Visual Studio where possible, but we’re failing to locate it now (still not sure why; continuing to look). Setting those environment variables lets an alternate codepath through the find-toolset code take over.
You can and should use the VS copy of MSBuild, but since it’s no longer in the GAC you must locate it. https://docs.microsoft.com/en-us/visualstudio/msbuild/updating-an-existing-application has details on what’s required, and https://github.com/Microsoft/MSBuildLocator/ is a package used to make the process easier.
Ok, here’s the source of the problem:
https://github.com/Microsoft/msbuild/blob/ab090d1255caa87e742cbdbc6d7fe904ecebd975/src/Shared/BuildEnvironmentHelper.cs#L232-L235
The returned instance of VS (on this machine) has version
15.3.26730.3
, which doesn’t matchCurrentVisualStudioVersion
which is15.0
.Need to track down an Update 2 machine to see if this is a recent change in the return value that we need to ping the setup folks about (I don’t see how it could have been working otherwise).
Also need to make our code more robust. Not quite sure how yet.
@rainersigwald That’s an awful lot of dicking around for something that used to be a checkbox.
Ok resolved the above by adding
Microsoft.Build.Tasks.Core
nuget package to my project. I then got an error when trying to run thePack
target:The error message prompted me to check binding redirects in app.config - was missing a binding redirect for some reason. Adding the folliwng seems to have fixed it:
@Kryptos-FR Yes, that’s exactly right. We’re working on #2030 to provide a NuGet package that makes that easier to do (and includes loading the MSBuild assemblies from the same installed VS).
@GeirGrusom I think you might like to use MSBuildLocator in your application. It handles the scenarios you’re currently using VSWhere for, and you should be able to use a single application to load both 2017 and 2019 projects (as long as you reference MSBuild 15.x when you build).
@raffaeler Thanks! Unfortunately, I do not see the same thing on my machine, also with
15.5.27130.2010
.My attempt to repro was:
Microsoft.CodeAnalysis
2.6.1.OpenSolutionAsync
and some minimal work.This failed with a similar-but different error:
That’s because I forgot to add binding redirects (as mentioned above in https://github.com/Microsoft/msbuild/issues/2369#issuecomment-327033965). I added this to my app.config:
And loading the projects completes without errors.
Follow-up questions:
@jairbubbles btw, you can have it working with the current build as well. Here’s how I handled it: https://github.com/T4MVC/R4MVC/commit/ae2fd5d8f3ab60708419d37c8a42d237d86d3061#diff-89dd7d1659695edb3702bfe879b34b09R61
Thx @artiomchi !
I can confirm you that with version
15.5.0-preview-000072-0942130
I do not need the hack anymore.Any chance to get this in a 15.3 patch?
In my case I need my library to work on computers which do not have VS2017 installed so I tweaked a little bit the workaround. I’m sharing it here in case it helps others:
Obviously I won’t be able to load 15.0 projects in this case.
I was able to make it work by setting the
VSINSTALLDIR
andVisualStudioVersion
. Although there was a caveat that I was not expecting (but which still makes sense somehow): you need to set those variables before calling any of theMicrosoft.Build
API (especiallyProjectCollection
) otherwise the list of toolsets is cached and you will not be able to find the one for “15.0” after that.This means that in order to do it properly one has to follow these steps in order:
Microsoft.VisualStudio.Setup.Configuration
ISetupInstance2.GetInstallationPath()
Microsoft.Build
API, for example:In case you need the full path to
MSBuild.exe
it should be inPath.Combine(installationPath, "MSBuild", "15.0", "Bin")
.Note: it is still going under some testing but it is my hope that this will work for the time being, until the fix is released in the next MSBuild.