One of the new features in .NET 5 that triggered my curiosity where source generators. I did some investigation on how to use them and want to share the lessons I learned along the way.
But let’s first start with an explanation what “a source generator” actually is:
A Source Generator is a new kind of component that C# developers can write that lets you do two major things:
- Retrieve a Compilation object that represents all user code that is being compiled. This object can be inspected and you can write code that works with the syntax and semantic models for the code being compiled, just like with analyzers today.
- Generate C# source files that can be added to a Compilation object during the course of compilation. In other words, you can provide additional source code as input to a compilation while the code is being compiled.
So in short, source generators are a new compiler feature that allow you to inspect existing code and generate new code(remark: you cannot change existing code).
I started with the example mentioned in the blog post above:
[Generator] | |
public class HelloWorldGenerator : ISourceGenerator { | |
public void Execute(GeneratorExecutionContext context) | |
{ | |
// begin creating the source we'll inject into the users compilation | |
var sourceBuilder = new StringBuilder(@" | |
using System; | |
namespace HelloWorldGenerated | |
{ | |
public static class HelloWorld | |
{ | |
public static void SayHello() | |
{ | |
Console.WriteLine(""Hello from generated code!""); | |
Console.WriteLine(""The following syntax trees existed in the compilation that created this program:""); | |
"); | |
// using the context, get a list of syntax trees in the users compilation | |
var syntaxTrees = context.Compilation.SyntaxTrees; | |
// add the filepath of each tree to the class we're building | |
foreach (SyntaxTree tree in syntaxTrees) | |
{ | |
sourceBuilder.AppendLine($@"Console.WriteLine(@"" - {tree.FilePath}"");"); | |
} | |
// finish creating the source to inject | |
sourceBuilder.Append(@" | |
} | |
} | |
}"); | |
// inject the created source into the users compilation | |
context.AddSource("helloWorldGenerator", SourceText.From(sourceBuilder.ToString(), Encoding.UTF8)); | |
} | |
public void Initialize(GeneratorInitializationContext context) | |
{ | |
// No initialization required for this one | |
} | |
} |
But when I build my project, nothing happened and it looked like no code was generated. What was I doing wrong?
How to debug a source generator?
So the first question became how can I debug a source generator? Now I had no clue how to investigate what was (not) happening.
The trick is to add a `Debugger.Launch()` statement inside your code:
public void Initialize(GeneratorInitializationContext context) | |
{ | |
Debugger.Launch(); | |
} |
Now when you build your code a popup should appear that asks you how you what debugger you want to launch.