Skip to content

Commit

Permalink
Merge pull request #31 from ByronMayne/27-add-debugger-attach-point
Browse files Browse the repository at this point in the history
Added method for launching a debugger during type initialization
  • Loading branch information
ByronMayne authored Dec 8, 2024
2 parents cb1ffbc + d854cbc commit 34708c0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/SourceGenerator.Foundations/HoistSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Assembly>();
s_loadedAssemblies = new Dictionary<AssemblyName, Assembly>(new AssemblyNameComparer());
Initialize();
Expand Down

0 comments on commit 34708c0

Please sign in to comment.