6

Context

I was able to debug my source generator in VS 2019 by setting the project properties/debug Launch to Roslyn Component, then the target project combo to one of my consuming project:

enter image description here enter image description here

Now this feature seems to be missing in VS 2022, I can not create a launch profile using the Launch Profile UI with similar settings.

Question

How can I debug Roslyn source generator in Visual Studio 2022?

g.pickardou
  • 32,346
  • 36
  • 123
  • 268
  • 1
    bit of a different approach, but I just use a regular test project, and set up the roslyn side in the test: https://github.com/protobuf-net/protobuf-net/tree/main/src/BuildToolsUnitTests – Marc Gravell Jan 19 '22 at 16:31
  • 2
    Many thx. Currently I am putting a Debugger.Launch() into my source generator Initialize(), then building the consuming project, which brings up the pick debugger dialog, where I pick the already running VS instance. – g.pickardou Jan 19 '22 at 16:57
  • yeah, that's a lot of manual work compared to just hitting "debug" on a given test :) – Marc Gravell Jan 19 '22 at 17:10
  • @MarcGravell you should put that as an answer, it's by far the best solution. – Tod May 26 '22 at 08:15

1 Answers1

13

I got it working in VS 2022 17.1.0. Follow this instructions:

https://github.com/JoanComasFdz/dotnet-how-to-debug-source-generator-vs2022

Also make sure in the Visual Studio Installer that you have installed the .NET Compiler Platform SDK.

JoanComasFdz
  • 2,911
  • 5
  • 34
  • 50
  • 2
    Thanks a lot for this @JoanComasFdz. Your "Prerequisites" section says "The .NET Compiler Platform SDK must be installed.". I had assumed it was already installed. Pointing it out here made me doublecheck. – Eric Aug 21 '22 at 09:35
  • 17.7.0 is working for me, plus dont seem to have to restart VS when changing the code gen project. – S Waye Aug 13 '23 at 17:49
  • My bad :-( I did not have the ".NET Compiler Platform SDK" installed. After installing it, works like a charm in VS 17.7.0 installation! – Palo Mraz Aug 14 '23 at 09:45