My codebase contains about 30 solutions. Most of the projects in these solutions are (C#) .NET Framework, but more recently I've been adding some .NET Standard 2.0 and .NET Core 3.1 projects. All projects in all solutions build perfectly fine in Visual Studio 2019 - just click "Build" and they build.
All projects in all solutions also build in Azure DevOps (...although configuring the build pipelines to reach that point was painful).
As a convenience, I wrote a simple tool that would use the Microsoft.Build NuGet package to cycle through all solutions in the codebase and build them. This would allow me to quickly check whether I'd made any changes in one solution that broke something in another (e.g. in a shared project) without me noticing. This is where the troubles begin.
.NET Framework projects all build fine. However, when building any .NET Core project, I get build errors. These vary slightly from project to project, but all include some error similar to the following:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VisualStudio\v16.0\VSSDK\Microsoft.VsSDK.targets(583,5):
error MSB4018: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Build, Version=14.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
The system cannot find the file specified.
In some cases it might be Microsoft.Build.Framework instead of Microsoft Build, or it might be version 15.0.0.0 instead of 14.0.0.0, and there are always other errors in addition that arise from not being able to find the assembly it is looking for; but this appears to be the core of the problem.
The Microsoft.Build Nuget package is version 16.7.0. The only installed MSBuild version is whichever is installed with with Visual Studio 2019 version 16.7.6, which I think is 16.7.0. None of the affected projects have any apparent dependency on older versions of MSBuild, e.g. in the project their ToolsVersion="16.0", and anyway if there was something wrong with the project it wouldn't build in Visual Studio - not to mention that some of them are brand new as of only the last few days.
So, why is something(?) deciding to look for older versions of Microsoft.Build assemblies (and failing), when I'm building with a current version of Microsoft.Build assemblies, and the projects are all correct, and they all build in Visual Studio? Or - how can I find this out for myself?