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: Use provided references for search of dependencies in AssemblyCompilations #10491

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
35 changes: 26 additions & 9 deletions src/Docfx.Dotnet/CompilationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static (Compilation, IAssemblySymbol) CreateCompilationFromAssembly(strin
: MetadataImportOptions.Public
),
syntaxTrees: s_assemblyBootstrap,
references: GetReferenceAssemblies(assemblyPath)
references: GetReferenceAssemblies(assemblyPath, references)
.Select(CreateMetadataReference)
.Concat(references ?? [])
.Append(metadataReference));
Expand Down Expand Up @@ -164,11 +164,12 @@ private static IEnumerable<MetadataReference> GetDefaultMetadataReferences(strin
}
}

private static IEnumerable<string> GetReferenceAssemblies(string assemblyPath)
private static IEnumerable<string> GetReferenceAssemblies(string assemblyPath, MetadataReference[] references)
{
using var assembly = new PEFile(assemblyPath);
var assemblyResolver = new UniversalAssemblyResolver(assemblyPath, false, assembly.DetectTargetFrameworkId());
var result = new Dictionary<string, string>();
Dictionary<string, string>? referenceFiles = default;

GetReferenceAssembliesCore(assembly);

Expand All @@ -179,20 +180,36 @@ void GetReferenceAssembliesCore(PEFile assembly)
var file = assemblyResolver.FindAssemblyFile(reference);
if (file is null)
{
// Skip warning for some weired assembly references: https://github.com/dotnet/docfx/issues/9459
if (reference.Version?.ToString() != "0.0.0.0")
if(referenceFiles == null)
{
Logger.LogWarning($"Unable to resolve assembly reference {reference}", code: "InvalidAssemblyReference");
referenceFiles = new();
foreach (var referenceFile in references.OfType<PortableExecutableReference>())
{
var name = Path.GetFileNameWithoutExtension(referenceFile.FilePath);
if (!string.IsNullOrEmpty(name)
&& !referenceFiles.TryAdd(name, referenceFile.FilePath!))
{
Logger.LogWarning($"Duplicate reference files for '{name}'.", code: "InvalidAssemblyReference");
}
}
}
if(!referenceFiles.TryGetValue(reference.Name, out file))
{
// Skip warning for some weired assembly references: https://github.com/dotnet/docfx/issues/9459
if (reference.Version?.ToString() != "0.0.0.0")
{
Logger.LogWarning($"Unable to resolve assembly reference {reference}", code: "InvalidAssemblyReference");
}

continue;
continue;
}
}

Logger.LogVerbose($"Loaded {reference.Name} from {file}");

using var referenceAssembly = new PEFile(file);
if (result.TryAdd(referenceAssembly.Name, file))
if (result.TryAdd(reference.Name, file))
{
using var referenceAssembly = new PEFile(file);
GetReferenceAssembliesCore(referenceAssembly);
}
}
Expand All @@ -201,7 +218,7 @@ void GetReferenceAssembliesCore(PEFile assembly)
return result.Values;
}

private static MetadataReference CreateMetadataReference(string assemblyPath)
internal static MetadataReference CreateMetadataReference(string assemblyPath)
{
var documentation = XmlDocumentationProvider.CreateFromFile(Path.ChangeExtension(assemblyPath, ".xml"));
return MetadataReference.CreateFromFile(assemblyPath, documentation: documentation);
Expand Down
6 changes: 1 addition & 5 deletions src/Docfx.Dotnet/DotnetApiCatalog.Compile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ await LoadCompilationFromProject(project.AbsolutePath) is { } compilation)
}

var references = config.References ?? [];
var metadataReferences = references.Select(assemblyPath =>
{
var documentation = XmlDocumentationProvider.CreateFromFile(Path.ChangeExtension(assemblyPath, ".xml"));
return MetadataReference.CreateFromFile(assemblyPath, documentation: documentation);
}).ToArray();
var metadataReferences = references.Select(CompilationHelper.CreateMetadataReference).ToArray();

// LoadCompilationFrom C# source files
if (files.TryGetValue(FileType.CSSourceCode, out var csFiles))
Expand Down