Roslyn Analyzer Rulesets and stylecop.json support in Rider 2018.2

Code analysis is an important technique to keep our code clean, readable and free of defects. Besides its own code analysis features, Rider 2018.1 started to add support for Roslyn Analyzers, such as xunit.analyzers, which seamlessly blend into the familiar way of highlighting code inspections, configuring their severities or applying related quick-fixes.

In Rider 2018.2, issues with .NET Core toolsets have meanwhile been fixed. But more importantly, Rider 2018.2 further improves the integration with Roslyn Analyzers by adding support for ruleset files and StyleCop.json configuration files used with StyleCop.Analyzers.

Getting Started

Before we get started, we need to make sure to enable the option Read settings from editorconfig, project settings and rule sets, which is located in Preferences | Editor | Inspections Settings:

Enable support for rule set files

Once enabled, we can start working with rule set files.

Working with rule set files

Rule set files are referenced from project files (csproj) via the CodeAnalysisRuleSet property and are usually shared between all projects in a solution. A typical rule set file is organized by analyzerId and defines an action (error, warning, info, hidden, none, inherit) for a certain ruleId:

Taking StyleCop.Analyzers as an example again, we can set the action for rule SA1200Using directive must appear within a namespace declaration – to Error:

Configuring a rule to be shown as error

Note that the error is visible not only in the editor, but also in the solution explorer and solution-wide analysis indicator.

Working with stylecop.json

Of course, not everyone likes their using directives inside the namespace declarations. This is why StyleCop.Analyzers allows us to change such behavior with a stylecop.json configuration file. Adding a setting for usingDirectivesPlacement with a value of outsideNamespace for instance, will make the old error disappear, but we will instead see errors when putting using directives inside namespace:

Configuring StyleCop analyzers via stylecop.json

Pro tip: having the schema file referenced will give us code completion.

Note that the stylecop.json configuration file must be included into a special item-group with the name AdditionalFiles. For completeness, here is the full content of the project file from the previous examples:

Download Rider 2018.2 now! We’d love to hear your feedback!

About Matthias Koch

Matthias is a passionate C# developer and likes to talk about clean code, testing and tooling in general. Much of his spare time in the last years was devoted to his very own open source projects, including NUKE. He is working at JetBrains as developer advocate for the .NET department. Follow him on Twitter.
This entry was posted in How-To's and tagged , , , , , . Bookmark the permalink.

7 Responses to Roslyn Analyzer Rulesets and stylecop.json support in Rider 2018.2

  1. Pingback: The Morning Brew - Chris Alcock » The Morning Brew #2671

  2. Pingback: Dew Drop - September 25, 2018 (#2810) - Morning Dew

  3. Pingback: 10 Essential .Net Developers’ Tools You Must Know - Finoit

  4. Pingback: 10 Essential .Net Developers’ Tools You Must Know By Jagdish Bhatt | Technopreneurph

  5. Pingback: 10 Essential .Net Developers’ Tools You Must Know - TRENDING STORY

  6. Jeff says:

    This doesn’t seem to work. Rider is ignoring my ruleset file (I have it set to look at rulesets in the preferences). I know for a fact that my Directory.Build.props is specifying the ruleset correctly because my build does fail if it generates rule warnings (I have treat warnings as errors set to true). However when doing Inspect Code from Rider, it finds tons and tons of warnings that are configured as Hints in my ruleset.

  7. Romain AVONDE says:

    Hello,

    I have a project with “stylecop.json”, in which I specify the language of the documentation to French.
    But when I generate the documentation of a constructor for example, it is generated in English.
    In addition warnings appear on the properties when I write the documentation in French.

    Regards,

Leave a Reply

Your email address will not be published. Required fields are marked *