Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better tooling support to debug source generators (breakpoints) #51962

Closed
Sergio0694 opened this issue Mar 18, 2021 · 6 comments
Closed

Better tooling support to debug source generators (breakpoints) #51962

Sergio0694 opened this issue Mar 18, 2021 · 6 comments

Comments

@Sergio0694
Copy link
Contributor

Overview

Since I started using source generators (which again, are brilliant!), I find the debugging experience particularly clunky. Not sure if I'm using the wrong approach here, but more or less my workflow to investigate crashes in my source generators is this:

  • I notice that one of my generator isn't working (or see it log a diagnostic for an exception)
  • I add a Debugger.Launch(); call at the first line of the Execute method in that source generator
  • I rebuild the source generator project
  • I rebuild the project using the source generator
  • VS shows the popup about an unhandled exception (ie. Debugger.Launch();)
  • I click "Ok" to debug it into VS, which switches to the debugger and breaks in the source generator
  • From there I happily go about actually investigating the exception

Now, this is somewhat clunky but overall not the worst, and it works fine. The issue though is that since Roslyn just tries to run the generator all the time, once you add that line you basically start having VS open the popup "an exception was thrown, launch a debugger?" every 2 seconds, forever. You basically can't work anymore as that window keeps popping up constantly: you have to delete that line, rebuild the generator, clean the other project and then it stops. If you close VS and reopen it, that popup starts showing up every 2 seconds again unless you've also cleaned the solution (since the compiled generator references is cached by VS.

The main issue is that if you just try to add a breakpoint into your source generator, and then rebuild the project using it, that breakpoint will just never actually be hit, despite Roslyn running the shader just fine. Same for using the "Exceptions Settings" window, even if you check an exception to have VS break on it, that doesn't work for a source generator.

Proposal

Would it be possible to just have breakpoints work from source generators? 😄

As in, so that if you set one and rebuild a project referencing the generator, the breakpoint would just be hit as expected. Then after that you could just remove it and go about your day. Since the generated assembly wouldn't be changed (no more Debugger.Launch() call), that isssue with VS just spamming those popups would go away as well.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Interactive untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 18, 2021
@jmarolf
Copy link
Contributor

jmarolf commented Mar 18, 2021

CC: @jasonmalinowski and @chsienki

we are working something like this to improve the experience here: dotnet/roslyn-sdk#738 that should make it into Visual Studio 16.10 Preview 2. The intent is that you just set a break point and start debugging as you describe.

Chris has also built https://github.com/chsienki/Kittitas which I hope to leverage so we can make debugging source generators for developers on linux an easy thing to accomplish.

@Sergio0694
Copy link
Contributor Author

That's awesome, glad to hear this is already in the works and coming soon!
Being able to just use breakpoints will make debugging SGs so much better and less frustrating! 🥳

@jinujoseph jinujoseph added Concept-Continuous Improvement and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 23, 2021
@jinujoseph jinujoseph added this to the 16.10 milestone Mar 23, 2021
@jasonmalinowski jasonmalinowski removed their assignment Mar 23, 2021
@jinujoseph jinujoseph modified the milestones: 16.10, 17.0 Jun 1, 2021
@chsienki
Copy link
Member

This is now supported via generator debugging the roslyn sdk.

@Sergio0694
Copy link
Contributor Author

Just double checking I'm not getting something wrong, I'm not sure I'm follow what the suggested approach is here.

"that should make it into Visual Studio 16.10 Preview 2. The intent is that you just set a break point and start debugging as you describe."

I'm using VS2022 Preview 3, and just trying to add a breakpoint into a generator and then building a project consuming the generator doesn't actually seem to be working, the breakpoint is never hit and enabled. I still have to add a Debugger.Launch() call in my Execute method in the generator, then rebuild the generator, then rebuild the consuming project, then click OK in the popup from VS saying an exception was hit, and finally I can enter debug mode at my breakpoint 🤔

@jasonmalinowski
Copy link
Member

@Sergio0694 There's a bit of setup you do -- you set the generator project as the thing being debugged and you pick one of your other projects in the project properties. There's some documentation here: http://stevetalkscode.co.uk/debug-source-generators-with-vs2019-1610

@chsienki Did we write docs anywhere for this?

@Sergio0694
Copy link
Contributor Author

Just to circle back on this - I realize the issue has been closed as completely but the debugging situation still seems not ideal. I can't find any official docs on MS Docs to debug source generators, and even just reading that blog post the setup seems clunky enough (extra VS workload needed, have to edit the .csproj, then edit the properties, etc.) that I find myself still just literally adding a Debugger.Launch() call in my generator, and then just do that. Like, if you add that line then you can literally just debug the generator normally when you build a project consuming it, and you don't need any additional workload or project property to get that to work. Could we just get better tooling for this? As in, would it be possible to just have some "Debug generator" option that would let you pick a project referencing the generator, build that, and just automatically launch the debugger when the generator is launched? That would seem like a much much better UX for generator authors 🙂

As in, the same steps are already possible (and I use them all the time), so it'd just be about providing better tooling for it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants