Code coverage exclusions don't work for 6.7.x+ with C# projects

Hello. We are migrating to newest version of sonarqube 6.7.4 from 5.5 (5.5 -> 5.6 -> 6.7).
We had code coverage as 85% for sonarqube 5.5 - 5.6 versions (with proper versions of sonar C# plugin - 5.2-5.5 and scanners for msbuild 2.0-3.0)

But for 6.7.4 and 7.1 we have 19% coverage (sonarC# 6.7 - 7.3 and scammer for msbuild 4.0 - 4.3)

Coverage Tool: opencover 4.5.619
Test Tool: Nunit

Also, Report Generator shows proper results for each scan for nothing matter of sonarqube versions. It proves that opencover filters and exclusions by attribute work as expected.

In addition, I tried to use dotcover and ReportGenerator shows its results properly, too.

I found that a lot of modules are included to 6.7+ sonarqube, which were missed due to opencover filters and its exclusions in sonarqube 5.5-5.6

Finally, I tried to use sonar.coverage.exclusions for global/project/scanner level, but seems they don’t work at all. I still see all modules in coverage treemap.

Here is an issue in github with details https://github.com/SonarSource/sonar-scanner-msbuild/issues/539

Hi Aleksandr,

I suggest double-checking this blog post which introduces some important changes/improvements in coverage handling in 6.x.

You likely need to pursue that effort. Understand which filepaths are handled during analysis time, and make sure that the exclusions you set are aligned with these filepaths.

Hi, Nico. Thank you for useful link. By the way, I found that sonar.coverage.exclusions works correctly. My fault that I set wrong path, which include Module (.net project) level, because my solution (.sln) includes multiple projects (.csproj). As example I have modules with name “App.UI” and “App.Core” for my solution App.sln, and I set as sonar.coverage.exclusions=**/App.UI/**. As a result, this pattern was not suitable for me. Therefore, I received wrong results for code coverage from both modules (.net projects).

In that case, I must to exclude only code coverage for specific modules (.net projects). But, code analysis results must be calculated all the same. I’d like to know if someone can suggest any approaches?

Seems, I found one of the possible solutions: set sonar.coverage.exclusions for module level in UI (Code - - Administration - General Settings - Analysis Scope). In that case, we can exclude all files (whole module or .net project) with **\*.cs. But still trying to find more automative way…

Aleksandr is hitting on another issue; excluding entire .Net projects from coverage. I too have found, so far, that the only way to do this is from within the UI at the module level.

Is there no value that can be used in /d:sonar.coverage.exclusions to exclude an entire project?

Is there no way to provide module-level config, allowing one to do what is accomplished in the UI (i.e. exclude **/*.cs, covering the entire contents of the module/project)?

I too have found, so far, that the only way to do this is from within the UI at the module level.

It would be better to add info about possibility to set parameters for module level to https://docs.sonarqube.org/display/SONAR/Narrowing+the+Focus. because now it looks like:

You can make these changes globally or at a project level. At both levels, the navigation path is the same: Administration > General Settings > Analysis Scope.


Is there no way to provide module-level config, allowing one to do what is accomplished in the UI (i.e. exclude **/*.cs, covering the entire contents of the module/project)?

I think, it must be:
sonar.modules=<guid prj1>,<guid prj1> ...

and then per modules:
<guid prj1>.sonar.coverage.exclusions=**/*.cs

Don’t test it yet. Also, with .net guids it looks not so pretty, may be somehow it can be set for .csproj files? Found that this structure is used, but it doesn’t work for me

...
<Project ...>
...
<Target Name="BeforeBuild">
<ItemGroup>
<SonarQubeSetting Include="sonar.coverage.exclusions">
<Value>**/*.cs</Value>
</SonarQubeSetting>
</ItemGroup>
</Target>
...
</Project>