GithubHelp home page GithubHelp logo

caseymarquis / dotnet-how-to-debug-source-generator-vs2022 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from joancomasfdz/dotnet-how-to-debug-source-generator-vs2022

0.0 0.0 0.0 7 KB

Step by step guide on how to debug a C# SourceGenerator in Visual Studio 2022

dotnet-how-to-debug-source-generator-vs2022's Introduction

dotnet-how-to-debug-source-generator-vs2022

Step by step guide on how to debug a C# SourceGenerator in Visual Studio 2022

Valid as of 1st March 2022 with Visual Studio 2022 Version 17.1.0.

Source: Github | Roslyn SDK | Support source generator debugging in the new Launch Profiles UI #850

Prerequisites

The .NET Compiler Platform SDK must be installed.

Solution structure

  1. Add a .NET Standard 2.0 class library for your SourceGenerator.
  2. Set the SourceGenerator projet as Startup project.
  3. Edit the SourceGenerator project and replace all with:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <Nullable>enable</Nullable>
    <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
    <CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
    <IsRoslynComponent>true</IsRoslynComponent>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.2.0" PrivateAssets="all" />
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
  </ItemGroup>

</Project>
  1. Save it.
  2. Add a Console Application project with some examples of code you would like to test the SourceGenerator with.
  3. Edit the Console Application porject and add a reference to the SourceGenerator this way:
  <ItemGroup>
    <ProjectReference
      Include="..\MySourceGenerator\MySourceGenerator.csproj"
      OutputItemType="Analyzer"
      ReferenceOutputAssembly="false"/>
  </ItemGroup>
  1. Save it.

Debug Configuration

  1. Right click on the SourceGenerator project
  2. Click Properties.
  3. Click Debug.
  4. Click Open debug launch profiles UI.
  5. Click on Delete to delete the profile shown.
  6. Click on Add
  7. Select Roslyn component.
  8. In Target project select the Console Application project.
  9. Close the UI.
  10. Restart Visual Studio 2022.
  11. In the debug profiles dropdown next to the Play button, select your SourceGenerator project.
  12. Put a break point in your SourceGenerator to make sure the debugger stops.
  13. Click Play.

Notes

Every time you change your source generator code, you will need to restart Visual Studio, otherwise Rebuilding the target project will not use the new version. This has something to do with Visual Studio caching.

Sources:

dotnet-how-to-debug-source-generator-vs2022's People

Contributors

joancomasfdz avatar caseymarquis avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.