SonarLint & StyleCop in same ruleset file (VisualStudio)

  • Operating system: Windows 10
  • SonarLint plugin version: SonarLint for VisualStudio 2022 (6.10.0.57359)
  • Programming language you’re coding in: C#
  • Is connected mode used:
    • Connected to SonarCloud or SonarQube (and which version): Developer Edition, Version 9.2.3 (build 50713)

In our projects we use SonarLint and StyleCop.
In the same rules file, we have the list of rules for both tools, example

<?xml version="1.0" encoding="utf-8"?>
<RuleSet xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="SonarQube - MyProjectName MyQualityProfileName" Description="This rule set was automatically generated from SonarQube https://XXXXX/profiles/show?key=XXXXX" ToolsVersion="14.0">
   <Rules AnalyzerId="SonarAnalyzer.CSharp" RuleNamespace="SonarAnalyzer.CSharp">
     <Rule ... />
     <Rule ... />
   </Rules>
   <Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
     <Rule ... />
     <Rule ... />

   </Rules>
</RuleSet>

When I make changes in the SonarQube project configuration (activate/deactivate rules), I have to synchronize the SonarQube project in my VisualStudio project and the rules file is always regenerated, losing the list of existing StyleCop rules.

How can I make sure that this list of StyleCop rules is not lost and that SonarQube respects them when synchronizing?

Thanks

no suggestion?

Hi @JordiPM. Apologies for the delay in replying.

All of the files under the .sonarlint folder are generated. It is not expected that they would be manually edited. However, combining the generated ruleset with a manually-edit ruleset is straightforward.

  1. Create your new manual ruleset, and configure the StyleCop rules.
  2. Also in the new ruleset, <Include> the generated ruleset. The MS docs have an example of using Include.
  3. Change the <CodeAnalysisRuleset> property in your project files to point to your new ruleset.

You should now be able to manually configure the StyleCop rules without losing the configuration when you synchronize the Quality Profile.