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