Skip to content

Commit

Permalink
Make compiler assembly loading a resolver in core loader
Browse files Browse the repository at this point in the history
  • Loading branch information
chsienki committed Apr 24, 2024
1 parent 7c8ef76 commit 13781c6
Showing 1 changed file with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal AnalyzerAssemblyLoader(AssemblyLoadContext? compilerLoadContext, Analyz
{
_loadOption = loadOption;
_compilerLoadContext = compilerLoadContext ?? AssemblyLoadContext.GetLoadContext(typeof(AnalyzerAssemblyLoader).GetTypeInfo().Assembly)!;
_externalResolvers = externalResolvers;
_externalResolvers = [.. externalResolvers, new CompilerAnalyzerAssemblyResolver(_compilerLoadContext)];
}

public bool IsHostAssembly(Assembly assembly)
Expand Down Expand Up @@ -125,19 +125,6 @@ public DirectoryLoadContext(string directory, AnalyzerAssemblyLoader loader, Ass
return externallyResolvedAssembly;
}

try
{
if (_compilerLoadContext.LoadFromAssemblyName(assemblyName) is { } compilerAssembly)
{
return compilerAssembly;
}
}
catch
{
// Expected to happen when the assembly cannot be resolved in the compiler / host
// AssemblyLoadContext.
}

// Prefer registered dependencies in the same directory first.
var simpleName = assemblyName.Name!;
var assemblyPath = Path.Combine(Directory, simpleName + ".dll");
Expand Down Expand Up @@ -207,6 +194,19 @@ protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
return IntPtr.Zero;
}
}

internal sealed class CompilerAnalyzerAssemblyResolver : IAnalyzerAssemblyResolver
{
private readonly AssemblyLoadContext _compilerAlc;

public CompilerAnalyzerAssemblyResolver(AssemblyLoadContext? compilerContext = null)
{
_compilerAlc = compilerContext ?? AssemblyLoadContext.GetLoadContext(typeof(AnalyzerAssemblyLoader).GetTypeInfo().Assembly)!;
}

public Assembly? ResolveAssembly(AssemblyName assemblyName) => _compilerAlc.LoadFromAssemblyName(assemblyName);

}
}
}

Expand Down

0 comments on commit 13781c6

Please sign in to comment.