Skip to content

Commit 270685b

Browse files
committed
warn if .deps.json could not be loaded
1 parent 2d7d610 commit 270685b

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

src/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/AssemblyLoader.netcore.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Runtime.CompilerServices;
1313
using System.Runtime.InteropServices;
1414
using System.Runtime.Loader;
15+
using System.Runtime.Versioning;
1516

1617
using Microsoft.Extensions.Logging;
1718

@@ -63,9 +64,8 @@ private static TResult UsingReflectionOnlyAssembly<TArg, TResult>(
6364
ILogger? logger = null
6465
)
6566
{
66-
using var mlc = new MetadataLoadContext(
67-
new PathAssemblyDependencyResolver(assemblySource.ComponentAssemblyPath)
68-
);
67+
var resolver = new PathAssemblyDependencyResolver(assemblySource.ComponentAssemblyPath);
68+
using var mlc = new MetadataLoadContext(resolver);
6969

7070
logger?.LogDebug(
7171
"loading assembly into reflection-only context (ComponentAssemblyPath: '{ComponentAssemblyPath}')",
@@ -90,6 +90,9 @@ private static TResult UsingReflectionOnlyAssembly<TArg, TResult>(
9090
var assemblyName = assm.FullName;
9191
var assemblyTypeFullName = assm.GetType().FullName;
9292

93+
if (!resolver.HasDepsJsonLoaded)
94+
WarnDepsJsonCouldNotBeLoaded(logger, resolver.PossibleAssemblyDepsJsonPath, assm);
95+
9396
logger?.LogDebug(
9497
"loaded reflection-only assembly '{AssemblyName}' ({AssemblyTypeFullName})",
9598
assemblyName,
@@ -144,6 +147,9 @@ private static TResult UsingAssembly<TArg, TResult>(
144147
var assemblyName = assm.FullName;
145148
var assemblyTypeFullName = assm.GetType().FullName;
146149

150+
if (!alc.HasDepsJsonLoaded)
151+
WarnDepsJsonCouldNotBeLoaded(logger, alc.PossibleAssemblyDepsJsonPath, assm);
152+
147153
logger?.LogDebug(
148154
"loaded assembly '{AssemblyName}' ({AssemblyTypeFullName})",
149155
assemblyName,
@@ -162,5 +168,25 @@ private static TResult UsingAssembly<TArg, TResult>(
162168
logger?.LogDebug("unloaded assembly '{AssemblyName}'", assemblyName);
163169
}
164170
}
171+
172+
private static void WarnDepsJsonCouldNotBeLoaded(
173+
ILogger? logger,
174+
string possibleAssemblyDepsJsonPath,
175+
Assembly assembly
176+
)
177+
{
178+
if (logger is null)
179+
return;
180+
181+
var targetFramework = assembly.GetAssemblyMetadataAttributeValue<TargetFrameworkAttribute, string?>();
182+
183+
if (targetFramework is not null && targetFramework.StartsWith(".NETFramework,", StringComparison.Ordinal))
184+
return;
185+
186+
if (File.Exists(possibleAssemblyDepsJsonPath))
187+
logger.LogWarning("dependency configuration could not be loaded: '{AssemblyDepsJsonPath}'", possibleAssemblyDepsJsonPath);
188+
else
189+
logger.LogWarning("dependency configuration could not be found: '{AssemblyDepsJsonPath}'", possibleAssemblyDepsJsonPath);
190+
}
165191
}
166192
#endif

src/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/PackageDependencyAssemblyResolver.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ string AssemblyDepsJsonPath
5656
}
5757

5858
public DependencyContext? DependencyContext { get; }
59+
public string PossibleAssemblyDepsJsonPath { get; }
5960
private readonly ILogger? logger;
6061

6162
public PackageDependencyAssemblyResolver(string componentAssemblyPath, ILogger? logger = null)
6263
{
63-
(this.DependencyContext, _) = LoadDependencyContextIfDepsJsonExist(componentAssemblyPath, logger);
64+
(DependencyContext, PossibleAssemblyDepsJsonPath) = LoadDependencyContextIfDepsJsonExist(componentAssemblyPath, logger);
6465
this.logger = logger;
6566
}
6667

src/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/PathAssemblyDependencyResolver.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ internal sealed class PathAssemblyDependencyResolver : PathAssemblyResolver {
1818
private readonly PackageDependencyAssemblyResolver packageDependencyResolver;
1919
private readonly ILogger? logger;
2020

21+
public bool HasDepsJsonLoaded => packageDependencyResolver.DependencyContext is not null;
22+
public string PossibleAssemblyDepsJsonPath => packageDependencyResolver.PossibleAssemblyDepsJsonPath;
23+
2124
public PathAssemblyDependencyResolver(string componentAssemblyPath, ILogger? logger = null)
2225
: base(
2326
Directory.GetFiles(RuntimeEnvironment.GetRuntimeDirectory(), "*.dll") // add runtime assemblies

src/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/UnloadableAssemblyLoadContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ internal sealed class UnloadableAssemblyLoadContext : AssemblyLoadContext {
1616
private readonly PackageDependencyAssemblyResolver packageDependencyResolver;
1717
private readonly ILogger? logger;
1818

19+
public bool HasDepsJsonLoaded => packageDependencyResolver.DependencyContext is not null;
20+
public string PossibleAssemblyDepsJsonPath => packageDependencyResolver.PossibleAssemblyDepsJsonPath;
21+
1922
public UnloadableAssemblyLoadContext(string componentAssemblyPath, ILogger? logger = null)
2023
: base(
2124
isCollectible: true // is required to unload assembly

0 commit comments

Comments
 (0)