From d854cbc8acffe9d16dfdbd50dd77d336a4fe0e0f Mon Sep 17 00:00:00 2001 From: Byron Mayne Date: Sun, 8 Dec 2024 13:09:25 -0500 Subject: [PATCH] Added a method to launch the debugger during initialization --- README.md | 9 +++++++++ src/SourceGenerator.Foundations/HoistSourceGenerator.cs | 2 -- .../Templates/SourceGeneratorHoistBase.cs | 7 +++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c702437..6926624 100644 --- a/README.md +++ b/README.md @@ -237,6 +237,15 @@ driver = driver.RunGenerators(compilation); ``` The only unique feature is the wrapper class `{YourGeneratorName}Host`. This class is an internal feature of `SGF` and is used to make sure all dependencies are resolved before calling into your source generator. + +## Advanced Features +There are some features that are designed for more advanced users or niche cases. + +### Debugger Attaching +When your source generator is being used in the wild it can happen there are exceptions that are thrown which can be hard to track down. There is no nice way to debug these issues besides logging. To get around this you can set the environment variable `SGF_DEBUGGER_LAUNCH=true` the debugger will be launched at the entrypoint of SGF. + + + ## Project Layout This library is made up of quite a few different components leveraging various techniques to help diff --git a/src/SourceGenerator.Foundations/HoistSourceGenerator.cs b/src/SourceGenerator.Foundations/HoistSourceGenerator.cs index ec53197..6f79a2c 100644 --- a/src/SourceGenerator.Foundations/HoistSourceGenerator.cs +++ b/src/SourceGenerator.Foundations/HoistSourceGenerator.cs @@ -18,13 +18,11 @@ internal class HoistSourceGenerator : IIncrementalGenerator { public void Initialize(IncrementalGeneratorInitializationContext context) { - var provider = context.SyntaxProvider .CreateSyntaxProvider( predicate: static (s, _) => IsSyntaxTargetForGeneration(s), transform: static (ctx, _) => GetSemanticTargetForGeneration(ctx)) .Where(static m => m is not null); - context.RegisterSourceOutput(context.AnalyzerConfigOptionsProvider, AddCoreTypes); context.RegisterSourceOutput(provider, static (spc, source) => Execute(source!, spc)); diff --git a/src/SourceGenerator.Foundations/Templates/SourceGeneratorHoistBase.cs b/src/SourceGenerator.Foundations/Templates/SourceGeneratorHoistBase.cs index 2424c0e..bce06dd 100644 --- a/src/SourceGenerator.Foundations/Templates/SourceGeneratorHoistBase.cs +++ b/src/SourceGenerator.Foundations/Templates/SourceGeneratorHoistBase.cs @@ -35,6 +35,13 @@ internal abstract class SourceGeneratorHoist static SourceGeneratorHoist() { + if(bool.TryParse(System.Environment.GetEnvironmentVariable("SGF_DEBUGGER_LAUNCH"), out bool launchDebugger) + && launchDebugger) + { + System.Diagnostics.Debugger.Launch(); + } + + s_assembliesWithResources = new List(); s_loadedAssemblies = new Dictionary(new AssemblyNameComparer()); Initialize();