Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage Microsoft and StyleCop code analysis rules at solution level

I'm trying to bring code analysis to our projects, so I started with a solution with both .NET Framework and .NET Standard projects.

I followed this tutorial but to sum-up, here is what I did:

  • Added a SolutionName.ruleset file at the solution level
  • Added a stylecop.json file at solution level and referenced this file as link in each project
  • Installed StyleCop.Analyzers 1.1.0-beta009 Nuget package on every project
  • Installed Microsoft.NetCore.Analyzers on .NET Standard projects and Microsoft.NetFramework.Analyzers for .NET Framework projects

First of all, Visual Studio looks so buggy but it's maybe due to some black magic I don't understand.

If I click on the ruleset file in the solution explorer, I play a bit with ticking and de-ticking the several checkboxes of each rules group and re-build to see the changes in the Error List window but after a short time it doesn't work anymore. I'm even trying to deselect everything, build again and all my warnings are still here. The only way to make them disappear is to close Visual Studio.

As shown on the screenshot, I expended the .NET Standard library project called ConfigLibrary to see that it has several associated Analyzers.

enter image description here

The problem is that all these rules seem to come from the Nuget package's DLL C:\Users\user\.nuget\packages\stylecop.analyzers\1.1.0-beta009\analyzers\dotnet\cs\StyleCop.Analyzers.dll.

If I deselect the StyleCop.Analyzers rules in my ruleset file, it does nothing, and if I search for these rules codes using the search textbox, it shows they are not selected. But at the end these rules are still applied...

When I try to uninstall the StyleCop.Analyzers Nuget package for all projects, then I don't get at all these warnings even when the rules are selected in my ruleset file.

Also my ruleset file is often automatically re-written by I don't know what process. And the rules don't work anymore even after I restart Visual Studio

enter image description here

So what is the proper way to handle some code analysis rule at the solution level for both .NET Framework and .NET Core projects?

How to avoid the analyzers' DLLs to override my rules defined in my ruleset file? Is there a workaround to make Visual Studio properly refresh the applied rules? Build, Rebuild and Run code analysis on solution are all buggy, only restarting Visual Studio works but it's kind of a heavy workaround especially with all these rules...

Is it me not understanding a basis concept or am I facing some weird bugs with .NET Core? Thank you

like image 589
Jérôme MEVEL Avatar asked Oct 18 '18 06:10

Jérôme MEVEL


People also ask

How do you change rules on StyleCop?

In your StyleCop install, there's a Settings. StyleCop file. You can edit this to turn off rules globally. Drag that file onto the Settings Editor executable in that file to edit it.

How do I enable StyleCop in VS 2022?

The stylecop feature doesn't work with Visual Studio 2022 . The Compatible versions are Visual Studio 2012, 2013, 2015, 2017, 2019 . Please try to always add a source to statements like this (even if it is obviously correct in this case 😉).

What is StyleCop used for?

StyleCop is an open-source static code analysis tool from Microsoft that checks C# code for conformance to StyleCop's recommended coding styles and a subset of Microsoft's . NET Framework Design Guidelines.

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.


1 Answers

Look up a rule set file and copy it to your repository, renaming it to "My Rules.ruleset" or something.

For instance, take this one:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Team Tools\Static Analysis Tools\Rule Sets\MinimumRecommendedRules.ruleset

Install the package for the projects you want to be verified according to your rules.

For all of these projects:

  • Go to properties, CodeAnalysis page
  • Open the "Run this rule set:" drop-down box and choose "".
  • Select your rule set file.
  • Add the ruleset file to your solution and open it.
  • Tweak your rules.
  • Build!
like image 157
Martin Maat Avatar answered Nov 15 '22 05:11

Martin Maat