SonarQube会覆盖规则集和分析器

时间:2016-06-01 01:13:30

标签: sonarqube sonar-runner

我有一个使用StyleCop.Analyzers(基于Roslyn)的C#项目。当我们使用msbuild 14编译这个项目时,我们看到StyleCop.Analyzers引发了以下警告:

warning SA1005: Single line comment must begin with a space.

到目前为止,这么好。这就是我们的期望。

然后,我们创建了一个简单的脚本,在SonarQube msbuild启动/停止中执行此编译:

cd MyProjectRoot
"D:\MSBuild.SonarQube.Runner-2.0\MSBuild.SonarQube.Runner.exe" begin /k:test /n:test /v:1.0.0 /d:sonar.host.url=https://mysonar.com/ /d:sonar.login=abc
"C:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe" /p:Configuration=Debug /t:Rebuild
"D:\MSBuild.SonarQube.Runner-2.0\MSBuild.SonarQube.Runner.exe" end

当我们运行时,我们有一些奇怪的事情:我们没有看到先前的警告SA1005。

通过查看msbuild输出,我们看到当在SonarQube启动/停止中调用msbuild 而不是时,我们有以下内容:

C:\Program Files (x86)\MSBuild\14.0\bin\csc.exe ... /analyzer:..\packages\StyleCop.Analyzers.1.0.0\analyzers\dotnet\cs\Newtonsoft.Json.dll /analyzer:..\packages\StyleCop.Analyzers.1.0.0\analyzers\dotnet\cs\StyleCop.Analyzers.CodeFixes.dll /analyzer:..\packages\StyleCop.Analyzers.1.0.0\analyzers\dotnet\cs\StyleCop.Analyzers.dll

但是当使用StyleCop SonarQube启动/停止时,我们有:

C:\Program Files (x86)\MSBuild\14.0\bin\csc.exe ... /analyzer:C:\Users\myuser\AppData\Local\Temp\1\.sonarqube\.static\csharp_1.11.0\SonarAnalyzer.zip\SonarAnalyzer.CSharp.dll /analyzer:C:\Users\myuser\AppData\Local\Temp\1\.sonarqube\.static\csharp_1.11.0\SonarAnalyzer.zip\SonarAnalyzer.dll

您可以看到,我们没有使用相同的分析仪!事实上,我根本没有使用我配置的分析仪。此外,我们没有使用相同的规则集。我没有在msbuild输出中显示它,但你可以相信我;)

所以有两个问题:

  • 如何在启用SonarQube的情况下进行编译时保留配置的代码分析器?
  • 如何在启用SonarQube的情况下进行编译时保留配置的RuleSet?

请注意我正在使用:

  • SonarQube msbuild runner 2.0
  • SonarQube服务器5.3
  • SonarQube c#plugin 5.2

1 个答案:

答案 0 :(得分:1)

SonarQube会覆盖分析期间使用的规则集。如果您希望规则集相同,则需要在SonarQube中设置质量配置文件以匹配您当前的规则集。有一个VS插件可以将您的规则集从SonarQube服务器同步到本地项目,如果您想使用它。

对于StyleCop分析仪,您需要使用以下SDK打包分析器:https://github.com/SonarSource-VisualStudio/sonarqube-roslyn-sdk/blob/master/README.md

一旦这样做,规则应作为质量门规则在声纳中使用,并在分析过程中自动运行。