Debugging C# Source Generator

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 !!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s