Disable one rule in external Roslyn rules for C# project

I have a C# project. There is a single rule that I want to disable for Roslyn.

I tried the solution mentioned here and this is the direct link for the solution.

So I disabled the single rule in my Project file’s ruleset file. That worked. But then Sonarlint downloaded a new ruleset from the Sonarqube server where the rule is set on again.

image

How can I turn off this rule, so that I don’t get codesmells related to this rule?
The rule id is CS 1591.

Hello, thanks for asking.

I understand that you use connected mode with a SonarQube server. In this case, the recommended way to silence the rule would be to disable it in the quality profile associated with your project on the server.

Thank you for your answer.

The rule doesn’t show in the quality profile. This is because the rule is by an external analyzer, namely Roslyn. That’s why I can’t disable it.

How can I disable external rules?

Hello @elifkus, welcome to the community!

I see that SonarLint has created another ruleset file in your project. I suspect that this happened because the original ruleset was not set to be used as CodeAnalysisRuleSet in the csproj.

Try changing your csproj to reference NW.DataAccess.ruleset:

	<PropertyGroup>
		<CodeAnalysisRuleSet>NW.DataAccess.ruleset</CodeAnalysisRuleSet>
	</PropertyGroup>

And update your binding.

SonarLint should then modify NW.DataAccess.ruleset to reference SonarLint’s solution ruleset.
With this configuration your rulesets should work, and SonarLint should not create another ruleset file (and you could delete NW.DataAccess-1.ruleset).

2 Likes

Thank you for your answer.

I checked my proj file. The ruleset was already set. The only difference was that I had a condition in the PropertyGroup. I removed the property group. This time it seemed to work. But then I fixed the other projects and after that when I updated the rules, again a new ruleset was generated.

The update changed even the project file:

     <PropertyGroup>
          <CodeAnalysisRuleSet>NW.DataAccess-1.ruleset</CodeAnalysisRuleSet>
     </PropertyGroup>

Update: I tried it again. This time it worked.

Sometimes after I build the projects the content of CodeAnalysisRuleset tag gets erased.

Thanks for the update @elifkus.

SLVS will never un-set the CodeAnalysisRuleset property, so this behavior is due to VS. I’ve observed a similar bug.

Just to confirm: are your rulesets now configured properly?

Btw, in the past week we did some changes in the rulesets generation. The updated documentation describes what the new behavior should be. We expect to release this version sometime next week.

Yes I realized that the unset behaviour is due to VS. In some of the occasions I erased the non-referenced ruleset files that were downloaded with suffix -1. It might be exactly the same bug.

The ruleset are currently configured properly. But I am not sure it’s going to stay this way next time we open that project and do development. We’ll see.

Thank you for the documentation link. If I run into issues again, I’ll update Sonarlint and check the documentation thoroughly.

1 Like