Confused over ruleset files vs. EditorConfig files
Asked Answered
H

3

24

Our organisation uses a ruleset file containing our organisation's StyleCop.Analyzers settings. This lives in source control, and all projects in all solutions in our organisation reference this file. If I edit the ruleset (via project properties → Code Analysis tab), I see a message prompting me to migrate the ruleset to an EditorConfig file. Why would I want to do this, and what is the difference between the two?

I've also noticed in Visual Studio menu ToolsOptionsText EditorC#Code Style, there are a number of settings eerily similar to many of the StyleCop.Analyzers rules (e.g., several settings relating to the use of "this", vs. StyleCop SA1101 "prefix local calls with this").

There is also a button here to create an EditorConfig file from these settings, so how would these settings coexist with the StyleCop.Analyzer settings, and which would take precedence?

Also, our Azure DevOps automated builds report warnings where any code "violates" the StyleCop rules. Will the builds still do this if we replaced the ruleset with an EditorConfig file?

Lastly, there is a GUI for editing the StyleCop.Analyzer settings (project properties → Code Analysis tab → Configure button), whereas EditorConfig files seem to be edited using the Visual Studio text editor. Given that there are hundreds of rules to manage, where would you get a complete list of rules that needed to be typed in? And wouldn't this be very tedious?

Hypotrachelium answered 4/8, 2020 at 10:53 Comment(1)
if rulesets went away, the problems would be solved. editorconfig for both IDE checking and compile time checking is what we needTotemism
B
7

Quotes and pictures are taken from Übersicht:

Setting a rule's severity in an EditorConfig file takes precedence over any severity that's set in a rule set or in Solution Explorer.

The settings in Visual Studio (Visual Studio menu ToolsOptionsText EditorC#Code Style) are used when no .editorconfig file is given or a rule is not configured in it.

In my experience, it is best to choose only one way of configuring. Also I experienced that the following described configuration of the .editorconfig file did not work / or edited the .ruleset as long as any .ruleset file is available.

Configure the rule via the light bulb

Configure rule via light bulb

Configure the rule via the error window

Configure rule via error window

Configure the rule via Solution Explorer. This is also a place where you find a list of all available rules.

Configure a rule via Solution Explorer

Command line tools respect the .editorconfig file:

When you build your project at the command line, rule violations appear in the build output if the following conditions are met:

  • The analyzers are installed as a NuGet package and not as a VSIX extension.
  • One or more rules are violated in the project's code.
  • The severity of a violated rule is set to either warning, in which case violations don’t cause build to fail, or error, in which case violations cause build to fail.
Brusa answered 3/11, 2020 at 10:25 Comment(1)
The learn.microsoft.com URL is unlikely to be correct.Palpebrate
D
4

Ruleset is a thing from the past. Migrate or stick with EditorConfig files.

From Convert an existing Ruleset file to EditorConfig file:

Starting in Visual Studio 2019 version 16.5, ruleset files are deprecated in favor of the EditorConfig file ...

Dinerman answered 15/10, 2021 at 6:29 Comment(0)
C
1

More information about the difference between ruleset and editorconfig is on FAQ for code analysis in Visual Studio:

Rule sets and EditorConfig files can coexist and can both be used to configure analyzers. Both EditorConfig files and rule sets let you enable and disable rules and set their severity.

However, EditorConfig files offer additional ways to configure rules too:

For the .NET code-quality analyzers, EditorConfig files let you define which types of code to analyze. For the .NET code-style analyzers that are built into Visual Studio, EditorConfig files let you define the preferred code styles for a codebase. In addition to rule sets and EditorConfig files, some analyzers are configured through the use of text files marked as additional files for the C# and VB compilers.

Creep answered 28/9, 2021 at 8:47 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.