diff --git a/EasySourceGenerators.GeneratorTests/GeneratesMethodExecutionRuntimeTests.cs b/EasySourceGenerators.GeneratorTests/GeneratesMethodExecutionRuntimeTests.cs index cbaf263..2da4545 100644 --- a/EasySourceGenerators.GeneratorTests/GeneratesMethodExecutionRuntimeTests.cs +++ b/EasySourceGenerators.GeneratorTests/GeneratesMethodExecutionRuntimeTests.cs @@ -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"); + + 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() { @@ -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; } @@ -273,6 +309,7 @@ private static ImmutableArray 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) diff --git a/EasySourceGenerators.Generators/GeneratesMethodExecutionRuntime.cs b/EasySourceGenerators.Generators/GeneratesMethodExecutionRuntime.cs index 723a93a..8efb3fa 100644 --- a/EasySourceGenerators.Generators/GeneratesMethodExecutionRuntime.cs +++ b/EasySourceGenerators.Generators/GeneratesMethodExecutionRuntime.cs @@ -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),