-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Augment MSBuild's handling of assembly loading for plugins (currently tasks, SDK resolvers, and loggers) to use an AssemblyLoadContext, allowing plugins to use different versions of dependencies. Creates a new ALC per assembly path (of initially-loaded assembly, not indirect dependencies). Closes #1754. Closes #4635.
- Loading branch information
1 parent
c3fc53d
commit 90ac0c3
Showing
4 changed files
with
76 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
|
||
using System; | ||
using System.Collections.Immutable; | ||
using System.Reflection; | ||
using System.Runtime.Loader; | ||
|
||
#nullable enable | ||
namespace Microsoft.Build.Shared | ||
{ | ||
/// <summary> | ||
/// This class is used to isolate the types used by an MSBuild plugin | ||
/// (SDK resolver, logger, or task). | ||
/// </summary> | ||
internal class MSBuildLoadContext : AssemblyLoadContext | ||
{ | ||
private readonly AssemblyDependencyResolver _resolver; | ||
private readonly string _directory; | ||
|
||
private static readonly ImmutableHashSet<string> _wellKnownAssemblyNames = | ||
new[] | ||
{ | ||
"MSBuild", | ||
"Microsoft.Build", | ||
"Microsoft.Build.Framework", | ||
"Microsoft.Build.Tasks.Core", | ||
"Microsoft.Build.Utilities.Core", | ||
}.ToImmutableHashSet(); | ||
|
||
|
||
public MSBuildLoadContext(string assemblyPath) : | ||
base(name: assemblyPath, isCollectible: false) // TODO: make this collectible? | ||
{ | ||
_directory = assemblyPath; | ||
_resolver = new AssemblyDependencyResolver(_directory); | ||
} | ||
|
||
protected override Assembly? Load(AssemblyName assemblyName) | ||
{ | ||
if (_wellKnownAssemblyNames.Contains(assemblyName.Name!)) | ||
{ | ||
return null; | ||
} | ||
|
||
string? assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName); | ||
if (assemblyPath != null) | ||
{ | ||
return LoadFromAssemblyPath(assemblyPath); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
protected override IntPtr LoadUnmanagedDll(string unmanagedDllName) | ||
{ | ||
string? libraryPath = _resolver.ResolveUnmanagedDllToPath(unmanagedDllName); | ||
if (libraryPath != null) | ||
{ | ||
return LoadUnmanagedDllFromPath(libraryPath); | ||
} | ||
|
||
return IntPtr.Zero; | ||
} | ||
|
||
} | ||
} |