SonarOpenCommunity / sonar-cxx

SonarQube C++ Community plugin (cxx plugin): This plugin adds C++ support to SonarQube with the focus on integration of existing C++ tools.
GNU Lesser General Public License v3.0
975 stars 361 forks source link

sonar.cxx.jsonCompilationDatabase not working #2700

Open loomdoom opened 2 weeks ago

loomdoom commented 2 weeks ago

Describe the bug Trying to run analysis using compile_commands.json. Tested with testdata from: https://github.com/SonarOpenCommunity/sonar-cxx/integration-tests/testdata/json_db_project/

To Reproduce Steps to reproduce the behavior:

  1. Get copy of "SonarOpenCommunity/sonar-cxx/integration-tests/testdata/json_db_project/"
  2. Run docker sonarqube: docker run -d --name sonarqube -e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true -p 9000:9000 sonarqube:9.9.6-community
  3. install sonar-cxx: sudo docker cp sonar-cxx-plugin-2.1.1.488.jar sonarqube:/opt/sonarqube/extensions/plugins/sonar-cxx-plugin-2.1.1.488.jar
  4. configure sonar qube add project with project key "json_db_project", generate token etc
  5. Enter folder where json_db_project/ is located
  6. sonar-scanner -X
  7. result:

    18:34:52.402 INFO Project configuration: 18:34:52.416 DEBUG 'src/file4.cc' indexed with no language 18:34:52.424 DEBUG 'src/file5.cc' indexed with no language 18:34:52.425 DEBUG 'src/file1.cc' indexed with no language 18:34:52.427 DEBUG 'src/file6.cc' indexed with no language 18:34:52.429 DEBUG 'src/file2.cc' indexed with no language 18:34:52.436 DEBUG 'src/file3.cc' indexed with no language 18:34:52.440 INFO 6 files indexed

and:

17:46:21.551 DEBUG Parsing 'JSON Compilation Database' format 17:46:21.773 DEBUG sonar.cxx.metric.api.file.suffixes: [.hxx, .hpp, .hh, .h] 17:46:21.782 DEBUG 'Complex Functions' metric threshold (cyclomatic complexity): 10 17:46:21.784 DEBUG 'Big Functions' metric threshold (LOC): 20 17:46:21.791 INFO Analyze only files contained in 'JSON Compilation Database': 0 files

Expected behavior expect to analyze files specified in compile_commands.json

Desktop :

sonar-project.properties - just modified to run with docker

sonar.projectKey=json_db_project sonar.scm.disabled=true sonar.xml.file.suffixes=.disable-xml sonar.cxx.file.suffixes=.cxx,.cpp,.cc,.c,.hxx,.hpp,.hh,.h sonar.sources=src sonar.host.url=http://localhost:9000 sonar.login=A_TOKEN sonar.cxx.jsonCompilationDatabase=compile_commands.json sonar.cxx.jsonCompilationDatabase.analyzeOnlyContainedFiles=true

scaner.log

guwirth commented 1 week ago

Hi @loomdoom,

sorry for the delayed answer.

Looking into your scaner.log:

17:46:16.430 INFO  Scanner configuration file: /redactedpath/scripts/sonar-scanner-6.0.0.4432-macosx/conf/sonar-scanner.properties
17:46:16.437 INFO  Project root configuration file: /redactedpath/projects/sonar-cxx/integration-tests/testdata/json_db_project/sonar-project.properties
17:46:16.468 INFO  SonarScanner CLI 6.0.0.4432
17:46:16.475 INFO  Java 17.0.11 Eclipse Adoptium (64-bit)
17:46:16.476 INFO  Mac OS X 14.1 x86_64

17:46:17.320 DEBUG Work directory: /redactedpath/projects/sonar-cxx/integration-tests/testdata/json_db_project/.scannerwork
17:46:17.520 DEBUG Community 9.9.6.92038

17:46:17.990 DEBUG Plugins:
17:46:17.990 DEBUG   * C++ (Community) 2.1.1.488 (cxx)

17:46:18.706 INFO  Project key: json_db_project
17:46:18.706 INFO  Base dir: /redactedpath/projects/sonar-cxx/integration-tests/testdata/json_db_project
17:46:18.706 INFO  Working dir: /redactedpath/projects/sonar-cxx/integration-tests/testdata/json_db_project/.scannerwork

17:46:18.848 DEBUG Creating module hierarchy
17:46:18.848 DEBUG   Init module 'json_db_project'
17:46:18.848 DEBUG     Base dir: /redactedpath/projects/sonar-cxx/integration-tests/testdata/json_db_project
17:46:18.848 DEBUG     Working dir: /redactedpath/projects/sonar-cxx/integration-tests/testdata/json_db_project/.scannerwork

17:46:18.874 DEBUG Available languages:
17:46:18.875 DEBUG   * CXX => "cxx"

17:46:19.923 DEBUG Declared extensions of language CXX were converted to sonar.lang.patterns.cxx : **/*.cxx,**/*.cpp,**/*.cc,**/*.c,**/*.hxx,**/*.hpp,**/*.hh,**/*.h

17:46:19.942 INFO  Indexing files...
17:46:19.942 INFO  Project configuration:
17:46:19.958 DEBUG 'src/file4.cc' indexed with language 'cxx'
17:46:19.965 DEBUG 'src/file5.cc' indexed with language 'cxx'
17:46:19.969 DEBUG 'src/file1.cc' indexed with language 'cxx'
17:46:19.970 DEBUG 'src/file6.cc' indexed with language 'cxx'
17:46:19.974 DEBUG 'src/file2.cc' indexed with language 'cxx'
17:46:19.981 DEBUG 'src/file3.cc' indexed with language 'cxx'
17:46:19.985 INFO  6 files indexed

17:46:21.433 INFO  ------------- Run sensors on project
17:46:21.527 INFO  Sensor CXX [cxx]
17:46:21.551 DEBUG Parsing 'JSON Compilation Database' format
17:46:21.791 INFO  Analyze only files contained in 'JSON Compilation Database': 0 files
17:46:21.791 ERROR No files are analyzed, check the settings of 'sonar.projectBaseDir' and 'sonar.cxx.jsonCompilationDatabase'.
17:46:21.794 INFO  Sensor CXX [cxx] (done) | time=267ms

There is no match for Analyze only files contained in 'JSON Compilation Database': 0 files. That means the files/paths in the compilation database does not match. The corresponding code is here: https://github.com/SonarOpenCommunity/sonar-cxx/blob/398033454fca8749927a9f6bc14a22e77de4b828/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/CxxSquidSensor.java#L410

As in all reports the hints here are valid: https://github.com/SonarOpenCommunity/sonar-cxx/wiki/Troubleshooting-Reports#file-path-issues

Regards,