dotnet / msbuild

The Microsoft Build Engine (MSBuild) is the build platform for .NET and Visual Studio.
https://docs.microsoft.com/visualstudio/msbuild/msbuild
MIT License
5.2k stars 1.35k forks source link

[Bug]: Type load exception for BuildRequestDataBase thrown upon getting cache result #10349

Closed mruxmohan4 closed 1 month ago

mruxmohan4 commented 1 month ago

Issue Description

When MSBuild is executed with the quickbuild project cache plugin enabled, a System.TypeLoadException is thrown nearly immediately after the QuickBuildProjectCachePlugin is loaded. Upon further inspection, this occurs when attempting to get the cache result. Instead of getting a proper cache hit where it normally would, the build results in cache misses and demonstrates that the build has completed successfully with the following warning.

c:\src\CloudBuild\private\BuildEngine\Updater\src\QuickBuildUpdater.csproj : warning : Exception querying the QuickBuild project cache plugin. Treating as a cache miss. Exception: System.TypeLoadException: Could not load type 'Microsoft.Build.Execution.BuildRequestDataBase' from assembly 'Microsoft.Build, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

This issue began occurring only after the MSBuild package version was updated from 17.12.0-preview-24328-03 to 17.12.0-preview-24354-03.

Steps to Reproduce

Command-line invocation:

Expected Behavior

c:\src\CloudBuild\private\BuildEngine\Updater\src>msbuild
MSBuild version 17.11.0-preview-24279-02+b963c24ef for .NET Framework

  Determining projects to restore...
  All projects are up-to-date for restore.
Static graph loaded in 0.121 seconds: 1 nodes, 0 edges
Loading the following project cache plugin: QuickbuildProjectCachePlugin
QuickBuildUpdater -> Cache Hit
Build completed, but files may be still materializing and may take some time to finish.

Project cache statistics:
  Cache Hit Count: 1
  Cache Miss Count: 0
  Cache Hit Ratio: 100.0%

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:29.32

Actual Behavior

c:\src\CloudBuild\private\BuildEngine\Updater\src>msbuild
MSBuild version 17.11.0-preview-24279-02+b963c24ef for .NET Framework

  Determining projects to restore...
  All projects are up-to-date for restore.
Static graph loaded in 0.094 seconds: 1 nodes, 0 edges
Loading the following project cache plugin: QuickbuildProjectCachePlugin
c:\src\CloudBuild\private\BuildEngine\Updater\src\QuickBuildUpdater.csproj : warning : Exception querying the QuickBuild project cache plugin. Treating as a cache miss. Exception: System.TypeLoadException: Could not load type 'Microsoft.Build.Execution.BuildRequestDataBase' from assembly 'Microsoft.Build, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
c:\src\CloudBuild\private\BuildEngine\Updater\src\QuickBuildUpdater.csproj : warning :    at Microsoft.Build.QuickbuildProjectCache.QuickbuildProjectCachePlugin.<>c__DisplayClass17_0.<<GetCacheResultAsync>g__GetCacheResultImpl|0>d.MoveNext()
c:\src\CloudBuild\private\BuildEngine\Updater\src\QuickBuildUpdater.csproj : warning :    at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine& stateMachine)
c:\src\CloudBuild\private\BuildEngine\Updater\src\QuickBuildUpdater.csproj : warning :    at Microsoft.Build.QuickbuildProjectCache.QuickbuildProjectCachePlugin.<>c__DisplayClass17_0.<GetCacheResultAsync>g__GetCacheResultImpl|0()
c:\src\CloudBuild\private\BuildEngine\Updater\src\QuickBuildUpdater.csproj : warning :    at Microsoft.Build.QuickbuildProjectCache.QuickbuildProjectCachePlugin.<GetCacheResultAsync>d__17.MoveNext() in c:\src\CloudBuild\private\BuildEngine\ProjectCachePlugin\src\Plugin\QuickbuildProjectCachePlugin.cs:line 286
  QuickBuildUpdater -> c:\src\CloudBuild\private\BuildEngine\Updater\src\bin\x64\Debug\QuickBuildUpdater.exe
  The package QuickBuildUpdater.1.0.19 is missing a readme. Go to https://aka.ms/nuget/authoring-best-practices/readme to learn why package readmes are important.
  Successfully created package 'c:\src\CloudBuild\target\distrib\debug\amd64\nuget\QuickBuildUpdater.1.0.19.nupkg'.
Build completed, but files may be still materializing and may take some time to finish.

Project cache statistics:
  Cache Hit Count: 0
  Cache Miss Count: 0
  Cache Hit Ratio: NaN

Build succeeded.

c:\src\CloudBuild\private\BuildEngine\Updater\src\QuickBuildUpdater.csproj : warning : Exception querying the QuickBuild project cache plugin. Treating as a cache miss. Exception: System.TypeLoadException: Could not load type 'Microsoft.Build.Execution.BuildRequestDataBase' from assembly 'Microsoft.Build, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
c:\src\CloudBuild\private\BuildEngine\Updater\src\QuickBuildUpdater.csproj : warning :    at Microsoft.Build.QuickbuildProjectCache.QuickbuildProjectCachePlugin.<>c__DisplayClass17_0.<<GetCacheResultAsync>g__GetCacheResultImpl|0>d.MoveNext()
c:\src\CloudBuild\private\BuildEngine\Updater\src\QuickBuildUpdater.csproj : warning :    at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine& stateMachine)
c:\src\CloudBuild\private\BuildEngine\Updater\src\QuickBuildUpdater.csproj : warning :    at Microsoft.Build.QuickbuildProjectCache.QuickbuildProjectCachePlugin.<>c__DisplayClass17_0.<GetCacheResultAsync>g__GetCacheResultImpl|0()
c:\src\CloudBuild\private\BuildEngine\Updater\src\QuickBuildUpdater.csproj : warning :    at Microsoft.Build.QuickbuildProjectCache.QuickbuildProjectCachePlugin.<GetCacheResultAsync>d__17.MoveNext() in c:\src\CloudBuild\private\BuildEngine\ProjectCachePlugin\src\Plugin\QuickbuildProjectCachePlugin.cs:line 286
    1 Warning(s)
    0 Error(s)

Time Elapsed 00:00:37.23

Analysis

This appears directly related to https://github.com/dotnet/msbuild/pull/10172, which adds the Microsoft.Build.Execution.BuildRequestDataBase class and is the latest MSBuild change which has impacted that class.

Versions & Configurations

MSBuild version 17.11.0-preview-24279-02+b963c24ef for .NET Framework
17.11.0.27902

Version of CLI tool: Visual Studio 2022 Developer Command Prompt v17.11.0-pre.2.1

JanKrivanek commented 1 month ago

@dfederm Can you advise here? It looks like possibly clash of multiple versions of MSBuild references - but I'd expect QuickBuild to use just single version of MSBuild. A dump of a repro would be helpful here

dfederm commented 1 month ago

QuickBuild uses pre-release builds, updated weekly, for parsing. This is what we compile against. It uses the user-configured MSBuild for actually building projects.

For this specific scenario though, this is the project cache plugin for QuickBuild. So we're compiled against the prerelease version of MSBuild, but we're obviously in the MSBuild process of whatever the user is using.

So the problem here is that we must choose some version of MSBuild to compile against, but we have no control over the version of MSBuild being used at runtime. So the change with BuildRequestDataBase causes a breaking change when the compile vs runtime are on different sides of the line.

Currently we are unable to update the MSBuild used in QuickBuild indefinitely until this issue is resolved.