Sonar.sources seems ignored

I have a simple python project organized as follows:

src
  app.py
  fizzbuzz.py
test
  fizzbuzz_test.py

But it seems that sonar.sources is not working properly because SonarQube is considering tests file part of the coverage.

I’ve tried several configurations:

1. Simple config

sonar.sources=src
sonar.tests=test

but I got this error:

ERROR: Error during SonarScanner execution
ERROR: File test/fizzbuzz_test.py can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files

2. Complex test inclusion

sonar.sources=src
sonar.tests.inclusions=test/**/*_test.py

that pass indexing

INFO: Indexing files...
INFO: Project configuration:
INFO:   Excluded sources: .vscode, conftest.py
INFO: 10 files indexed...  (last one was .pylintrc)
INFO: 10 files indexed...  (last one was .pylintrc)
INFO: 32 files indexed
INFO: 1 file ignored because of inclusion/exclusion patterns

but tests files are indexed as code that SonarQube want to cover.

I’m scanning it with:

  • SonarQube: 9.5.0.56709
  • SonarScanner 4.7.0.2747

With this config:

sonar.sources=src/app.py
sonar.tests.inclusions=test/**/*_test.py

# Goal: i want to delete this line
# sonar.coverage.exclusions=test/**/*, docs/**/*

sonar.exclusions=.vscode, conftest.py

sonar.language=py
sonar.python.version=3.9
sonar.sourceEncoding=UTF-8

# Unit tests
sonar.python.xunit.reportPath=build/reports/tests/*.xml
sonar.python.coverage.reportPaths=build/reports/coverage/*.xml

What I’m missing to proper configure src files ?

This config should work if your project is laid out exactly as described.

And in my tests it works just fine.

.
├── src
│   └── person.py
└── test
    └── person_test.py
sonar-scanner -Dsonar.sources=src -Dsonar.tests=test -Dsonar.projectKey=test -X

10:26:07.800 INFO: Indexing files...
10:26:07.801 INFO: Project configuration:
10:26:07.825 DEBUG: Average line length for src/person.py is 10
10:26:07.829 DEBUG: 'src/person.py' indexed with language 'py'
10:26:07.857 DEBUG: Average line length for test/person_test.py is 10
10:26:07.857 DEBUG: 'test/person_test.py' indexed as test with language 'py'
10:26:07.868 INFO: 2 files indexed

Just a shot in the dark – can you try running the scanner with analysis parameters passed on the command line (rather than a sonar-project.properties file)?

1 Like

It was a stupid issue, I spent days before writing this post trying to figure out the problem.

Thanks to your comment I have double-check the command line and I find out an option -Dsonar.sources=. that was overriding sonar-project.properties file entry. :sweat_smile:

Thanks, a lot!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.