Sonarscanner ignore the sonar.exclusions

Hello,

I have a CI with Github Action that use dotnet-sonarscanner.
I have some unit test that uses assets like sample json, html, … I don’t want to analyse these files because they are not part of the project but externals samples.

I’m unable to get these file excluded from analyses, this make me crazy ! :exploding_head::crazy_face:
Do you have any idea of what I’m doing wrong ?

Thanks !

Here is the sonar scanner command :

./.sonar/scanner/dotnet-sonarscanner begin /k:"wavenet-be_******" /o:"wavenet-be" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.projectBaseDir="${{ github.workspace }}" /d:sonar.scm.provider=git /d:sonar.cs.opencover.reportsPaths="src/Project/**/TestResults/**/coverage.opencover.xml" -d:sonar.cs.vstest.reportsPaths="src/Project/**/TestResults/*.trx" /v:${{ env.MAJOR }}.${{ env.MINOR }}.${{ env.PATCH }}
dotnet build ./src/Project
dotnet test ./src/Project--logger trx --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover
./.sonar/scanner/dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"

(Also tested with the task highbyte/sonarscan-dotnet, same issue)

I have setup the source exclusions :

And the exclusions is being retrieved by sonar scanner :

INFO: Load project repositories
INFO: Load project repositories (done) | time=165ms
INFO: SCM collecting changed files in the branch
INFO: SCM collecting changed files in the branch (done) | time=160ms
INFO: Indexing files...
INFO: Project configuration:
INFO:   Excluded sources: **/build-wrapper-dump.json, src/Project/Wavenet.ProjectSolution.Application.Tests/Assets/**/*.*, **/Assets/**
INFO:   Excluded sources for coverage: **/Wavenet.ProjectSolution.Functions/**/*, **/Wavenet.ProjectSolution/Domain/**, **/Wavenet.ProjectSolution/Infrastructure/**
INFO: Indexing files of module 'Wavenet.ProjectSolution.Functions'
INFO:   Base dir: /home/runner/work/client-project/client-project/src/Scrapper/Wavenet.ProjectSolution.Functions
INFO:   Source paths: ...
INFO:   Excluded sources: **/build-wrapper-dump.json, src/Scrapper/Wavenet.ProjectSolution.Application.Tests/Assets/**/*.*, **/Assets/**
INFO:   Excluded sources for coverage: **/Wavenet.ProjectSolution.Functions/**/*, **/Wavenet.ProjectSolution/Domain/**, **/Wavenet.ProjectSolution/Infrastructure/**
INFO: Indexing files of module 'Wavenet.ProjectSolution'
INFO:   Base dir: /home/runner/work/client-project/client-project/src/Scrapper/Wavenet.ProjectSolution
INFO:   Source paths: ...
INFO:   Excluded sources: **/build-wrapper-dump.json, src/Scrapper/Wavenet.ProjectSolution.Application.Tests/Assets/**/*.*, **/Assets/**
INFO:   Excluded sources for coverage: **/Wavenet.ProjectSolution.Functions/**/*, **/Wavenet.ProjectSolution/Domain/**, **/Wavenet.ProjectSolution/Infrastructure/**
INFO: Indexing files of module 'Wavenet.ProjectSolution.Application.Tests'
INFO:   Base dir: /home/runner/work/client-project/client-project/src/Scrapper/Wavenet.ProjectSolution.Application.Tests
INFO:   Test paths: CollectorSquare/Queries/GetCollectorSquareBagsPagesToScrapHandl...
INFO:   Excluded sources: **/build-wrapper-dump.json, src/Scrapper/Wavenet.ProjectSolution.Application.Tests/Assets/**/*.*, **/Assets/**
INFO:   Excluded sources for coverage: **/Wavenet.ProjectSolution.Functions/**/*, **/Wavenet.ProjectSolution/Domain/**, **/Wavenet.ProjectSolution/Infrastructure/**
INFO: Indexing files of module 'wavenet-be_client-project-scrapper'
INFO:   Base dir: /home/runner/work/client-project/client-project
INFO:   Excluded sources: **/build-wrapper-dump.json, src/Scrapper/Wavenet.ProjectSolution.Application.Tests/Assets/**/*.*, **/Assets/**
INFO:   Excluded sources for coverage: **/Wavenet.ProjectSolution.Functions/**/*, **/Wavenet.ProjectSolution/Domain/**, **/Wavenet.ProjectSolution/Infrastructure/**
INFO: 92 files indexed
INFO: Quality profile for cs: Sonar way
INFO: Quality profile for json: Sonar way
INFO: Quality profile for web: Sonar way

But … sonarcloud continue to show errors from these files …

Any idea ?
I would buy a coffee or a beer to someone who point out my mistakes :sweat_smile::pray:

We usually use the sonar-project.properties to set these things. Just a WILD guess, try to exclude **/assets/** in addition to **/Assets/**. While at it, add this in the mix: **/Assets/**/*.*.

Note that I did run into some bugs while using the GH actions scanner in the past, so it might be something similar.

If you do use the properties file please make sure you these these 2 settings to something:

sonar.sources = .
sonar.tests = .

Without these, the coverage reports do not work for example.

@simonbaudart I took an other look at the docs: Analysis scope & SonarCloud

** is to match directories, never files so you want **/Assets/**/* since **/Assets/** will match all the directories inside any Assets subfolder, but will not exclude the actually files, so you need the trailing /* to actually match the files.

Update: I confirmed with the Sonar team and a trailing /** or /**/* are functionally the same thing.

Hello,

I changed the path, to test all cases, I’ve put this :

INFO: Indexing files...
INFO: Project configuration:
INFO:   Excluded sources: **/build-wrapper-dump.json, **/Assets/**, **/Assets/**/*, **/assets/**/*

But the issue is still the same …

I also try to add the sonar-project.properties, but sonar scanner for msbuild is not happy : 08:54:59.124 sonar-project.properties files are not understood by the SonarScanner for MSBuild.

Thanks for your help !

Simon

Hello !

Thanks to the /d:sonar.verbose=true flag, I found that the issues where pushed because the assets files where detected as test files because they are parts of the unit test project.

So I had to exclude these as well in the tests sources files, and now it’s ok ! :raised_hands:

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