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:
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:
<?xml version="1.0" encoding="utf-8"?7gt; <RuleSet Name="Name" Description="Description" ToolsVersion="15.0"> <Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers"> <Rule Id="SA0000" Action="Error" /> </Rules> </RuleSet>
Taking
StyleCop.Analyzers
as an example again, we can set the action for rule SA1200 – Using directive must appear within a namespace declaration – to 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:
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:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> <CodeAnalysisRuleSet>Custom.ruleset</CodeAnalysisRuleSet> </PropertyGroup> <ItemGroup> <PackageReference Include="StyleCop.Analyzers" Version="1.0.2" /> <AdditionalFiles Include="stylecop.json" /> </ItemGroup> </Project>
Download Rider 2018.2 now! We’d love to hear your feedback!
The Morning Brew - Chris Alcock » The Morning Brew #2671 says:
September 25, 2018[…] Roslyn Analyzer Rulesets and stylecop.json support in Rider 2018.2 – Matthias Koch […]
Dew Drop - September 25, 2018 (#2810) - Morning Dew says:
September 25, 2018[…] Roslyn Analyzer Rulesets and stylecop.json support in Rider 2018.2 (Matthias Koch) […]
10 Essential .Net Developers’ Tools You Must Know - Finoit says:
February 11, 2019[…] StyleCop.Analyzers […]
10 Essential .Net Developers’ Tools You Must Know By Jagdish Bhatt | Technopreneurph says:
February 18, 2019[…] StyleCop.Analyzers […]
10 Essential .Net Developers’ Tools You Must Know - TRENDING STORY says:
February 22, 2019[…] StyleCop.Analyzers […]
Jeff says:
June 25, 2019This 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.
Romain AVONDE says:
August 13, 2019Hello,
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,