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

fix assembly dependencies resolver for netcore #1318

Merged
merged 1 commit into from
Feb 23, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,57 @@ internal sealed class TestAssemblyLoadContext : AssemblyLoadContext
private readonly string _testAssemblyPath;
private readonly string _basePath;
private readonly TestAssemblyResolver _resolver;
private readonly System.Runtime.Loader.AssemblyDependencyResolver _runtimeResolver;

public TestAssemblyLoadContext(string testAssemblyPath)
{
_testAssemblyPath = testAssemblyPath;
_resolver = new TestAssemblyResolver(this, testAssemblyPath);
_basePath = Path.GetDirectoryName(testAssemblyPath);
_runtimeResolver = new AssemblyDependencyResolver(testAssemblyPath);
}

protected override Assembly Load(AssemblyName name)
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var loadedAssembly = assemblies.FirstOrDefault(x => x.GetName().Name == name.Name);
if (loadedAssembly != null)
{
return loadedAssembly;
}

loadedAssembly = base.Load(name);
if (loadedAssembly != null)
{
return loadedAssembly;
}

var runtimeResolverPath = _runtimeResolver.ResolveAssemblyToPath(name);
if (string.IsNullOrEmpty(runtimeResolverPath) == false &&
File.Exists(runtimeResolverPath))
{
loadedAssembly = LoadFromAssemblyPath(runtimeResolverPath);
}

if (loadedAssembly != null)
{
return loadedAssembly;
}

loadedAssembly = _resolver.Resolve(this, name);
if (loadedAssembly != null)
loadedAssembly = base.Load(name);
{
return loadedAssembly;
}

if (loadedAssembly == null)
// Load assemblies that are dependencies, and in the same folder as the test assembly,
// but are not fully specified in test assembly deps.json file. This happens when the
// dependencies reference in the csproj file has CopyLocal=false, and for example, the
// reference is a projectReference and has the same output directory as the parent.
string assemblyPath = Path.Combine(_basePath, name.Name + ".dll");
if (File.Exists(assemblyPath))
{
// Load assemblies that are dependencies, and in the same folder as the test assembly,
// but are not fully specified in test assembly deps.json file. This happens when the
// dependencies reference in the csproj file has CopyLocal=false, and for example, the
// reference is a projectReference and has the same output directory as the parent.
string assemblyPath = Path.Combine(_basePath, name.Name + ".dll");
if (File.Exists(assemblyPath))
loadedAssembly = LoadFromAssemblyPath(assemblyPath);
loadedAssembly = LoadFromAssemblyPath(assemblyPath);
}

return loadedAssembly;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public void Dispose()
_loadContext.Resolving -= OnResolving;
}

public Assembly Resolve(AssemblyLoadContext context, AssemblyName name)
{
return OnResolving(context, name);
}

private Assembly OnResolving(AssemblyLoadContext context, AssemblyName name)
{
foreach (var library in _dependencyContext.RuntimeLibraries)
Expand Down