Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Framework/Traits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,13 @@ public bool? LogPropertiesAndItemsAfterEvaluation
/// </summary>
public readonly bool UseSingleLoadContext = Environment.GetEnvironmentVariable("MSBUILDSINGLELOADCONTEXT") == "1";

/// <summary>
/// Use custom AssemblyLoadContext for loading dependencies found in the MSBuild tools directory.
/// When enabled, assemblies found in the MSBuild tools directory are loaded into the plugin's isolated context
/// instead of the shared default context, preventing potential version conflicts with the host application.
/// </summary>
public readonly bool UseCustomLoadContextForDependenciesInToolsDirectory = Environment.GetEnvironmentVariable("MSBUILDUSECUSTOMLOADCONTEXTFORDEPENDENCIESINTOOLSDIRECTORY") == "1";
Comment thread
AR-May marked this conversation as resolved.

/// <summary>
/// Enables the user of autorun functionality in CMD.exe on Windows which is disabled by default in MSBuild.
/// </summary>
Expand Down
6 changes: 4 additions & 2 deletions src/Shared/MSBuildLoadContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,15 @@ public MSBuildLoadContext(string assemblyPath)
// If the Assembly is provided via a file path, the following rules are used to load the assembly:
// - the assembly from the user specified path is loaded, if it exists, into the custom ALC, or
// - if the simple name of the assembly exists in the same folder as msbuild.exe, then that assembly gets loaded
// into the default ALC (so it's shared with other uses).
// into the default ALC (so it's shared with other uses), or into the custom ALC if the custom load context escape hatch
// is enabled (to isolate MSBuild assemblies from the app's dependencies).
var assemblyNameInExecutableDirectory = Path.Combine(BuildEnvironmentHelper.Instance.CurrentMSBuildToolsDirectory,
$"{assemblyName.Name}.dll");

if (FileSystems.Default.FileExists(assemblyNameInExecutableDirectory))
{
return AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyNameInExecutableDirectory);
AssemblyLoadContext targetAlc = Framework.Traits.Instance.EscapeHatches.UseCustomLoadContextForDependenciesInToolsDirectory ? this : AssemblyLoadContext.Default;
return targetAlc.LoadFromAssemblyPath(assemblyNameInExecutableDirectory);
}

return null;
Expand Down
Loading