vscode-csharp icon indicating copy to clipboard operation
vscode-csharp copied to clipboard

Add support for debugging of source generators

Open DevAM-Tools opened this issue 1 year ago • 2 comments

I want to request support for debugging of source generators.

When developing a source generator having the possibility to debug the application is equally important as it is for any other application. Therefore debugging an source generator should be as easy as it is for any other dotnet application.

DevAM-Tools avatar Aug 07 '25 06:08 DevAM-Tools

I don't know how feasible this is -- the language service and compiler are going to load your source generator which means you really need a separate instance of VS / VS Code that just has your source generator opened.

Your best bet is probably to add something like:

#pragma warning disable RS1035 // Do not use APIs banned for analyzers
        // Wait for the debugger to attach if the environment variable is set
        if (Environment.GetEnvironmentVariable("DEBUG_MY_SOURCE_GENERATOR")?.Equals("1") ?? false)
        {
            
            while (!Debugger.IsAttached)
            {
                Thread.Sleep(100);
            }
        }
#pragma warning restore RS1035 // Do not use APIs banned for analyzers

Then open two instances of VS Code (or VS if you are on Windows).

gregg-miskelly avatar Aug 07 '25 16:08 gregg-miskelly

Resolving this as a duplicate of #8117

AbhitejJohn avatar Aug 11 '25 17:08 AbhitejJohn