How to make Microsoft.Build.Evaluation.Project use same base properties as Visual Studio?
Microsoft.Build.Evaluation.Project seems to have some rather odd ideas of what values to use when loading projects. In particular, I have a number of projects with the following dependency:
<PackageReference Include="Microsoft.Web.WebJobs.Publish" Version="2.0.0" />
Visual Studio consumes these projects without any problems, but if I try to load one of them with
var proj = new Project(@"path\to\project.csproj");
it throws an
InvalidProjectFileException with error message
The imported project "C:\Program Files\dotnet\sdk\3.1.202\Microsoft\VisualStudio\v16.0\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm that the expression in the Import declaration "C:\Program Files\dotnet\sdk\3.1.202\Microsoft\VisualStudio\v16.0\WebApplications\Microsoft.WebApplication.targets" is correct, and that the file exists on disk. E:\Users\ptaylor\.nuget\packages\microsoft.web.webjobs.publish\2.0.0\build\webjobs.console.targets
The relevant import (which, note, comes from a Microsoft nuget and is not under my control) is
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
The fundamental problem is that Visual Studio uses the sane value for
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild, whereas
Microsoft.Build.Evaluation is using
C:\Program Files\dotnet\sdk\3.1.202. I note that the project has
<TargetFramework>net471</TargetFramework>, so it's not .Net Core and I can't understand why it would make sense to use a .Net Core SDK.
The idea behind using
Microsoft.Build.Evaluation.Project is to write some tooling to complement Visual Studio, so it defeats the point if the two can't agree on basic things like where Microsoft installs its tools. My question is therefore: is there a clean and robust way to make
Microsoft.Build.Evaluation use the same properties as Visual Studio? I can see hacky solutions involving passing values for
MSBuildExtensionsPath32 and similar properties via at least two different mechanisms, but the least hacky value I can think of with the properties available is
$(VSAPPIDDIR)\..\..\MSBuild and that doesn't really pass the sniff test.