6
votes

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

1

1 Answers

1
votes

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!