Skip to content

Commit 2d1bd92

Browse files
Copilotdex3r
andauthored
Preserve executable output kind when evaluating generator methods (top-level statements support) (#78)
* Initial plan * Preserve compilation output kind for generator execution and add top-level statements regression test Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com> * Apply review cleanup and finalize executable output-kind fix Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com> * Add explicit System.Console metadata reference in runtime regression tests Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com> Co-authored-by: Mateusz Krzaczek <dex3r@users.noreply.github.com>
1 parent 59b0d15 commit 2d1bd92

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

EasySourceGenerators.GeneratorTests/GeneratesMethodExecutionRuntimeTests.cs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,39 @@ public static class GenHost
5757
Assert.That(result.value, Is.EqualTo("hello"));
5858
}
5959

60+
[Test]
61+
public void ExecuteSimpleGeneratorMethod_ExecutesWhenCompilationHasTopLevelStatements()
62+
{
63+
CSharpCompilation compilation = CreateCompilation("""
64+
using System;
65+
66+
Console.WriteLine("warmup");
67+
68+
namespace TestNamespace
69+
{
70+
public partial class Target
71+
{
72+
public partial string GetValue();
73+
}
74+
75+
public static class GenHost
76+
{
77+
public static string Generate() => "hello";
78+
}
79+
}
80+
""",
81+
outputKind: OutputKind.ConsoleApplication);
82+
83+
IMethodSymbol generatorMethod = GetMethodSymbol(compilation, "TestNamespace.GenHost", "Generate");
84+
IMethodSymbol partialMethod = GetMethodSymbol(compilation, "TestNamespace.Target", "GetValue");
85+
86+
(string? value, string? error) result =
87+
GeneratesMethodExecutionRuntime.ExecuteSimpleGeneratorMethod(generatorMethod, partialMethod, compilation);
88+
89+
Assert.That(result.error, Is.Null);
90+
Assert.That(result.value, Is.EqualTo("hello"));
91+
}
92+
6093
[Test]
6194
public void ExecuteGeneratorMethodWithArgs_ConvertsArgumentsToMethodParameterType()
6295
{
@@ -253,14 +286,17 @@ public static string Generate()
253286
Assert.That(result.error, Does.StartWith("Compilation failed:"));
254287
}
255288

256-
private static CSharpCompilation CreateCompilation(string source, string assemblyName = "TestAssembly")
289+
private static CSharpCompilation CreateCompilation(
290+
string source,
291+
string assemblyName = "TestAssembly",
292+
OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary)
257293
{
258294
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source);
259295
CSharpCompilation compilation = CSharpCompilation.Create(
260296
assemblyName: assemblyName,
261297
syntaxTrees: new[] { syntaxTree },
262298
references: GetMetadataReferences(),
263-
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
299+
options: new CSharpCompilationOptions(outputKind));
264300
return compilation;
265301
}
266302

@@ -273,6 +309,7 @@ private static ImmutableArray<MetadataReference> GetMetadataReferences()
273309
MetadataReference.CreateFromFile(Path.Combine(dotnetDirectory, "System.Runtime.dll")),
274310
MetadataReference.CreateFromFile(Path.Combine(dotnetDirectory, "System.Collections.dll")),
275311
MetadataReference.CreateFromFile(Path.Combine(dotnetDirectory, "System.Linq.dll")),
312+
MetadataReference.CreateFromFile(Path.Combine(dotnetDirectory, "System.Console.dll")),
276313
MetadataReference.CreateFromFile(Path.Combine(dotnetDirectory, "netstandard.dll")),
277314
MetadataReference.CreateFromFile(typeof(Generate).Assembly.Location),
278315
MetadataReference.CreateFromFile(typeof(GeneratesMethodExecutionRuntime).Assembly.Location)

EasySourceGenerators.Generators/GeneratesMethodExecutionRuntime.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,6 @@ private static CSharpCompilation BuildExecutionCompilation(
345345
?? CSharpParseOptions.Default;
346346

347347
return (CSharpCompilation)compilation
348-
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
349348
.AddSyntaxTrees(
350349
CSharpSyntaxTree.ParseText(dummySource, parseOptions),
351350
CSharpSyntaxTree.ParseText(methodBuilderSource, parseOptions),

0 commit comments

Comments
 (0)