roslynator icon indicating copy to clipboard operation
roslynator copied to clipboard

Enable all formatting rules

Open marcospgp opened this issue 4 years ago • 22 comments

How can I enable all formatting rules with error severity using an .editorconfig file?

Edit: Managed to do it like this for now:

dotnet_diagnostic.RCS0001.severity = error
dotnet_diagnostic.RCS0002.severity = error
dotnet_diagnostic.RCS0003.severity = error
dotnet_diagnostic.RCS0004.severity = error
dotnet_diagnostic.RCS0005.severity = error
dotnet_diagnostic.RCS0006.severity = error
dotnet_diagnostic.RCS0007.severity = error
dotnet_diagnostic.RCS0008.severity = error
dotnet_diagnostic.RCS0009.severity = error
dotnet_diagnostic.RCS0010.severity = error
dotnet_diagnostic.RCS0011.severity = error
dotnet_diagnostic.RCS0012.severity = error
dotnet_diagnostic.RCS0013.severity = error
dotnet_diagnostic.RCS0014.severity = error
dotnet_diagnostic.RCS0015.severity = error
dotnet_diagnostic.RCS0016.severity = error
dotnet_diagnostic.RCS0020.severity = error
dotnet_diagnostic.RCS0021.severity = error
dotnet_diagnostic.RCS0022.severity = error
dotnet_diagnostic.RCS0023.severity = error
dotnet_diagnostic.RCS0024.severity = error
dotnet_diagnostic.RCS0025.severity = error
dotnet_diagnostic.RCS0027.severity = error
dotnet_diagnostic.RCS0028.severity = error
dotnet_diagnostic.RCS0029.severity = error
dotnet_diagnostic.RCS0030.severity = error
dotnet_diagnostic.RCS0031.severity = error
dotnet_diagnostic.RCS0032.severity = error
dotnet_diagnostic.RCS0033.severity = error
dotnet_diagnostic.RCS0034.severity = error
dotnet_diagnostic.RCS0036.severity = error
dotnet_diagnostic.RCS0038.severity = error
dotnet_diagnostic.RCS0039.severity = error
dotnet_diagnostic.RCS0041.severity = error
dotnet_diagnostic.RCS0042.severity = error
dotnet_diagnostic.RCS0043.severity = error
dotnet_diagnostic.RCS0044.severity = error
dotnet_diagnostic.RCS0045.severity = error
dotnet_diagnostic.RCS0046.severity = error
dotnet_diagnostic.RCS0047.severity = error
dotnet_diagnostic.RCS0048.severity = error
dotnet_diagnostic.RCS0049.severity = error
dotnet_diagnostic.RCS0050.severity = error
dotnet_diagnostic.RCS0051.severity = error
dotnet_diagnostic.RCS0052.severity = error
dotnet_diagnostic.RCS0053.severity = error
dotnet_diagnostic.RCS0054.severity = error
dotnet_diagnostic.RCS0055.severity = error
dotnet_diagnostic.RCS0056.severity = error

marcospgp avatar Jul 29 '21 17:07 marcospgp

You can also run this script to generate editorconfig with default values: https://github.com/JosefPihrt/Roslynator/blob/master/tools/generate_default_editorconfig.cmd

josefpihrt avatar Jul 29 '21 17:07 josefpihrt

@JosefPihrt isn't there a way to add something like dotnet_diagnostic.roslynator_formatters.severity?

marcospgp avatar Jul 30 '21 08:07 marcospgp

According to documentation, chapter "Set rule severity of multiple analyzer rules at once in an EditorConfig file" the best solution is

dotnet_analyzer_diagnostic.category-formatting.severity = error

which is not exactly what you want because it will set all analyzers that have category "formatting" not just roslynator formatting analyzers.

josefpihrt avatar Jul 30 '21 10:07 josefpihrt

@JosefPihrt how is the category defined? couldn't a category of roslynator-formatting be defined?

marcospgp avatar Jul 30 '21 10:07 marcospgp

I'm interested in this too, and the suggestion above sadly doesn't work for me.

fgimian avatar Aug 05 '21 09:08 fgimian

@marcospgp That's interesting idea. The problem is that would be a breaking change because other may rely on the actual category name.

josefpihrt avatar Aug 05 '21 11:08 josefpihrt

@JosefPihrt Are you able to define your own category name for this? Perhaps another alternative could be a roslynator setting which enables a sane set of the formatting analyzers by default? 😄

@marcospgp Just an extra note. Be careful when enabling all these checks at the same time as some are mutually exclusive 😄 One of the primary examples is RCS0045 which enforces \n for line breaks and RCS0044 which enforces \r\n.

fgimian avatar Aug 05 '21 11:08 fgimian

@fgimian thanks for the advice, the conflict is fine as I am adjusting the rules as I run into those issues :) it's a nice way to choose the rules as I go.

marcospgp avatar Aug 07 '21 14:08 marcospgp

@marcospgp , @JosefPihrt : any progress or suggestions on this please? I want to enable all the recommended formatting rules by default as well. Configuring 1 by 1 is just very tedious and every time a new rule is added, we need to know to manually turn it on as well.

tuanle07 avatar Apr 28 '22 10:04 tuanle07

Version 4.0 introduces possibility to set severity for all Roslynator analyzers

dotnet_analyzer_diagnostic.category-roslynator.severity = error

This option works for analyzers that are enabled by default so it is necessary to enable analyzers that are disabled by default by adding option to .roslynatorconfig

roslynator_analyzers.enabled_by_default = true

For further info please see https://github.com/JosefPihrt/Roslynator/blob/master/docs/Configuration.md

josefpihrt avatar Apr 28 '22 20:04 josefpihrt

It would be great to avoid having an additional file for the enabled_by_default setting, but it does beat having to list each rule manually in .editorconfig :)

marcospgp avatar May 02 '22 14:05 marcospgp

It's not perfect with the enabled_by_default setting, I agree. But this behavior is defined by Microsoft, see https://docs.microsoft.com/en-us/visualstudio/code-quality/use-roslyn-analyzers?view=vs-2022#set-rule-severity-of-multiple-analyzer-rules-at-once-in-an-editorconfig-file

josefpihrt avatar May 05 '22 08:05 josefpihrt

Do think this issue can be closed now @marcospgp ?

josefpihrt avatar May 05 '22 08:05 josefpihrt

One question, according to that link the severity of all rules should be able to be set in .editorconfig as long as the rules are enabled by default (even if with a severity of "hidden", it seems).

Are the roslynator formatting rules enabled by default? If not, wouldn't enabling them with a severity of "hidden" achieve the same effect, while allowing the severity for all of them to be set at once in .editorconfig?

marcospgp avatar May 05 '22 11:05 marcospgp

@marcospgp : I agree your idea could be pretty neat I think.

tuanle07 avatar May 05 '22 13:05 tuanle07

I think that setting severity of all formatting analyzers to "hidden" is not feasible because all "hidden" analyzers would show up in the context menu (Ctrl+.)

josefpihrt avatar May 28 '22 12:05 josefpihrt

Hi,

Here my C:/Users/<USERNAME>/AppData/Local/JosefPihrt/Roslynator/.roslynatorconfig:

# Roslynator
# - https://github.com/JosefPihrt/Roslynator
# - https://github.com/JosefPihrt/Roslynator/blob/main/docs/Configuration.md

# Enable/disable all analyzers by default
# NOTE: This option can be used only in .roslynatorconfig file
roslynator_analyzers.enabled_by_default = true

How to know if it works? Under VS I don't see any change.

Thanks.

dhabierre avatar Jun 22 '23 08:06 dhabierre

@dhabierre Please check format of your roslynatorconfig file: https://github.com/JosefPihrt/Roslynator/blob/ab443617938c40b1ac87a612da054cd7837a82c9/docs/Configuration.md?plain=1#L41-L42

Following format is correct:

is_global = true

roslynator_analyzers.enabled_by_default = true

josefpihrt avatar Jun 22 '23 09:06 josefpihrt

I personally have still had no luck getting this to work.

An individual rule in an editorconfig works:

~\source\abc via .NET v7.0.304 🎯 net7.0
🕙 [ 10:48:05 AM ] ❯ type .\Program.cs
using System;
using System.Buffers;
Console.WriteLine("hi");

~\source\abc via .NET v7.0.304 🎯 net7.0
🕙 [ 10:48:07 AM ] ❯ type .\.editorconfig
[*.cs]
dotnet_diagnostic.RCS0003.severity = error

~\source\abc via .NET v7.0.304 🎯 net7.0
🕙 [ 10:48:13 AM ] ❯ dotnet build --no-incremental
MSBuild version 17.6.3+07e294721 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
C:\Users\Fots\source\abc\Program.cs(2,22): error RCS0003: Add blank line after using directive list. [C:\Users\Fots\source\abc\abc.csp
roj]

Build FAILED.

C:\Users\Fots\source\abc\Program.cs(2,22): error RCS0003: Add blank line after using directive list. [C:\Users\Fots\source\abc\abc.csp
roj]
    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.79

But enabling them globally doesn't:

~\source\abc via .NET v7.0.304 🎯 net7.0
🕙 [ 10:48:53 AM ] ❯ type C:\Users\Fots\AppData\Local\JosefPihrt\Roslynator\.roslynatorconfig
is_global = true
roslynator_analyzers.enabled_by_default = true

~\source\abc via .NET v7.0.304 🎯 net7.0
🕙 [ 10:48:59 AM ] ❯ dir .editorconfig
Get-ChildItem: Cannot find path 'C:\Users\Fots\source\abc\.editorconfig' because it does not exist.

~\source\abc via .NET v7.0.304 🎯 net7.0
🕙 [ 10:49:04 AM ] ❯ dotnet build --no-incremental
MSBuild version 17.6.3+07e294721 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  abc -> C:\Users\Fots\source\abc\bin\Debug\net7.0\abc.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:01.00

I also see no warnings or messages on Visual Studio.

Here's another example where I made sure to set the default Roslynator severity and used a local .roslynatorconfig and another diagnostic:

~\source\abc via .NET v7.0.304 🎯 net7.0
🕙 [ 10:58:08 AM ] ❯ type .\Program.cs
using System;
using System.Buffers;
Console.WriteLine("hi");
var x = 1;
if (x > 1)
{
}
else
if (x > 0)
{
}

~\source\abc via .NET v7.0.304 🎯 net7.0
🕙 [ 10:58:10 AM ] ❯ type .\.editorconfig
[*.cs]
dotnet_analyzer_diagnostic.category-roslynator.severity = error
dotnet_diagnostic.RCS0041.severity = error

~\source\abc via .NET v7.0.304 🎯 net7.0
🕙 [ 10:58:12 AM ] ❯ type .\.roslynatorconfig
roslynator_analyzers.enabled_by_default = true
roslynator_refactorings.enabled = true
roslynator_compiler_diagnostic_fixes.enabled = true

~\source\abc via .NET v7.0.304 🎯 net7.0
🕙 [ 10:58:15 AM ] ❯ dotnet build --no-incremental
MSBuild version 17.6.3+07e294721 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
C:\Users\Fots\source\abc\Program.cs(8,5): error RCS0041: Remove new line between 'if' keyword and 'else' keyword. [C:\Users\Fots\sourc
e\abc\abc.csproj]

Build FAILED.

C:\Users\Fots\source\abc\Program.cs(8,5): error RCS0041: Remove new line between 'if' keyword and 'else' keyword. [C:\Users\Fots\sourc
e\abc\abc.csproj]
    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.81

Great so now let's remove the specific rule for RCS0041 and see if it is caught with roslynator_analyzers.enabled_by_default = true:

~\source\abc via .NET v7.0.304 🎯 net7.0
🕙 [ 10:58:22 AM ] ❯ type .\.editorconfig
[*.cs]
dotnet_analyzer_diagnostic.category-roslynator.severity = error

~\source\abc via .NET v7.0.304 🎯 net7.0
🕙 [ 10:58:35 AM ] ❯ dotnet build --no-incremental
MSBuild version 17.6.3+07e294721 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  abc -> C:\Users\Fots\source\abc\bin\Debug\net7.0\abc.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.97

Cheers Fotis

fgimian avatar Jun 24 '23 00:06 fgimian

@fgimian Default configuration file (.roslynatorconfig) works only with VS extension (or VS Code extension):

https://github.com/JosefPihrt/Roslynator/blob/ab443617938c40b1ac87a612da054cd7837a82c9/docs/Configuration.md?plain=1#L37

Do you use VS extension? If you use nuget packages it won't work.

Regarding the second example: Can you explain what is meant by "local .roslynatorconfig". Only .roslynatorconfig file is the file in AppData/....

josefpihrt avatar Jun 24 '23 10:06 josefpihrt

@fgimian Default configuration file (.roslynatorconfig) works only with VS extension (or VS Code extension):

https://github.com/JosefPihrt/Roslynator/blob/ab443617938c40b1ac87a612da054cd7837a82c9/docs/Configuration.md?plain=1#L37

Do you use VS extension? If you use nuget packages it won't work.

Regarding the second example: Can you explain what is meant by "local .roslynatorconfig". Only .roslynatorconfig file is the file in AppData/....

Aaah, thanks for the reply. I always avoid editor extensions as they are incompatible with CI. So I think I'll just continue to enable each of the analysers as I'm doing today. 😄

Thanks heaps for your help! Fotis

fgimian avatar Jun 24 '23 10:06 fgimian

For others who come across this issue, the following page seems to cover much of the needs discussed:

https://josefpihrt.github.io/docs/roslynator/configuration/

tlunsfordCXP avatar Nov 28 '23 14:11 tlunsfordCXP