Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused over ruleset files vs editorconfig files

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. Can someone explain why I would want to do this, and what the difference is between the two?

I've also noticed in VS Tools->Options->Text Editor->C#->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 co-exist 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 VS 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?

like image 439
Andrew Stephens Avatar asked Aug 04 '20 10:08

Andrew Stephens


People also ask

Does rider support EditorConfig?

Use EditorConfig JetBrains Rider supports code formatting styles, code syntax styles, C# naming styles, and code inspection severity levels defined in the EditorConfig format.

Is StyleCop still used?

StyleCop used to be a Visual Studio plugin and a NuGet package. You can still use this in Visual Studio 2019, but the current recommended way to use StyleCop is to use the Roslyn-based analyzers.

What is the use of StyleCop?

StyleCop is a C# source code analyzer that allows you to enforce a set of style and consistency rules. You can adapt the rules that you don't want to check depending on your needs. This kind of tools helps you to have a code: Readable.

Where is .EditorConfig located?

editorconfig file at the root of your repo or in the directory that your project resides. Visual Studio looks for a file named . editorconfig in the directory of the opened file and in every parent directory.


3 Answers

Quotes and pictures are taken from https://docs.microsoft.com/de-de/visualstudio/code-quality/use-roslYn-analyzers?view=vs-2019#rule-severity

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 (VS Tools->Options->Text Editor->C#->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 describte configuration of the .editorconfig file did not work / or edited the .ruleset as long as any .ruleset file is available.

Configure rule via light bulb Configure rule via light bulb

Configure rule via error window Configure rule via error window

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

Commandline 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 on't cause build to fail, or error, in which case violations cause build to fail.
like image 190
sa.he Avatar answered Sep 29 '22 00:09

sa.he


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

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

https://docs.microsoft.com/en-us/visualstudio/code-quality/use-roslyn-analyzers?view=vs-2019#convert-an-existing-ruleset-file-to-editorconfig-file

like image 22
stefan.seeland Avatar answered Sep 30 '22 00:09

stefan.seeland


More info about difference between ruleset and editorconfig here : https://docs.microsoft.com/en-us/visualstudio/code-quality/analyzers-faq?view=vs-2019

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.

like image 32
Dude Pascalou Avatar answered Sep 28 '22 00:09

Dude Pascalou