msbuild icon indicating copy to clipboard operation
msbuild copied to clipboard

Do not share assemblies of task dependencies if not running on `MSBuild.exe`.

Open teo-tsirpanis opened this issue 9 months ago • 5 comments

Fixes microsoft/MSBuildLocator#336 Fixes #13286

Context

When MSBuild resolves dependencies for a task, if it cannot find the dependent assembly in the task assembly's directory, it tries finding it in MSBuild's tools directory. In that case, the dependency is loaded to AssemblyLoadContext.Default instead of the task's own ALC, in order to share it across tasks and avoid loading it multiple times.

However, loading it to the default ALC does not reliably work if MSBuild is running on an arbitrary host app (such as by being loaded by MSBuildLocator), because it will cause failures if the host app has itself a dependency on an older assembly version than the task's dependency. This is what caused the linked issue.

Changes Made

Updated MSBuildLoadContext.Load to load dependencies found in the tools directory, into itself, instead of in AssemblyLoadContext.Default. To avoid regressing performance in the majority of cases, the existing behavior is maintained if MSBuild is running as part of MSBuild.exe (detected by checking the entry assembly's name, once in static initialization).

Testing

Validated that the test failures in the linked issue no longer reproduce. I did this by using a custom copy of .NET SDK 8.0.418, with a patched Microsoft.Build.dll.

Notes

teo-tsirpanis avatar Feb 24 '26 00:02 teo-tsirpanis

/azp run

AR-May avatar Feb 26 '26 09:02 AR-May

Azure Pipelines successfully started running 1 pipeline(s).

azure-pipelines[bot] avatar Feb 26 '26 09:02 azure-pipelines[bot]

Today, RunningInMSBuildExe is false when running dotnet build, which means this code always loads into a custom ALC. This can potentially cause a performance regression.

I talked to @AR-May about this offline, and I'm not sure this is the right behavior. I think this PR may be exactly correct and our definition for RunningInMSBuildExe may be wrong--because I think conceptually what that is supposed to mean is something more like "we fully understand and control the runtime environment" and builds launched through dotnet build and the nodes they launch should count in that category.

We're going to dig into that angle a bit more.

rainersigwald avatar Feb 26 '26 15:02 rainersigwald

MSBuild triage: @rainersigwald, @AR-May are we still interested in merging this?

AlesProkop avatar Jun 23 '26 12:06 AlesProkop

In light of #13305 being merged, our options are:

  • Keep #13305 and close this PR.
  • Remove the escape hatch added in #13305 and merge this PR, so that we always do not share task dependencies if not running on MSBuild.exe.
  • Keep both the escape hatch and automatically enable it if not running on MSBuild.exe.

I have a slight preference to the second option, because it leads to the least error-prone behavior by default. I think the third option is an overkill. Let me know what you think.

teo-tsirpanis avatar Jun 23 '26 20:06 teo-tsirpanis