Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,39 @@ public static class GenHost
Assert.That(result.value, Is.EqualTo("hello"));
}

[Test]
public void ExecuteSimpleGeneratorMethod_ExecutesWhenCompilationHasTopLevelStatements()
{
CSharpCompilation compilation = CreateCompilation("""
using System;

Console.WriteLine("warmup");
Comment on lines +64 to +66
Copy link

Copilot AI Mar 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new regression test’s top-level statement uses Console.WriteLine, but GetMetadataReferences() doesn’t explicitly reference System.Console (it relies on whatever assemblies happen to already be loaded in the AppDomain). That can make the test environment-dependent/flaky if System.Console isn’t loaded. Consider using a top-level statement that doesn’t require extra references (e.g., int _ = 0;), or explicitly add a System.Console metadata reference for determinism.

Suggested change
using System;
Console.WriteLine("warmup");
int _ = 0;

Copilot uses AI. Check for mistakes.

namespace TestNamespace
{
public partial class Target
{
public partial string GetValue();
}

public static class GenHost
{
public static string Generate() => "hello";
}
}
""",
outputKind: OutputKind.ConsoleApplication);

IMethodSymbol generatorMethod = GetMethodSymbol(compilation, "TestNamespace.GenHost", "Generate");
IMethodSymbol partialMethod = GetMethodSymbol(compilation, "TestNamespace.Target", "GetValue");

(string? value, string? error) result =
GeneratesMethodExecutionRuntime.ExecuteSimpleGeneratorMethod(generatorMethod, partialMethod, compilation);

Assert.That(result.error, Is.Null);
Assert.That(result.value, Is.EqualTo("hello"));
}

[Test]
public void ExecuteGeneratorMethodWithArgs_ConvertsArgumentsToMethodParameterType()
{
Expand Down Expand Up @@ -253,14 +286,17 @@ public static string Generate()
Assert.That(result.error, Does.StartWith("Compilation failed:"));
}

private static CSharpCompilation CreateCompilation(string source, string assemblyName = "TestAssembly")
private static CSharpCompilation CreateCompilation(
string source,
string assemblyName = "TestAssembly",
OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary)
{
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source);
CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName: assemblyName,
syntaxTrees: new[] { syntaxTree },
references: GetMetadataReferences(),
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
options: new CSharpCompilationOptions(outputKind));
return compilation;
}

Expand All @@ -273,6 +309,7 @@ private static ImmutableArray<MetadataReference> GetMetadataReferences()
MetadataReference.CreateFromFile(Path.Combine(dotnetDirectory, "System.Runtime.dll")),
MetadataReference.CreateFromFile(Path.Combine(dotnetDirectory, "System.Collections.dll")),
MetadataReference.CreateFromFile(Path.Combine(dotnetDirectory, "System.Linq.dll")),
MetadataReference.CreateFromFile(Path.Combine(dotnetDirectory, "System.Console.dll")),
MetadataReference.CreateFromFile(Path.Combine(dotnetDirectory, "netstandard.dll")),
MetadataReference.CreateFromFile(typeof(Generate).Assembly.Location),
MetadataReference.CreateFromFile(typeof(GeneratesMethodExecutionRuntime).Assembly.Location)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ private static CSharpCompilation BuildExecutionCompilation(
?? CSharpParseOptions.Default;

return (CSharpCompilation)compilation
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
.AddSyntaxTrees(
CSharpSyntaxTree.ParseText(dummySource, parseOptions),
CSharpSyntaxTree.ParseText(methodBuilderSource, parseOptions),
Expand Down
Loading