Header Files are Excluded from Analysis when Using any Sort of Scoping

ALM used (GitHub, Bitbucket Cloud, Azure DevOps)


CI system used (Bitbucket Cloud, Azure DevOps, Travis CI, Circle CI)


Scanner command used when applicable (private details masked)

(ommiting details like login, repo, project key, etc…)

sonar-scanner -Dsonar.pullrequest.base=master -Dsonar.pullrequest.branch=foo -Dsonar.coverageReportPaths=./build/code_coverage.xml -Dsonar.cfamily.compile-commands=./build/compile_commands.json

Languages of the repository


Error observed (wrap logs/code around with triple quotes ``` for proper formatting)

The problem with the above is if I try to add any sort of scoping rules via sonar-project.properties, then all of the sudden the report starts filtering out header files and test files. This applies to both regular analysis and coverage analysis despite the documentation stating that analysis scoping rules don’t apply to coverage results.

When no sonar-project.properties file is specified, the header and test files appear in the analysis, and coverage is reported.

Here’s how the project is structured:

- project_foo/
  - foo_utils/
    - include/foo_utils/
      - foo_util.h
    - src/
      - version.cc.in (template for file generated as part of build)
    - test/
      - test_foo_util.cc

This is a new project. Currently it consists of header-only code and tests for that code.

Here is a sample sonar-project.properties file I’ve tried:


The resulting error:

[2022-07-14T04:04:26.211Z] ERROR: Error during SonarScanner execution

[2022-07-14T04:04:26.211Z] java.lang.IllegalStateException: The Compilation Database JSON file was found but 0 C/C++/Objective-C files were analyzed. Please make sure that:

[2022-07-14T04:04:26.211Z]   * you are correctly invoking the scanner with correct configuration

[2022-07-14T04:04:26.211Z]   * your compiler is supported

[2022-07-14T04:04:26.211Z]   * you are providing the path to the correct Compilation Database JSON

[2022-07-14T04:04:26.211Z]   * you are building and analyzing the same source checkout, absolute paths must be identical in build and analysis steps

The behavior can be slightly different depending on the scoping rules. For instance in other scenarios we are not seeing any error, it’s just that the headers and test files are not included in the analysis.

Mainly I just want to understand why, when no scoping rules are present, the scanner seems to be able to handle headers just fine, but as soon as any are added, it starts ignoring them.

Hello @isaactorz,

A header file is not analyzed by itself, but because it is included from another file. In the case of a header-only library, this other file can be a unit test file that exercises the header. In that case, the test file should be included as part of sonar.sources, not sonar.tests, which is not supported for C or C++ (see more details).

If you don’t specify sonar.sources, it will default to your project root, and include the test files. As soon as you specify it, you must ensure that test files are part of it.

If the previous comment does not solve your issue, we will need to see your compile_commands.json file, to better understand the structure of your project.

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