How should I set the sonar.coverage.exclusions

The documentation includes general guidance on adding coding rules, including which languages support adding custom rules, and general guidance on how to go about it.
If the docs don’t answer your question, please tell us:

The folder structure is as follows: sub folders B1, B2, B3, … B30 is under folder B, I just want to exclude B1, B2 and B3,
So my sonar.coverage.exclusions should be something like “**/B/B1/**, **/B/B2/**, **/B/B3/**”
and I need to type “**/B” for 3 times, is there any way to simplify this so that I only needs to type it once. for example, the rule may be something like this “**/B/{B1, B2, B3}”
Please share the relevant code snippet, along with any error messages you’re encountering:

AFAIK These follow Ant style patterns. So doing this in your sonar-project.properties file should work:

sonar.coverage.exclusions = \
  **/B/B1/**,\
  **/B/B2/**,\
  **/B/B3/**

Or single line:

sonar.coverage.exclusions = **/B/B1/**, **/B/B2/**, **/B/B3/**

It might be possible to use a regex pattern, if sonar uses Spring under the hood, but I have zero idea if that’s actually supported.

sonar.coverage.exclusions = **/B/{subdir:B[1-3]}/**

What we do is that we actually have a wrapper before calling the sonar scanner. This wrapper sets environment variables, such as directories to exclude and these variables are then passed to the properties file.

sonar.coverage.exclusions = \
  ${env.SONAR_ADDITIONAL_COVERAGE_EXCLUSIONS}\
  test/**,\
  **/setup.py

And in your case the code in a shell script wrapper would look like:

SONAR_ADDITIONAL_COVERAGE_EXCLUSIONS=''
for dir in B/B{1,2,3}; do
    SONAR_ADDITIONAL_COVERAGE_EXCLUSIONS="${SONAR_ADDITIONAL_COVERAGE_EXCLUSIONS}**/${dir}/**,"
done
export SONAR_ADDITIONAL_COVERAGE_EXCLUSIONS

SONAR_ADDITIONAL_COVERAGE_EXCLUSIONS is just a name we picked to be very unique, you can pick whatever you like.

Of course if you goal is to avoid repeating for just 3 directories, that’s not very efficient, but for larger use cases it might work for you.

Let us know if these work. I’m actually curious about the regex option. It is undocumented so it might not be future proof, even if it works today.

Update: After looking at the docs it seems that ** is only for folders, so you might need to finish your patterns with **/* to actually exclude files.

Thanks for the great answer @sodul

Just to add a little extra info: no, it’s not possible to do any fancy regex (if I read the code correctly, we actually guard against it). We ultimately convert the fairly simple ant patterns into regex. You can see the implementation here: sonar-plugin-api/plugin-api/src/main/java/org/sonar/api/utils/WildcardPattern.java at master · SonarSource/sonar-plugin-api · GitHub

@Colin The documentation is not 100% explicit about the patterns.

Can we use foo/** and foo/**/* interchangeably or do we need to use foo/**/* only?

If I look at the default settings for Go for example there is **/vendor/** for sonar.go.exclusions.

The documentation always use /**/* and seems to imply that /**` will only match directories with no clear guidance. Maybe it ‘depends’ for each setting, but if that’s the case it should be clarified explicitly in the documentation of each setting.

Just using sonar.exclusions=** (for example) will indeed exclude all files. If I had to guess, we usually document **/* because the most common use case is excluding a specific file extension (**/*.js).

@Colin thanks for the clarification. Can you get the documentation updated to clarify this explicitly? This is an other source of potential confusion when trying to tune the configs.

@sodul Just to be clear, you’d like some mention that ** and **/* are functionally equivalent?

Yes, as trailing patterns. It does not have to be a long note, but it will avoid users having to guess and experiment if this is explicitly documented.

1 Like