msbuild: Cannot use MSBuild API from a dotnet-CLI project tool
Using MSBuild API for in-proj project evaluation in a dotnet-CLI project tool throws this excpetion:
Unhandled Exception: System.TypeInitializationException: The type initializer for 'BuildEnvironmentHelperSingleton' threw an exception. ---> System.InvalidOperationException: Could not determine a valid location to MSBuild. Try running this process from the Developer Command Prompt for Visual Studio.
at Microsoft.Build.Shared.ErrorUtilities.ThrowInvalidOperation(String resourceName, Object[] args)
at Microsoft.Build.Shared.BuildEnvironmentHelper.Initialize()
at Microsoft.Build.Shared.BuildEnvironmentHelper.BuildEnvironmentHelperSingleton..cctor()
IIUC the problem is that BuildEnvironmentHelperSingleton looks in AppContext.BaseDirectory (and a few other locations) for MSBuild.exe. When dotnet-CLI invokes a project tool, the AppContext.BaseDirectory will be $(NuGetPackages)/.tools/$(ToolName)/$(ToolVersion)/netcoreapp1.0/. The only content NuGet/CLI will put into this directory is the *.deps.json file and project.lock.json file for the tool.
Using Microsoft.Build.Runtime 15.1.262-preview5.
cc @piotrpMSFT
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 3
- Comments: 29 (22 by maintainers)
Ping on a really old thread. At this point, all of the tools I have created workaround this limitation by invoking a new MSBuild process instead of calling on MSBuild API directly. It’s a less-than-ideal programming experience though.
I recently saw this: https://github.com/daveaglick/Buildalyzer. Is this kind of API something MSBuild would every provide as a 1st class thing? If not, I suugest we just close this as “wontfix” and invite tool authors to use something like https://github.com/daveaglick/Buildalyzer if they want to use MSBuild in-proc.