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
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 (
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>
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!
Subscribe to Blog updates
Thanks, we've got you!
Another Look into the Future with Rider’s Predictive Debugger
In the 2023.2 release cycle, we’ve introduced the Predictive Debugger in ReSharper, which gives you predictions about code paths and variables beyond the current execution pointer. We’ve written extensively about its advantages compared to alternative debugging strategies like thorough thinking, log…
Visualize Entity Framework Relationships and Additional Query Analysis in ReSharper 2023.3
A lot of teams are using Entity Framework or EF Core to work with their database. As an Object-Relational Mapper (ORM), it bridges objects in code to a relational database model, so that as a developer you don’t have to worry too much about the actual database. We all know: that’s not entirely tr…
Automatically Analyze ASP.NET Core Performance With Dynamic Program Analysis
Slow web pages may make your users or customers abandon your web application, even before they’ve had a proper look at it. You’ve likely also been frustrated working with a web application that is slow to load. The good news is that the latest versions of ReSharper and JetBrains Rider’s Dynamic P…
OSS Power-Ups: MassTransit – Webinar Recording
The recording of our webinar, OSS Power-Ups: MassTransit, with Chris Patterson, is available. This was the thirteenth episode of our OSS Power-Ups series, where we put a spotlight on open-source .NET projects. Subscribe to our community newsletter to receive notifications about future webinars.…