šDevelop Roslyn Source Generator in Rider
tags
.NET
type
Post
summary
status
Published
slug
develop-roslyn-source-generator-in-rider
date
Nov 21, 2023
Whatās .NET source generatorSample solutionDevelopment workflow in RiderKnown issues / Limitations / WorkaroundsSome problems related to Roslyn Analyzer
Whatās .NET source generator
.NET source generator is a kind of Roslyn analyzer, which will inspect your C# code on building stage, generate code based on created templates. The additional sources of code could be executed with the program, so that you donāt need to write unnecessary code in your project.
Here are some basic definitions of the source generator usage.
- retrieve compilation representing user code
- with syntax trees
- with semantic models
- new source code can be added to the compilation as additional source code
- 0..n
- as strings
- additive only, which means source generator could not change the code in the project.
- can produce diagnostics
- you may access additional files (non-C# source texts)
- unordered
- no access to files created by other source generators
- are technical analyzers
Sample solution
Here is a quite simple source generator solution, it can read all Classes name in the project, store the classes name in a generated string array.
- The SampleGenerator project is a .NET Standard 2.0 class library. Declared in
.csproj
file.
- The TestGenerator is a console app, invoke generated
SampleCodeGenerator
namespace, please notice itās not as same as source generatorās namespace. When opening the project in Rider, sinceSampleCodeGenerator
hasnāt generated, it will show errors in TestGenerator project.
Build the solution and run TestGenerator project, the generated source is available in the Source Generators folder.
Development workflow in Rider
Since source generator works on the project building stage, usually the debugger is attached to the running.NET process, so there is different configuration for debugging generated code and source generator itself.
- Debug source generator itself:
Users will need to create a new section in source generator projectsā
launchsettings.json
and add below contents.{ "$schema": "http://json.schemastore.org/launchsettings.json", "profiles": { "Generators": { "commandName": "DebugRoslynComponent", "targetProject": "../TestGenerator/TestGenerator.csproj" } } }
Then start a debugging session from this profile. This method is mentioned in this blog as well.
- Debug generated source:
First, build the project using the source generator. Then users will be able to add breakpoints in generated source files directly.
An alternative way is to add breakpoints in the project and step into the generated source file.
Known issues / Limitations / Workarounds
- Debugging generated project files.
In some cases, users may want to set breakpoint in
*.g.cs
generated sources that are used for navigation, and the final target implementations are in the project files. Itās impossible to add breakpoints directly in the *.g.cs
which is used for navigation.To work around this issue, the user needs to use
System.Diagnostics.Debugger.Break()
in source generator, so that the debugger could attach to the generated source and step into the project source files. Or, in the latest Rider, user can set breakpoints in *.g.cs
directly.Rider canāt map generated files to files embedded in pdb, it only uses the version from pdb for the debugger. Step from the navigation code to the target source could be time taken, but it is the only solution in current stage.
Existing feature request: RIDER-72249 Cannot set breakpoint in source generated code used for navigation
- Code changes in source generator are not taking effect
- RIDER-94207 Source generators don't update when being changed
- RIDER-101933 Code changes in source generator could not be reflected in Rider
- Execute
dotnet build-server shutdown
in solution root folder. - Execute
dotnet clean
to delete all build results. - Build the solution in Rider so that code changes will be reflected as expected.
- Add the steps inĀ Before launchĀ configuration
- TheĀ External Tools/Build Server ShutdownĀ config looks something like this.
When developing a solution including source generator projects, it requires building the entire solution to make sure the source generator taking effects. Sometimes changes in source generators will not take effect and lead to the wrong build resultāalthough code could be generated normally.
Itās a known issue described in the two tickets:
The solution is refreshing the build server with the below steps:
To simplify the operations, the following
Run/Debug Configurations
could be added for the project.- Missing Roslyn Syntax Visualizer in Rider
The integrated Roslyn Syntax Visualizer in Visual Studio is very handy when developing Roslyn analyzer and source generator. There are some third party implementations of the tool in Rider, but most of them are outdated and do not support the latest C # version.
Existing feature request: RIDER-29517 Syntax visualizer should be available in Rider
Some problems related to Roslyn Analyzer
Ā
Ā