Why do we get this InvalidProjectFileException exception when using the Microsoft.Build library?

Viewed 256

I've been asked to investigate why our build process is failing when it's being ran on a machine that has Visual Studio 2022 (Version 17.0.0 Preview 3.1) installed on it. I'm not very familiar with C#, MSBuild, or NuGet packages so I may be asking a nonsensical question! Please be patient.

Our C# build app tries to create an instance of Microsoft.Build.Execution.ProjectInstance, as follows:

var pi = new ProjectInstance(projectFile, BuildProperties, null);

Where "projectFile" is the location of a an XML script for MSBuild. But this call causes the following exception:

Microsoft.Build.Exceptions.InvalidProjectFileException: 'The tools version "Current" is unrecognized. Available tools versions are "2.0", "3.5", "4.0".

If I try changing the null argument to "16.0", "17.0", or "Current" I get variations on the same exception.

(If I try to use msbuild.exe directly, it says that it's "Microsoft (R) Build Engine version 17.0.0-preview-21380-01+b6e7d6051 for .NET Framework", although I accept that this may be a red herring since we're automating MSBuild, not running msbuild.exe directly, so apologies if this is irrelevant.)

I have the latest NuGet Microsoft.Build packages (v16.11.0) referenced from the build app project. I notice there are no v17 packages (yet).

Are we doing something fundamentally wrong here? Why do we get this exception? It seems unusual that the exception's message says that the only tools are really low version numbers. I would have expected it to say that the tool version are more like 16, 17 etc, but not "2.0"! Is the ToolsVersion argument in the ProjectInstance constructor related to the version of the NuGet packages (16.11), or to the version of MSBuild that was installed with VS2022 (17.0 preview), or something else entirely!?

All help very much appreciated. If I've worded the question badly, or you need more info to assist, then please ask.

0 Answers
Related