Skip to content

Commit

Permalink
fix(Weaver): fixing AssemblyResolver failing to find system dlls
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed Aug 7, 2024
1 parent f14fc64 commit 0551a60
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal sealed class PostProcessorAssemblyResolver : IAssemblyResolver
{
private readonly string[] _assemblyReferences;
private readonly string[] _assemblyReferencesFileName;
private readonly string[] _allParentDirectories;
private readonly Dictionary<string, AssemblyDefinition> _assemblyCache = new Dictionary<string, AssemblyDefinition>();
private readonly ICompiledAssembly _compiledAssembly;
private AssemblyDefinition _selfAssembly;
Expand All @@ -24,6 +25,14 @@ public PostProcessorAssemblyResolver(ICompiledAssembly compiledAssembly)
_assemblyReferences = compiledAssembly.References;
// cache paths here so we dont need to call it each time we resolve
_assemblyReferencesFileName = _assemblyReferences.Select(r => Path.GetFileName(r)).ToArray();

// add path to a system type, because reference path might not be correct
var systemTypePath = typeof(int).Assembly.Location;
_allParentDirectories = _assemblyReferences
.Append(systemTypePath)
.Select(Path.GetDirectoryName)
.Distinct()
.ToArray();
}

public void Dispose()
Expand Down Expand Up @@ -96,8 +105,7 @@ private string FindFile(AssemblyNameReference name)
//in the ILPostProcessing API. As a workaround, we rely on the fact here that the indirect references
//are always located next to direct references, so we search in all directories of direct references we
//got passed, and if we find the file in there, we resolve to it.
var allParentDirectories = _assemblyReferences.Select(Path.GetDirectoryName).Distinct();
foreach (var parentDir in allParentDirectories)
foreach (var parentDir in _allParentDirectories)
{
var candidate = Path.Combine(parentDir, name.Name + ".dll");
if (File.Exists(candidate))
Expand Down

0 comments on commit 0551a60

Please sign in to comment.