In this post, we will look into how to Debug Source Generator using Visual Studio 2019. The prerequisite is to ensure you have Visual Studio 2019 16.10 or above.
Prerequisite
- Ensure you have Visual Studio 2019 16.10 or above.
- Ensure .Net Compiler Platform SDK is installed.
To install .Net Compiler Platform SDK, launch the Visual Studio Installer and verify/install the component under Visual Studio Extension.
Step 1 : IsRosylnComponent
Edit your Generator Project and add following Tag.
<IsRoslynComponent>true</IsRoslynComponent>
Your PropertyGroup would look like the following now.
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsRoslynComponent>true</IsRoslynComponent>
</PropertyGroup>
Step 2: Set Reference In Client Project
After you have added a reference to Generator Project, ensure you set the OutputItemType
and ReferenceOutputAssembly
in the ProjectReference
as Analyzer and false respectively.
Edit your Client Project File, and ensure your reference look as the following.
<ProjectReference Include="..\InGen.Generators\InGen.Generators.csproj"
OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
Step 3: Set Debug Options
Navigate to the Generator Project in Solution Explorer and Select Properties. In the Debug Tab, ensure the following Select Roslyn Component for Launch. Select your client project as the Target Project.
Step 4: Set Start up Project.
The final step involves setting your Generator Project as the Start Up Project. Now you can set the debugger points in your Generator Project just like you have been doing in your regular Projects.

Happy Weekend !!
How does this work in VS 2022? The UI for editing project properties has changed.
LikeLike
Thank you for bringing this up. It looks like things have changed quite a bit there. I will post a reply as soon as I figure that out myself.
LikeLike
In case you are still wondering how to do it, you can find more information about this in a gihub issue, where the UI development was trackedhttps://github.com/dotnet/roslyn-sdk/issues/850
There is even a video attached, where to find the debug launch options with the new property settings UI
LikeLike
Thank you 😊
LikeLike