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:
SolutionName.ruleset
file at the solution levelstylecop.json
file at solution level and referenced this file as link in each projectStyleCop.Analyzers 1.1.0-beta009
Nuget package on every projectMicrosoft.NetCore.Analyzers
on .NET Standard projects and Microsoft.NetFramework.Analyzers
for .NET Framework projectsFirst 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.
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
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
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.
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 😉).
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.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With