Skip to content

Commit 0e8ad24

Browse files
Copilotlinkdotnet
andcommitted
Fix cacheability by removing IAssemblySymbol from record and using Combine
Co-authored-by: linkdotnet <[email protected]>
1 parent 4305df7 commit 0e8ad24

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

src/bunit.generators.internal/Web.AngleSharp/WrapperElementsGenerator.cs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,23 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2929

3030
var elementInterfaceTypes = FindElementInterfaces(angleSharpAssembly);
3131
// Create cacheable records with just the essential info needed for generation
32+
// Store metadata names instead of symbols for cacheability
3233
return new ElementInterfacesData(
33-
angleSharpAssembly,
3434
elementInterfaceTypes.Select(t => new ElementTypeInfo(
3535
t.Name,
36-
t.ToDisplayString(GeneratorConfig.SymbolFormat)
36+
t.ToDisplayString(GeneratorConfig.SymbolFormat),
37+
GetMetadataName(t)
3738
)).ToImmutableArray());
3839
});
3940

41+
// Combine with compilation to retrieve symbols during execution
42+
var elementInterfacesWithCompilation = elementInterfaces.Combine(context.CompilationProvider);
43+
4044
// Output the hardcoded source files
4145
context.RegisterSourceOutput(elementInterfaces, GenerateStaticContent);
4246

4347
// Output the generated wrapper types
44-
context.RegisterSourceOutput(elementInterfaces, GenerateWrapperTypes);
48+
context.RegisterSourceOutput(elementInterfacesWithCompilation, GenerateWrapperTypes);
4549
}
4650

4751
private static void GenerateStaticContent(SourceProductionContext context, ElementInterfacesData? data)
@@ -54,14 +58,22 @@ private static void GenerateStaticContent(SourceProductionContext context, Eleme
5458
context.AddSource("WrapperBase.g.cs", ReadEmbeddedResource("Bunit.Web.AngleSharp.WrapperBase.cs"));
5559
}
5660

57-
private static void GenerateWrapperTypes(SourceProductionContext context, ElementInterfacesData? data)
61+
private static void GenerateWrapperTypes(SourceProductionContext context, (ElementInterfacesData? data, Compilation compilation) input)
5862
{
63+
var (data, compilation) = input;
5964
if (data is null)
6065
return;
6166

67+
// Find the AngleSharp assembly in the compilation
68+
var meta = compilation.References.FirstOrDefault(x => x.Display?.EndsWith($"{Path.DirectorySeparatorChar}AngleSharp.dll", StringComparison.Ordinal) ?? false);
69+
var assembly = compilation.GetAssemblyOrModuleSymbol(meta);
70+
71+
if (assembly is not IAssemblySymbol angleSharpAssembly)
72+
return;
73+
6274
// Retrieve the actual symbols from the assembly for code generation
6375
var elementSymbols = data.ElementTypes
64-
.Select(t => data.Assembly.GetTypeByMetadataName(t.FullyQualifiedName.Replace("global::", "")))
76+
.Select(t => angleSharpAssembly.GetTypeByMetadataName(t.MetadataName))
6577
.Where(s => s is not null)
6678
.Cast<INamedTypeSymbol>()
6779
.ToList();
@@ -108,6 +120,17 @@ private static void GenerateWrapperFactory(StringBuilder source, ImmutableArray<
108120
source.AppendLine("}");
109121
}
110122

123+
private static string GetMetadataName(INamedTypeSymbol typeSymbol)
124+
{
125+
// Get the full metadata name that can be used with GetTypeByMetadataName
126+
// This is the fully qualified name without the "global::" prefix
127+
var containingNamespace = typeSymbol.ContainingNamespace;
128+
var namespacePrefix = containingNamespace?.IsGlobalNamespace == false
129+
? containingNamespace.ToDisplayString() + "."
130+
: "";
131+
return namespacePrefix + typeSymbol.Name;
132+
}
133+
111134
private static IReadOnlyList<INamedTypeSymbol> FindElementInterfaces(IAssemblySymbol angleSharpAssembly)
112135
{
113136
var htmlDomNamespace = angleSharpAssembly
@@ -164,9 +187,9 @@ private static string ReadEmbeddedResource(string resourceName)
164187
// Cacheable data structure that stores minimal information about element interfaces
165188
// This allows the incremental generator to cache and reuse results across builds
166189
internal sealed record ElementInterfacesData(
167-
IAssemblySymbol Assembly,
168190
ImmutableArray<ElementTypeInfo> ElementTypes);
169191

170192
internal sealed record ElementTypeInfo(
171193
string Name,
172-
string FullyQualifiedName);
194+
string FullyQualifiedName,
195+
string MetadataName);

0 commit comments

Comments
 (0)