Skip to content
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
fix: add logics to skip non-managed assembly
  • Loading branch information
filzrev committed May 28, 2025
commit f803a1de9cde0b8529760ac5fb852bd07151b3a3
12 changes: 7 additions & 5 deletions src/Docfx.Dotnet/CompilationHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.


using System.Reflection.PortableExecutable;
using Docfx.Common;
using Docfx.Exceptions;
using ICSharpCode.Decompiler.Metadata;
Expand Down Expand Up @@ -103,7 +103,7 @@ public static Compilation CreateCompilationFromVBCode(string code, IDictionary<s
references: GetDefaultMetadataReferences("VB").Concat(references ?? []));
}

public static (Compilation, IAssemblySymbol) CreateCompilationFromAssembly(string assemblyPath, bool includePrivateMembers = false, params MetadataReference[] references)
public static (Compilation, IAssemblySymbol) CreateCompilationFromAssembly(string assemblyPath, PEReader? peReader = null, bool includePrivateMembers = false, params MetadataReference[] references)
{
var metadataReference = CreateMetadataReference(assemblyPath);
var compilation = CS.CSharpCompilation.Create(
Expand All @@ -115,7 +115,7 @@ public static (Compilation, IAssemblySymbol) CreateCompilationFromAssembly(strin
: MetadataImportOptions.Public
),
syntaxTrees: s_assemblyBootstrap,
references: GetReferenceAssemblies(assemblyPath, references)
references: GetReferenceAssemblies(assemblyPath, peReader, references)
.Select(CreateMetadataReference)
.Concat(references ?? [])
.Append(metadataReference));
Expand Down Expand Up @@ -164,9 +164,11 @@ private static IEnumerable<MetadataReference> GetDefaultMetadataReferences(strin
}
}

private static IEnumerable<string> GetReferenceAssemblies(string assemblyPath, MetadataReference[] references)
private static IEnumerable<string> GetReferenceAssemblies(string assemblyPath, PEReader? peReader, MetadataReference[] references)
{
using var assembly = new PEFile(assemblyPath);
using var assembly = peReader == null
? new PEFile(assemblyPath)
: new PEFile(assemblyPath, peReader);
var assemblyResolver = new UniversalAssemblyResolver(assemblyPath, false, assembly.DetectTargetFrameworkId());
var result = new Dictionary<string, string>();
Dictionary<string, string>? referenceFiles = default;
Expand Down
14 changes: 12 additions & 2 deletions src/Docfx.Dotnet/DotnetApiCatalog.Compile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Reflection.PortableExecutable;
using Docfx.Common;
using Microsoft.Build.Construction;
using Microsoft.Build.Framework;
Expand Down Expand Up @@ -109,8 +110,17 @@ await LoadCompilationFromProject(project.AbsolutePath) is { } compilation)
{
foreach (var assemblyFile in assemblyFiles)
{
Logger.LogInfo($"Loading assembly {assemblyFile.NormalizedPath}");
var (compilation, assembly) = CompilationHelper.CreateCompilationFromAssembly(assemblyFile.NormalizedPath, config.IncludePrivateMembers, metadataReferences);
var normalizedAssemblyPath = assemblyFile.NormalizedPath;

using var peReader = new PEReader(new FileStream(normalizedAssemblyPath, FileMode.Open, FileAccess.Read));
if (!peReader.HasMetadata)
{
Logger.LogInfo($"Skip non-managed assembly {normalizedAssemblyPath}");
continue;
}

Logger.LogInfo($"Loading assembly {normalizedAssemblyPath}");
var (compilation, assembly) = CompilationHelper.CreateCompilationFromAssembly(normalizedAssemblyPath, peReader, config.IncludePrivateMembers, metadataReferences);
hasCompilationError |= compilation.CheckDiagnostics(config.AllowCompilationErrors);
assemblies.Add((assembly, compilation));
}
Expand Down