This article explains how to use the dotnet format command with .editorconfig and its limitations and then shows you that StyleCop.Analyzers can overcome them. This article also mentions how to apply the StyleCop ruleset to all projects in a solution.
About dotnet format
The dotnet format can format all source codes at once in a project or solution according to rulesets. The .NET SDK version 6.0 or later includes the dotnet format by default. If you use .NET Core 3.1, you can install it as follows.
The dotnet format reads one or more rulesets from .editorconfig files in a target project or solution folder or its ancestor folders. When you format your source codes to follow the C# Coding Style, you can use this .editorconfig in the .NET Runtime repository.
When you use the dotnet format with the .editorconfig, you need to specify the info level severity. Otherwise, the dotnet format ignores most of the rules.
The above command formats the following code to the next one.
This result is not sufficient. the method name keeps camelCase, and the
if statement remains in a single line despite the coding style. There are unnecessary empty lines, too.
To get more disciplined results, we can use StyleCop.Analyzers. The StyleCop.Analyzers provides a way to apply the StyleCop ruleset traditionally provided by an extension named StyleCop for Visual Studio.
When you format source codes in a project with StyleCop.Analyzers, you need to add the StyleCop.Analyzers package to the project as follows. Then, StyleCop.Analyzers works with the default ruleset.
The default ruleset doesn't follow the coding style. Therefore, you need to download and adjust the default ruleset file from the StyleCop.Analyzers repository. You should add the following lines to the project file when you save the ruleset file as stylecop.ruleset.
Then, you can adjust the rules to follow the coding style in the stylecop.ruleset as follows. You can download the adjusted file in my repository.
The dotnet format can format source codes with StyleCop.Analyzers. If the version of the dotnet format is 6.0 or higher, there needs nothing special to use StypeCop.Analyzers. You can use the same command shown above.
If the version is 5.*, you should run the command with an additional option.
After running the above command, you can get the following result much better than the previous result.
When you need to format every project in a solution folder, you need to add the StyleCop.Analyzers package to each project. There is, however, a way to eliminate this effort, the Directory.Build.props file.
When you put Directory.Build.props in the solution folder, MSBuild imports this file to the top of each project file. So you can place the following Directory.Build.props file in the solution folder to make Stylecop.Analyzers available in every project.
I illustrated how to use the dotnet format command with StyleCop.Analyzers and the Directory.Build.props file. You can format source codes in every project in a solution folder to use them and get more disciplined results than only with the .editorconfig file.
One thought on “The dotnet format command and StyleCop.Analyzers”
This is going to be very useful for me, as I'm currently working on a coding standards initiative at work.
Thanks a lot!