Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
fix(should): inject public OverloadResolutionPriorityAttribute polyfi…
…ll in net8 generator tests

Net8.0 BCL lacks OverloadResolutionPriorityAttribute. The Polyfill copy
compiled into the test assembly is internal, so the synthetic GeneratorTest
compilation can't bind constructor arguments — generator returns priority 0
and snapshot snapshot diverges from the verified output that includes
[OverloadResolutionPriority(N)]. Inject a public copy into the compilation
under #if NET8_0 so attribute resolves consistently across all multi-target
TFMs.
  • Loading branch information
thomhurst committed May 1, 2026
commit d0935c08494d4108f959b3f82c7f7edefa16ada1
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,26 @@ public static SetAssertion<TItem> That<TItem>(
/// </summary>
private static async Task<string> RunGenerator(string userSource, [CallerMemberName] string testName = "")
{
// On net8.0 hosts, OverloadResolutionPriorityAttribute is missing from the BCL and the
// Polyfill copy compiled into this test assembly is internal — invisible to the synthetic
// GeneratorTest compilation. Inject a public copy so the attribute resolves consistently
// across all TFMs the test multi-targets.
var inputTrees = new List<SyntaxTree> { CSharpSyntaxTree.ParseText(userSource) };
#if NET8_0
inputTrees.Add(CSharpSyntaxTree.ParseText("""
namespace System.Runtime.CompilerServices;
[System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Constructor | System.AttributeTargets.Property, Inherited = false)]
public sealed class OverloadResolutionPriorityAttribute : System.Attribute
{
public OverloadResolutionPriorityAttribute(int priority) => Priority = priority;
public int Priority { get; }
}
"""));
#endif

var compilation = CSharpCompilation.Create(
assemblyName: "GeneratorTest",
syntaxTrees: [CSharpSyntaxTree.ParseText(userSource)],
syntaxTrees: inputTrees,
references: GetReferences(),
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

Expand All @@ -342,11 +359,11 @@ private static async Task<string> RunGenerator(string userSource, [CallerMemberN
await Assert.That(diagnostics.Length).IsEqualTo(0)
.Because("Generator should not emit diagnostics for valid input");

var trees = updatedCompilation.SyntaxTrees
.Where(t => t != compilation.SyntaxTrees[0])
var generatedTrees = updatedCompilation.SyntaxTrees
.Where(t => !compilation.SyntaxTrees.Contains(t))
.Select(t => t.ToString());

var combined = string.Join("\n//------\n", trees);
var combined = string.Join("\n//------\n", generatedTrees);

await Verify(combined)
.UseFileName($"{nameof(ShouldExtensionGeneratorTests)}.{testName}")
Expand Down
Loading