简体   繁体   English

合并 SonarAnalyzer.CSharp 和 Microsoft.CodeAnalysis 规则集

[英]Merge SonarAnalyzer.CSharp and the Microsoft.CodeAnalysis rulesets

We run a SonarQube in our company.我们公司运行一个SonarQube。 We also installed the SonarLint for Visual Studio 2019 extension.我们还安装了 SonarLint for Visual Studio 2019 扩展。

If I now connect a solution to the SonarQube, a file named "{MySolution}csharp.ruleset" is created.如果我现在将解决方案连接到 SonarQube,则会创建一个名为“{MySolution}csharp.ruleset”的文件。 Inside are 5 nodes: "Managed Binary Analysis", "Microsoft.CodeAnalysis.CSharp", "Microsoft.CodeAnalysis.CSharp.Features", "Microsoft.CodeAnalysis.Features" and finally "SonarAnalyser.CSharp".里面有 5 个节点:“Managed Binary Analysis”、“Microsoft.CodeAnalysis.CSharp”、“Microsoft.CodeAnalysis.CSharp.Features”、“Microsoft.CodeAnalysis.Features”和最后的“SonarAnalyser.CSharp”。

The project now is analysed using all of these rulesets.现在使用所有这些规则集对项目进行分析。 When I change one of these rules it is changed in my file, but not on the SonarQube server.当我更改这些规则之一时,它会在我的文件中更改,但不会在 SonarQube 服务器上更改。 When I change something on the SonarQube and "reload" the rules from there, the standard settings for the first 4 nodes are resetted and the now set rules inside SonarQube are inside the SonarAnalyzer.CSharp.当我更改 SonarQube 上的某些内容并从那里“重新加载”规则时,前 4 个节点的标准设置将被重置,现在 SonarQube 内设置的规则位于 SonarAnalyzer.CSharp 内。

My question is: "How can I change some of these Microsoft rules and tell SonarQube to use these rules company wide and not only until I reload the rules from SonarQube? Is it maybe possible to set these rules as SonarAnalyzer rules (merge them) or can we set these rules parted from the normal analyzing? Or, what would make the most sense, can we say SonarQube to just actualize the SonarAnalyzer.CSharp ruleset and don't touch the other ones?"我的问题是:“我如何更改其中一些 Microsoft 规则并告诉 SonarQube 在公司范围内使用这些规则,而不仅仅是在我从 SonarQube 重新加载规则之前?是否可以将这些规则设置为 SonarAnalyzer 规则(合并它们)或我们可以将这些规则设置为与正常分析分开吗?或者,最有意义的是,我们可以说 SonarQube 来实现 SonarAnalyzer.CSharp 规则集而不触及其他规则集吗?

If I now connect a solution to the SonarQube, a file named "{MySolution}csharp.ruleset" is created.如果我现在将解决方案连接到 SonarQube,则会创建一个名为“{MySolution}csharp.ruleset”的文件。 Inside are 5 nodes: "Managed Binary Analysis",...里面有 5 个节点:“托管二进制分析”,...

I'm guessing you're opening the ruleset file in VS and using the VS editor.我猜你是在 VS 中打开规则集文件并使用 VS 编辑器。 If you open the generated ruleset file with a text editor you'll see it only contains one "node" for the SonarAnalyzer rules.如果您使用文本编辑器打开生成的规则集文件,您将看到它只包含一个 SonarAnalyzer 规则的“节点”。

The VS editor is showing the other four "nodes" for built-in rules that are enabled by default. VS 编辑器显示默认启用的内置规则的其他四个“节点”。 When you change any of the default settings, VS is saving those changes to ruleset file generated by SonarLint.当您更改任何默认设置时,VS 会将这些更改保存到 SonarLint 生成的规则集文件中。

You can achieve what you want as follows:您可以按如下方式实现您想要的:

  1. add a new ruleset file imports the SonarLint-generated file eg添加一个新的规则集文件导入 SonarLint 生成的文件,例如
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Corporate ruleset" ToolsVersion="17.0">
  <!-- Assumes this file is in the same folder as the solution file -->
  <Include Path=".sonarlint\sonarlint-visualstudiocsharp.ruleset" Action="Default" />
</RuleSet>
  1. change your MSBuild projects so that the CodeAnalysisRuleSet property points to this new ruleset file rather than the SonarLint-generated ruleset.更改您的 MSBuild 项目,以便CodeAnalysisRuleSet属性指向这个新的规则集文件而不是 SonarLint 生成的规则集。

Now, when you edit the settings for the non-Sonar rules in the VS editor, the changes will be saved to your new ruleset file.现在,当您在 VS 编辑器中编辑非 Sonar 规则的设置时,更改将保存到您的新规则集文件中。

To change the Sonar rules settings, you would modify your Quality Profile in SonarQube, and tell SonarLint to regenerate it's ruleset file.要更改 Sonar 规则设置,您可以在 SonarQube 中修改您的质量配置文件,并告诉 SonarLint 重新生成它的规则集文件。

FYI this wiki page describes what changes SonarLint for Visual Studio is making to your projects, and how you can customise them.仅供参考,此 wiki 页面描述了 SonarLint for Visual Studio 对您的项目所做的更改,以及您如何自定义它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM