dotnet-format in JetBrains Rider
In this post I describe how to setup dotnet format
in JetBrains Rider.
dotnet format
can be a great tool to have reliable code formatting across teams. It is a code formatting linter, similar to prettier
and eslint
from the TypeScript world. See my previous post for an overview on dotnet format.
These tools can be added to “any” git based flow, and can expect identical formatting across different code editors!
No more “Rider/ReSharper” vs “Visual Studio” vs “VS Code”.
But as this is still early in the dotnet world, we need to force our editors to use it. This post describes the flow for JetBrains Rider.
Prerequisites
Install dotnet format
dotnet tool install -g dotnet-format
Using dotnet format
on Save in Rider
dotnet format
is easy to apply as a command, but if you have to invoke the command manually before each commit it gets tedious, and error prone.
Rider has a built in file-watcher, and it can be used for invoking dotnet format on a specific file. This is both faster, and simpler!
Following tips from a github issue dotnet/format#230, with some adjustments I managed a nice setup.
Steps:
- Open Settings ->
Tools
->File Watchers
- Press The “Plus Sign” to
Add
aCustom
watcher- Set the name to “dotnet format on save”
- FileType: C#
- Scope:
Open Files
- Program: Write
dotnet
- Arguments:
format $SolutionPath$ --verbosity diagnostic --include $FileRelativePath$
- I disabled all advanced option checkboxes.
- All other values were left default
- Press
Ok
, andSave
Settings
All files you change and save in Rider in this project will now be formatted by dotnet format! Cool!
Thats all for this time! If you want to learn more about dotnet format rules I recommend to A: look at the .editorconfig
file (link) of the dotnet-format repository, and B: Check the Supported editorconfig options
In late 2021 i switched to CSharpier, a great alternative to dotnet format
for whitespace formatting. I highly recommend trying it out. Check my blog on that here: csharpier intro
Code Snippets are licensed under MIT No Attribution
Edit in January 2024: I updated the command due to changes in dotnet format.