Skip to content

Commit ab00bf5

Browse files
committed
Give each LoadAndInvokeMain compilation a unique assembly name
Every generator test project shares the same assembly name ('test', from RoslynTestUtils.CreateTestProject), and LoadAndInvokeMain loads into AssemblyLoadContext.Default, which never unloads. When more than one theory case in this test run reaches LoadAndInvokeMain, the second load collides with the first under the identical assembly identity (FileLoadException: a different copy of assembly 'test.dll' is already loaded). Rename the compilation to a fresh unique name right before emitting so concurrent/sequential loads in the same process don't collide. Also note in a test comment that nested binding of this same shape is tracked separately in #131399, filed per review discussion on this PR.
1 parent 16c022b commit ab00bf5

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

‎src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/GeneratorTests.Helpers.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,12 @@ private static byte[] CreateAssemblyImage(Compilation compilation)
240240
/// </summary>
241241
private static object? LoadAndInvokeMain(Compilation compilation, string resultFieldName)
242242
{
243+
// Every test project shares the same assembly name ("test", from RoslynTestUtils.CreateTestProject),
244+
// and AssemblyLoadContext.Default can't unload. If more than one theory case in a run reaches this
245+
// method, loading the second image collides with the first under the identical identity. Give each
246+
// load a unique name so they can coexist.
247+
compilation = compilation.WithAssemblyName($"test_{Guid.NewGuid():N}");
248+
243249
byte[] image = CreateAssemblyImage(compilation);
244250
Assembly assembly = AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(image));
245251

‎src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/GeneratorTests.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,10 @@ public async Task SoleReadOnlyCollectionConstructorParameterIsBindable(string co
552552
// Regression test: a type whose only member is a non-bindable, read-only collection
553553
// constructor parameter (no other bindable property) used to make the generator emit a
554554
// call to an Initialize method that was never generated, producing CS0103 at compile time.
555+
//
556+
// This only covers the top-level GetCore path. Binding this same shape as a *nested*
557+
// member (reached via BindCore/EmitObjectInit) silently produces null instead of the
558+
// real value, a separate pre-existing bug tracked in dotnet/runtime#131399.
555559
string source = $$"""
556560
using Microsoft.Extensions.Configuration;
557561
using System.Collections.Generic;

0 commit comments

Comments
 (0)