@@ -17,6 +17,8 @@ namespace NUnit.Engine.Internal
17
17
{
18
18
internal sealed class TestAssemblyResolver : IDisposable
19
19
{
20
+ private static readonly Logger log = InternalTrace . GetLogger ( typeof ( TestAssemblyResolver ) ) ;
21
+
20
22
private readonly ICompilationAssemblyResolver _assemblyResolver ;
21
23
private readonly DependencyContext _dependencyContext ;
22
24
private readonly AssemblyLoadContext _loadContext ;
@@ -62,6 +64,14 @@ public Assembly Resolve(AssemblyLoadContext context, AssemblyName name)
62
64
63
65
private Assembly OnResolving ( AssemblyLoadContext context , AssemblyName name )
64
66
{
67
+ context = context ?? _loadContext ;
68
+
69
+ if ( TryLoadFromTrustedPlatformAssemblies ( context , name , out var loadedAssembly ) )
70
+ {
71
+ log . Info ( "'{0}' assembly is loaded from trusted path '{1}'" , name , loadedAssembly . Location ) ;
72
+ return loadedAssembly ;
73
+ }
74
+
65
75
foreach ( var library in _dependencyContext . RuntimeLibraries )
66
76
{
67
77
var wrapper = new CompilationLibrary (
@@ -79,24 +89,81 @@ private Assembly OnResolving(AssemblyLoadContext context, AssemblyName name)
79
89
foreach ( var assemblyPath in assemblies )
80
90
{
81
91
if ( name . Name == Path . GetFileNameWithoutExtension ( assemblyPath ) )
82
- return _loadContext . LoadFromAssemblyPath ( assemblyPath ) ;
92
+ {
93
+ loadedAssembly = context . LoadFromAssemblyPath ( assemblyPath ) ;
94
+ log . Info ( "'{0}' ({1}) assembly is loaded from runtime libraries {2} dependencies" ,
95
+ name ,
96
+ loadedAssembly . Location ,
97
+ library . Name ) ;
98
+
99
+ return loadedAssembly ;
100
+ }
83
101
}
84
102
}
85
103
104
+ if ( name . Version == null )
105
+ {
106
+ return null ;
107
+ }
108
+
86
109
foreach ( string frameworkDirectory in AdditionalFrameworkDirectories )
87
110
{
88
111
var versionDir = FindBestVersionDir ( frameworkDirectory , name . Version ) ;
112
+
89
113
if ( versionDir != null )
90
114
{
91
115
string candidate = Path . Combine ( frameworkDirectory , versionDir , name . Name + ".dll" ) ;
92
116
if ( File . Exists ( candidate ) )
93
- return _loadContext . LoadFromAssemblyPath ( candidate ) ;
117
+ {
118
+ loadedAssembly = context . LoadFromAssemblyPath ( candidate ) ;
119
+ log . Info ( "'{0}' ({1}) assembly is loaded from AdditionalFrameworkDirectory {2} dependencies with best candidate version {3}" ,
120
+ name ,
121
+ loadedAssembly . Location ,
122
+ frameworkDirectory ,
123
+ versionDir ) ;
124
+
125
+ return loadedAssembly ;
126
+ }
127
+ else
128
+ {
129
+ log . Debug ( "Best version dir for {0} is {1}, but there is no {2} file" , frameworkDirectory , versionDir , candidate ) ;
130
+ }
94
131
}
95
132
}
96
133
134
+ log . Info ( "Cannot resolve assembly '{0}'" , name ) ;
97
135
return null ;
98
136
}
99
137
138
+ private static bool TryLoadFromTrustedPlatformAssemblies ( AssemblyLoadContext context , AssemblyName assemblyName , out Assembly loadedAssembly )
139
+ {
140
+ // https://learn.microsoft.com/en-us/dotnet/core/dependency-loading/default-probing
141
+ loadedAssembly = null ;
142
+ var trustedAssemblies = System . AppContext . GetData ( "TRUSTED_PLATFORM_ASSEMBLIES" ) as string ;
143
+ if ( string . IsNullOrEmpty ( trustedAssemblies ) )
144
+ {
145
+ return false ;
146
+ }
147
+
148
+ var separator = RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ? ";" : ":" ;
149
+ foreach ( var assemblyPath in trustedAssemblies . Split ( separator ) )
150
+ {
151
+ var fileName = Path . GetFileNameWithoutExtension ( assemblyPath ) ;
152
+ if ( string . Equals ( fileName , assemblyName . Name , StringComparison . InvariantCultureIgnoreCase ) == false )
153
+ {
154
+ continue ;
155
+ }
156
+
157
+ if ( File . Exists ( assemblyPath ) )
158
+ {
159
+ loadedAssembly = context . LoadFromAssemblyPath ( assemblyPath ) ;
160
+ return true ;
161
+ }
162
+ }
163
+
164
+ return false ;
165
+ }
166
+
100
167
private static string GetDotNetInstallDirectory ( )
101
168
{
102
169
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
@@ -119,9 +186,11 @@ private static string FindBestVersionDir(string libraryDir, Version targetVersio
119
186
{
120
187
Version version ;
121
188
if ( TryGetVersionFromString ( Path . GetFileName ( subdir ) , out version ) )
189
+ {
122
190
if ( version >= targetVersion )
123
191
if ( bestVersion . Major == 0 || bestVersion > version )
124
192
bestVersion = version ;
193
+ }
125
194
}
126
195
127
196
return bestVersion . Major > 0
0 commit comments