Add escape hatch for not sharing assemblies from tools directory#13305
Merged
rainersigwald merged 1 commit intodotnet:mainfrom Mar 5, 2026
Merged
Conversation
4f68098 to
57a258e
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces an escape hatch to address assembly loading issues when MSBuild is hosted by applications (like via MSBuildLocator) that may have conflicting dependency versions. The change allows users to opt-in to loading MSBuild tools directory assemblies into isolated AssemblyLoadContexts instead of the default shared context.
Changes:
- Added a new environment variable-based escape hatch (
MSBUILDUSECUSTOMLOADCONTEXTFORDEPENDENCIESINTOOLSDIRECTORY) to control assembly loading behavior - Modified assembly loading logic to conditionally use custom or default AssemblyLoadContext based on the escape hatch setting
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Shared/MSBuildLoadContext.cs | Updated assembly loading logic to check the escape hatch and load into appropriate context |
| src/Framework/Traits.cs | Added new escape hatch property for controlling dependency loading behavior |
rainersigwald
approved these changes
Feb 27, 2026
Member
rainersigwald
left a comment
There was a problem hiding this comment.
lgtm as long as @AndyGerlicher @dfederm are ok with it.
Member
|
Got IM approval, let's try it. |
This was referenced Mar 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.Defaultinstead 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
Added escape hatch that customers can set up to work around this problem.
Testing
manual, tested on the provided repro project