Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Source Generators debug in vscode

How can I debug the ISourceGenerator code during the build process with vscode? Examples on the net use Debugger.Launch() but this only works in Visual Studio.

    [Generator]
    public class HelloWorldGenerator : ISourceGenerator
    {
        public void Execute(GeneratorExecutionContext context)
        {
      
        }

        public void Initialize(GeneratorInitializationContext context)
        {
            if (!Debugger.IsAttached)
            {
                // how to make it break here?
                Debugger.Launch();
            }
        }
    }
like image 431
jeohd Avatar asked Sep 05 '25 03:09

jeohd


1 Answers

If you add such construction:

while (!System.Diagnostics.Debugger.IsAttached)
    System.Threading.Thread.Sleep(500);

you will be able to stop build process until you attach any debugger you want. I use Rider IDE, but this should work for VS Code too. When it stucks in cycle, it is available for attaching as .NET Core process: .NET Core process

like image 156
Dmitry Vornychev Avatar answered Sep 08 '25 10:09

Dmitry Vornychev