Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load all assemblies from the MSBuild folder if not already loaded #107

Merged
merged 7 commits into from
Dec 22, 2020
Merged
Changes from 2 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
39 changes: 21 additions & 18 deletions src/MSBuildLocator/MSBuildLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public static class MSBuildLocator
"Microsoft.Build.Framework",
"Microsoft.Build.Tasks.Core",
"Microsoft.Build.Utilities.Core",
"System.Runtime.CompilerServices.Unsafe",
"System.Numerics.Vectors"
};

#if NET46
Expand Down Expand Up @@ -173,7 +171,7 @@ public static void RegisterMSBuildPath(string msbuildPath)
}

// AssemblyResolve event can fire multiple times for the same assembly, so keep track of what's already been loaded.
var loadedAssemblies = new Dictionary<string, Assembly>(s_msBuildAssemblies.Length);
var loadedAssemblies = new Dictionary<string, Assembly>();

// Saving the handler in a static field so it can be unregistered later.
#if NET46
Expand All @@ -185,7 +183,7 @@ public static void RegisterMSBuildPath(string msbuildPath)

AppDomain.CurrentDomain.AssemblyResolve += s_registeredHandler;
#else
s_registeredHandler = (assemblyLoadContext, assemblyName) =>
s_registeredHandler = (_, assemblyName) =>
{
return TryLoadAssembly(assemblyName);
};
Expand All @@ -200,27 +198,32 @@ Assembly TryLoadAssembly(AssemblyName assemblyName)
// Assembly resolution is not thread-safe.
lock (loadedAssemblies)
{
Assembly assembly;
if (loadedAssemblies.TryGetValue(assemblyName.FullName, out assembly))
if (loadedAssemblies.TryGetValue(assemblyName.FullName, out Assembly assembly))
{
return assembly;
}

if (IsMSBuildAssembly(assemblyName))
rainersigwald marked this conversation as resolved.
Show resolved Hide resolved
string targetAssembly = Path.Combine(msbuildPath, assemblyName.Name + ".dll");
rainersigwald marked this conversation as resolved.
Show resolved Hide resolved
if (File.Exists(targetAssembly))
{
var targetAssembly = Path.Combine(msbuildPath, assemblyName.Name + ".dll");
if (File.Exists(targetAssembly))
// If the assembly was already loaded, return that. This could theoretically cause version problems, but the user shouldn't be
// trying to load an MSBuild that is incompatible with the version of the assembly they currently have loaded.
Assembly loadedAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name.Equals(assemblyName.Name));
if (loadedAssembly != null)
{
// Automatically unregister the handler once all supported assemblies have been loaded.
if (Interlocked.Increment(ref numResolvedAssemblies) == s_msBuildAssemblies.Length)
{
Unregister();
}

assembly = Assembly.LoadFrom(targetAssembly);
loadedAssemblies.Add(assemblyName.FullName, assembly);
return assembly;
loadedAssemblies.Add(assemblyName.FullName, loadedAssembly);
return loadedAssembly;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete

// Automatically unregister the handler once all supported assemblies have been loaded.
if (Interlocked.Increment(ref numResolvedAssemblies) == s_msBuildAssemblies.Length)
{
Unregister();
}

assembly = Assembly.LoadFrom(targetAssembly);
loadedAssemblies.Add(assemblyName.FullName, assembly);
return assembly;
}

return null;
Expand Down