@@ -18,6 +18,9 @@ namespace Refit.GeneratorTests;
1818 Justification = "Compiles generator inputs against on-disk assemblies; never run as a single-file app." ) ]
1919public static class Fixture
2020{
21+ /// <summary>The runtime data key containing framework assemblies available to the current test host.</summary>
22+ private const string TrustedPlatformAssemblies = "TRUSTED_PLATFORM_ASSEMBLIES" ;
23+
2124 /// <summary>The metadata reference for the Refit assembly with documentation.</summary>
2225 private static readonly MetadataReference _refitAssembly = MetadataReference . CreateFromFile (
2326 typeof ( GetAttribute ) . Assembly . Location ,
@@ -240,15 +243,23 @@ public static string GenerateForDeclaration(
240243 /// <returns>The created compilation.</returns>
241244 public static CSharpCompilation CreateLibrary ( params SyntaxTree [ ] source )
242245 {
243- var references = new List < MetadataReference > ( ) ;
246+ var referencePaths = new HashSet < string > ( StringComparer . Ordinal ) ;
247+ AddTrustedPlatformAssemblies ( referencePaths ) ;
248+
244249 foreach ( var assembly in GetAssemblyReferencesForCodegen ( ) )
245250 {
246251 if ( ! assembly . IsDynamic && ! string . IsNullOrEmpty ( assembly . Location ) )
247252 {
248- references . Add ( MetadataReference . CreateFromFile ( assembly . Location ) ) ;
253+ referencePaths . Add ( assembly . Location ) ;
249254 }
250255 }
251256
257+ var references = new List < MetadataReference > ( referencePaths . Count + 1 ) ;
258+ foreach ( var referencePath in referencePaths )
259+ {
260+ references . Add ( MetadataReference . CreateFromFile ( referencePath ) ) ;
261+ }
262+
252263 references . Add ( _refitAssembly ) ;
253264 return CSharpCompilation . Create (
254265 "compilation" ,
@@ -274,6 +285,28 @@ .. AppDomain.CurrentDomain
274285 private static CSharpCompilation CreateLibrary ( params string [ ] source ) =>
275286 CreateLibrary ( source . Select ( s => CSharpSyntaxTree . ParseText ( s ) ) . ToArray ( ) ) ;
276287
288+ /// <summary>Adds runtime framework references used by Roslyn in-memory compilations.</summary>
289+ /// <param name="referencePaths">The reference path set to populate.</param>
290+ private static void AddTrustedPlatformAssemblies ( HashSet < string > referencePaths )
291+ {
292+ var trustedPlatformAssemblies = ( string ? ) AppContext . GetData ( TrustedPlatformAssemblies ) ;
293+ if ( string . IsNullOrEmpty ( trustedPlatformAssemblies ) )
294+ {
295+ return ;
296+ }
297+
298+ // CI hosts can type-forward BCL types, such as Uri, to implementation assemblies
299+ // that are not loaded yet. The trusted platform assembly list gives Roslyn the
300+ // complete runtime framework closure for generated-output compilation tests.
301+ foreach ( var referencePath in trustedPlatformAssemblies . Split ( Path . PathSeparator ) )
302+ {
303+ if ( ! string . IsNullOrEmpty ( referencePath ) )
304+ {
305+ referencePaths . Add ( referencePath ) ;
306+ }
307+ }
308+ }
309+
277310 /// <summary>Determines whether a diagnostic represents an error in generator output.</summary>
278311 /// <param name="diagnostic">The diagnostic to inspect.</param>
279312 /// <returns><see langword="true"/> when the diagnostic should fail generated-output compilation tests.</returns>
0 commit comments