Exclusions don't work in pysonar

I’m trying to get started with SonarCloud on a Python and web project.

I’m trying to diagnose an issue with pysonar miscounting the number of lines. This mis-count is a separate issue.

To do this I want to temporarily exclude the web directory. I tried this:

pysonar \
  --sonar-token=XXX \
  --sonar-project-key=XXX \
  --sonar-organization=XXX --sonar.coverage.exclusions='web/**'

According to the docs, that option should work. But I see in the logs:

WARNING: Too many duplication groups on file web/src/components/XXX.ts. Keep only the first 100 groups.
WARNING: Too many duplication references on file web/src/components/YYY.ts for block at line 978. Keep only the first 100 references.
WARNING: Too many duplication groups on file web/src/components/ZZZ.ts. Keep only the first 100 groups.

So it looks like pysonar is not respecting that exclusion. I’ve also tried supplying --sonar-coverage-exclusions='web' which also does’t work.

When I supply --debug:

DEBUG: Properties: {"scannerProperties": [{"key": "sonar.scanner.app", "value": "python"},
 {"key": "sonar.scanner.appVersion", "value": "1.0"},
 {"key": "sonar.scanner.bootstrapStartTime", "value": XXX},
 {"key": "sonar.verbose", "value": true},
 {"key": "sonar.scanner.skipJreProvisioning", "value": false},
 {"key": "sonar.scanner.connectTimeout", "value": 5},
 {"key": "sonar.scanner.socketTimeout", "value": 60},
 {"key": "sonar.scanner.responseTimeout", "value": 0},
 {"key": "sonar.scanner.truststorePassword", "value": "changeit"},
 {"key": "sonar.scanner.keystorePassword", "value": "changeit"},
 {"key": "sonar.scanner.os", "value": "mac"},
 {"key": "sonar.scanner.arch", "value": "aarch64"},
 {"key": "sonar.projectBaseDir", "value": XXX"},
 {"key": "sonar.organization", "value": "XXX"},
 {"key": "sonar.token", "value": "XXX"}, 
 {"key": "sonar.projectKey", "value": "XXX"},
 {"key": "sonar.coverage.exclusions", "value": "web"},
 {"key": "sonar.host.url", "value": "https://sonarcloud.io"},
 {"key": "sonar.scanner.apiBaseUrl", "value": "https://api.sonarcloud.io"},
 {"key": "sonar.scanner.sonarcloudUrl", "value": "https://sonarcloud.io"},
 {"key": "sonar.scanner.proxyPort", "value": "443"},
 {"key": "sonar.scanner.javaExePath", "value": "XXX/.sonar/cache/OpenJDK21U-jre_aarch64_mac_hotspot_21.0.9_10.tar.gz_unzip/jdk-21.0.9+10-jre/Contents/Home/bin/java"}]}

How can I exclude a directory from analysis?

Just realised that sonar-coverage-exclusions is for code coverage, not sonar analysis. So that’s not the right option.

So I added this to my pyproject.toml:

[tool.sonar]
sources = "src"
exclusions = "web"

I also included --toml-path=pyproject.toml to be sure it is using that toml file.

I’m still seeing the same reports for files I want to exclude, and the same line count reported.

The debug log shows

INFO: 0 files ignored because of inclusion/exclusion patterns

Any ideas?

Hi,

Welcome to the community!

Let’s say you got half right the first time and the other half right the second time. :sweat_smile:

Try exclusions = web/**/*. That’s all files (/*) under 0-n levels of subdirectories (/**).

 
HTH,
Ann

Ann,

Thanks very much. I did actually try exactly that after posting the question. I still see the same log messages that include the web directory, and the same “too many lines” error.

Is it possible that there’s a bug in the pysonar implementation? Or that this setting isn’t picked up from the project.toml file?

I can’t find mention of exclusions inthe pysonar docs. They refer to the ‘analysis parameters’ page which also doesn’t mention it specifically. Although it does then link to the scope page, which does.

Any other ideas?

Hi,

Let’s fall back here. Can you either set this exclusion in the UI or on the analysis command line by adding -Dsonar.exclusions=web/**/*?

 
Thx,
Ann

Ann,

Thanks for your help.

That does work! Supplying -Dsonar.exclusions='web/**/*'

INFO:   Excluded sources: web/**/*, **/build-wrapper-dump.json

And the analysis all works.

UI

I’m not sure which UI you’re referring to. I’m just running this from the command line.

TOML Path

When I remove the --toml-path argument it picks up exclusions from the config file just fine. So it works as expected with an un-specified toml path.

I was adding that for debugging purposes. I tried a relative path pyproject.toml , ./pyproject.toml, and full /Users/me/path/to/project.toml. I don’t need to use this setting, so I’m happy to drop that. But it does look a little strange.

Thanks again

Joe

1 Like

Hi Joe,

Thanks for reporting back. I’ve forwarded this to the maintainers of the scanner. I know they’ll want to take a look.

 
Ann

Hi @afandian ,

Thank you for reporting this issue!

There is an inconsistency in the behavior and the documentation of PySonar. The --toml-path is in fact requiring the path to the folder holding the pyproject.toml file which contradicts the documentation.

I have created a ticket to fix the documentation as well as to allow for the user to pass the path to the file itself.

Thank you again for your feedback.

David

1 Like

Thank you, glad it’s explainable!