Sonar.Coverage.Exclusions not excluding as expected based on file regex pattern

SonarQube version: 7.0.0.36138

Thank you for looking at my report.

Directory is not excluded from code coverage calculations based on the regex pattern applied to the property sonar.coverage.exclusions. The same regex pattern is set in the sonar.exclusions property, which successfully matches the directory and excludes it entirely from any calculation.

This is undesirable as I still want the tests to follow the same rules as the rest of the code, but not be included in the overall code coverage.

Steps to reproduce/error observed:

Scenario 1
Have a directory called test and set the regex pattern
Set the regex to in sonar.coverage.exclusions : file:**/test/**
Expected: to remove the directory from code coverage calculation
Actual: Directory not excluded from cove coverage

Scenario 2
Same regex pattern set in sonar.exclusions: file:**/test/**
Expected: To remove the directory from all code calculations
Actual: Is working as expected, the directory is removed from all calculations.

Let me know if I need to provide any other relevant logs or material in order to diagnose and solve the issue.

#bug:fault

Hi,

There should be a few lines in your analysis log about how your exclusion settings are handled. Can you share those?

Also, where are you putting these settings? On the analysis command line? In the UI? In a settings file somewhere?

Ann

Hello,

The settings are set in the UI. I don’t see anything in the logs about calculating exclusions, unless I’m looking at the wrong log file. I’ve set the logs to Debug and downloaded the Compute Engine log:

2018.08.22 12:27:41 INFO  ce[AWVhmszB7e7-9RhP0Omq][o.s.c.t.CeWorkerImpl] Execute task | project=ACH | type=REPORT | id=AWVhmszB7e7-9RhP0Omq | submitter=admin
2018.08.22 12:27:42 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Extract report | time=1162ms
2018.08.22 12:27:42 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Persist scanner context | time=4ms
2018.08.22 12:27:42 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Generate analysis UUID | time=0ms
2018.08.22 12:27:42 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Load analysis metadata | time=5ms
2018.08.22 12:27:42 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Initialize | time=0ms
2018.08.22 12:27:42 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Verify billing | time=0ms
2018.08.22 12:27:42 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Build tree of components | time=79ms
2018.08.22 12:27:42 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Validate project | time=7ms
2018.08.22 12:27:42 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Load quality profiles | time=53ms
2018.08.22 12:27:42 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Load Quality gate | time=3ms
2018.08.22 12:27:42 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.p.s.PeriodResolver] Compare to previous version (2018-08-22)
2018.08.22 12:27:42 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Load differential periods | time=8ms
2018.08.22 12:27:42 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.p.f.FileMoveDetectionStep] Either no files added or no files removed. Do nothing.
2018.08.22 12:27:42 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Detect file moves | time=8ms
2018.08.22 12:27:42 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Load inner file and in project duplications | time=48ms
2018.08.22 12:27:42 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.p.d.CrossProjectDuplicationStatusHolderImpl] Cross project duplication is disabled because it's disabled in the analysis report
2018.08.22 12:27:42 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Compute cross project duplications | time=0ms
2018.08.22 12:27:42 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Compute size measures | time=65ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Compute new coverage | time=193ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Compute coverage measures | time=1ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Compute comment measures | time=0ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Copy custom measures | time=2ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Compute duplication measures | time=0ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Compute duplication data measures | time=0ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Compute size measures on new code | time=0ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Compute language distribution | time=0ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Compute test measures | time=0ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Compute complexity measures | time=4ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Load measure computers | time=0ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.p.s.ExecuteVisitorsStep]   Execution time for each component visitor:
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.p.s.ExecuteVisitorsStep]   - LoadComponentUuidsHavingOpenIssuesVisitor | time=2ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.p.s.ExecuteVisitorsStep]   - IntegrateIssuesVisitor | time=343ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.p.s.ExecuteVisitorsStep]   - CloseIssuesOnRemovedComponentsVisitor | time=1ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.p.s.ExecuteVisitorsStep]   - MaintainabilityMeasuresVisitor | time=0ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.p.s.ExecuteVisitorsStep]   - NewMaintainabilityMeasuresVisitor | time=4ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.p.s.ExecuteVisitorsStep]   - ReliabilityAndSecurityRatingMeasuresVisitor | time=2ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.p.s.ExecuteVisitorsStep]   - NewReliabilityAndSecurityRatingMeasuresVisitor | time=3ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.p.s.ExecuteVisitorsStep]   - LastCommitVisitor | time=0ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.p.s.ExecuteVisitorsStep]   - MeasureComputersVisitor | time=0ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Execute component visitors | time=368ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Compute measure variations | time=161ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Compute Quality Gate measures | time=0ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Compute Quality profile measures | time=0ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Generate Quality profile events | time=1ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Generate Quality gate events | time=1ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Persist components | time=15ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Persist analysis | time=2ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Persist analysis properties | time=1ms
2018.08.22 12:27:43 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Persist measures | time=63ms
2018.08.22 12:27:45 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Persist live measures | time=1921ms
2018.08.22 12:27:45 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Persist issues | time=31ms
2018.08.22 12:27:45 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Persist project links | time=6ms
2018.08.22 12:27:45 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Persist events | time=5ms
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Persist sources | time=346ms
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Persist tests | time=3ms
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Persist cross project duplications index | time=0ms
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Enable analysis | time=4ms
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Update last usage date of quality profiles | time=2ms
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.d.p.p.KeepOneFilter] -> Keep one snapshot per day between 2018-07-25 and 2018-08-21
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.d.p.p.DefaultPeriodCleaner] <- Delete analyses of component AWP0Jm54EV7H7hTFLQct: 
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.d.p.p.KeepOneFilter] -> Keep one snapshot per week between 2017-08-23 and 2018-07-25
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.d.p.p.DefaultPeriodCleaner] <- Delete analyses of component AWP0Jm54EV7H7hTFLQct: 
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.d.p.p.KeepOneFilter] -> Keep one snapshot per month between 2013-08-28 and 2017-08-23
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.d.p.p.DefaultPeriodCleaner] <- Delete analyses of component AWP0Jm54EV7H7hTFLQct: 
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.d.p.p.KeepWithVersionFilter] -> Keep analyses with a version prior to 2016-08-24
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.d.p.p.DefaultPeriodCleaner] <- Delete analyses of component AWP0Jm54EV7H7hTFLQct: 
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.d.p.p.DeleteAllFilter] -> Delete data prior to: 2013-08-28
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.d.p.p.DefaultPeriodCleaner] <- Delete analyses of component AWP0Jm54EV7H7hTFLQct: 
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.d.purge.PurgeDao] <- Delete aborted builds
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Purge db | time=59ms
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.p.s.IndexAnalysisStep] Call org.sonar.server.measure.index.ProjectMeasuresIndexer@30ccfe20
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.p.s.IndexAnalysisStep] Call org.sonar.server.component.index.ComponentIndexer@43bf243d
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.p.s.IndexAnalysisStep] Call org.sonar.server.issue.index.IssueIndexer@5bc6f088
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.p.s.IndexAnalysisStep] Call org.sonar.server.permission.index.PermissionIndexer@6091542
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.p.s.IndexAnalysisStep] Call org.sonar.server.test.index.TestIndexer@6313eebc
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Index analysis | time=264ms
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Send issue notifications | time=1ms
2018.08.22 12:27:46 DEBUG ce[AWVhmszB7e7-9RhP0Omq][o.s.s.c.t.s.ComputationStepExecutor] Publish task results | time=0ms
2018.08.22 12:27:46 INFO  ce[AWVhmszB7e7-9RhP0Omq][o.s.c.t.CeWorkerImpl] Executed task | project=ACH | type=REPORT | id=AWVhmszB7e7-9RhP0Omq | submitter=admin | time=5488ms

Hi,

These are the server-side logs of analysis report processing. I was looking for the analysis logs.

Ann

[13:12:27] **[Step 5/5]* INFO: Source encoding: UTF-8, default locale: en_US*
[13:12:27] **[Step 5/5]* INFO: Index files*
[13:12:28] **[Step 5/5]* INFO: 24 files indexed*
[13:12:28] **[Step 5/5]* INFO: Quality profile for cs: Sonar way*
[13:12:28] **[Step 5/5]* INFO: Excluded sources for coverage:*
[13:12:28] **[Step 5/5]* INFO: file:**/test/***
[13:12:29] **[Step 5/5]* INFO: Sensor C# Properties [csharp]*
[13:12:29] **[Step 5/5]* INFO: Sensor C# Properties [csharp] (done) | time=2ms*
[13:12:29] **[Step 5/5]* INFO: Sensor SonarJavaXmlFileSensor [java]*

This is a part of the analysis log, specifically for the exclusion. The same path excludes the folder correctly but only if I set the sonar.exclusions variable. If I set sonar.coverage.exclusions with the same file path, it doesn’t exclude the code from coverage calculation.

Hi,

These are the logs I was looking for. Could you also add:

  • Which analyzer, and version you’re using
  • And for thoroughness, what your analysis command(s) look like

?

Ann

[13:41:38][Step 5/5] SonarScanner for MSBuild 4.1.1
[13:41:39][Step 5/5] INFO: SonarQube Scanner 3.1.0.1141
[13:41:39][Step 5/5] INFO: Java 1.8.0_181 Oracle Corporation (64-bit)
[13:41:39][Step 5/5] INFO: Linux 4.4.0-1065-aws amd64
[13:41:39][Step 5/5] INFO: SONAR_SCANNER_OPTS=-Xmx1024m
[13:41:39][Step 5/5] INFO: User cache: /root/.sonar/cache
[13:41:39][Step 5/5] INFO: SonarQube server 7.0.0

mono SonarScanner.MSBuild.exe begin /k:"%env.SonarProjectName%" /v:"%build.number%" /d:sonar.cs.opencover.reportsPaths="./coverage-results/results.opencover.xml"
mono SonarScanner.MSBuild.exe end

Hope I encompassed the correct information. Let me know if you need something else.

Hello @ganncamp,

Any updates on this issue?

Thank you,
Mihai

Hi,

Could you please try to remove the file: prefix in your exclusions patterns? Also try to pass the exclusion on the command line side, just to see if that help.

Any update in this issue? I`m facing similar problem =/

Hi @Renan_Young and welcome to our community forum!
Did you follow @Julien_HENRY’s suggestion? could you please let us know the version of the Scanner for MSBuild you are using? could you please let us know the regex you are using in the sonar.exclusions?

Hi.
I’m not sure if you’ve come up with the solution as yet. Here’s my take on it.

It seems in both cases, you are excluding the test package. Coverage however is applied to the src package. Try excluding specific directories/classes from your src package for the sonar.coverage.exclusions property.

1 Like

thank you, @ali-nutmeg, for the contribution to our community!

1 Like

Hi, Can you please let me know where to add the value for “sonar.coverage.exclusions”

Just pass it as a variable eg mvn sonar:sonar -Dsonar.coverage.exclusions=""

There are lots of ways. Just depends on how you send your code to sonar.

This is for java I guess, I am looking for C# project

I have added this thing

/d:sonar.coverage.exclusion=“APlusPlus.Data/*.cs”
but it is not working for me

Mihai Neagoe Hi, Can you please let me know how you did this, I am using version SonarQube Version 7.7 and I am not able to exclude project from code coverage

A post was split to a new topic: Sonar.coverage.exclusions in Jenkins pipeline

Hi!

Same issue here in 7.1.0.11001. We use a SQ server in our Jenkins pipeline setup. Administration is done via the administration web interface.

When entering a pattern under Analysis Scope > Coverage Exclusion, the matching files are NOT excluded from code coverage (unexpected). In our case, coverage remains at about 46%.

When entering the EXACT same pattern under Analysis Scope > Global Source File Exclusions, the matching files are no longer analysed and are NOT taken in account for code coverage (expected). In our case, coverage increases to 95%.

Hope this helps.

1 Like

I had a similar problem.
The “Coverage exclusion” change made in the global sonar administration is not copied to the project if something has already been manually set in the project administration (Project> Administration> Analysis Scope> Coverage Exclusion). In this case, you need to manually add a filter also to the project.
I.e. check if “Project> Administration> Analysis Scope> Coverage Exclusion” and “Global> Administration> Analysis Scope> Coverage Exclusion” are the same.