GitLab CI with Platformio - SonarScanner execution: 0 files were analyzed

Hello. :wave:

I’m new to SonarCloud and yesterday I spent several hours trying to set up SonarCloud with GitLab CI/CD for a Platformio project.

I took inspiration from your official guide and replace cmake with Platformio.

The .gitlab-ci.yml is the following

image: python:3.11

cache:
  paths:
    - ~/.cache/pip
    - ~/.platformio/.cache

variables:
  PIO_LIB_FOLDER: lib
  SONAR_SERVER_URL: "https://sonarcloud.io" 
  SONAR_SCANNER_VERSION: 5.0.1.3006 # Find the latest version in the "Linux" link on this page:
                                    # https://docs.sonarcloud.io/advanced-setup/ci-based-analysis/sonarscanner-cli/ 
  BUILD_WRAPPER_OUT_DIR: build_wrapper_output_directory # Directory where build-wrapper output will be placed
  
  SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"  # Defines the location of the analysis task cache
  GIT_DEPTH: "0"  # Tells git to fetch all the branches of the project, required by the analysis task
  # note that SONAR_TOKEN is transmitted to the environment through Gitlab CI

get-sonar-binaries:
  # this job download and unpacks the build-wrapper and the sonar-scanner
  # in this example it is done for every build.
  # This can be optimized by caching the files or better, by including them, in the build docker image.
  stage: .pre
  script:
    # Download sonar-scanner
    - curl -sSLo sonar-scanner.zip "https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-${SONAR_SCANNER_VERSION}-linux.zip"
    - unzip -o sonar-scanner.zip 
    - mv sonar-scanner-${SONAR_SCANNER_VERSION}-linux sonar-scanner
    # Download build-wrapper
    - curl -sSLo build-wrapper.zip "${SONAR_SERVER_URL}/static/cpp/build-wrapper-linux-x86.zip" 
    - unzip -o build-wrapper.zip
    - mv build-wrapper-linux-x86 build-wrapper
  cache:
    policy: push
    key: "${CI_COMMIT_SHORT_SHA}"
    paths:
      - build-wrapper/ # to share the build-wrapper between jobs
      - sonar-scanner/ # to share the sonar-scanner between jobs

build:
  stage: build
  # install the necessary build tools when needed
  before_script:
     - "pip install -U platformio"
  script:
    # prepare the build tree
    - mkdir build
    # run the build inside the build wrapper
    - build-wrapper/build-wrapper-linux-x86-64 --out-dir "${BUILD_WRAPPER_OUT_DIR}" platformio ci -e "ArduinoMaster" --build-dir="./build" --keep-build-dir --project-conf=platformio.ini  ./src/
    #- "pio run -e ArduinoSlaveRelay"
  artifacts:
    paths:
      - ${BUILD_WRAPPER_OUT_DIR}/build-wrapper-dump.json
     #- src/sonar_scanner_example
  cache:
    policy: pull-push
    key: "${CI_COMMIT_SHORT_SHA}"
    paths:
      - build-wrapper/
      - sonar-scanner/
      - "${BUILD_WRAPPER_OUT_DIR}"

sonarcloud-check:
  stage: .post
  cache:
    policy: pull
    key: "${CI_COMMIT_SHORT_SHA}"
    paths:
      - sonar-scanner/
      - "${BUILD_WRAPPER_OUT_DIR}"
  script:
    - sonar-scanner/bin/sonar-scanner --define sonar.host.url="${SONAR_SERVER_URL}" --define sonar.cfamily.build-wrapper-output="${BUILD_WRAPPER_OUT_DIR}"

The sonar-project.properties:

sonar.projectKey=paolino625_dcc-command-station
sonar.organization=paolino625

# This is the name and version displayed in the SonarCloud UI.
#sonar.projectName=DCC Command Station
#sonar.projectVersion=1.0

# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
sonar.sources=src
sonar.sourceEncoding=UTF-8

# sonar.cfamily.build-wrapper-output=bw-output

I had a look at the build-wrapper-dump.json and it SEEMS ok to me.
I’m attaching it here.
build-wrapper-dump.json (7.0 MB)

While executing the job I get the following error at the sonarcloud-check stage.

ERROR: Error during SonarScanner execution
java.lang.IllegalStateException: The "build-wrapper-dump.json" file was found but 0 C/C++/Objective-C files were analyzed. Please make sure that:
  * you are using the latest version of the build-wrapper and the CFamily analyzer
  * you are correctly invoking the scanner with correct configuration
  * your compiler is supported
  * you are wrapping your build correctly
  * you are wrapping a full/clean build
  * you are providing the path to the correct build-wrapper output directory
  * you are building and analyzing the same source checkout, absolute paths must be identical in build and analysis steps

I’m attaching here the full logs of SonarScanner with -X flag.
LogSonarScanner.txt (1.9 MB)

It must be an easy detail I missed but I cannot find out which one is.
Any suggestions?

Thank you in advance! :blush:

Hello @Paolino625, and welcome to the community.

There is one thing that strikes me from the output of the build wrapper. I can see that the working directory for the compiler invocation is /builds/paolino625/dcc-command-station/build, and the compiled file is, say, src/Progetto/ArduinoMaster/ArduinoMaster.cpp. This means that the file that gets compiled is actually /builds/paolino625/dcc-command-station/build/src/Progetto/ArduinoMaster/ArduinoMaster.cpp.

But the indexed file is /builds/paolino625/dcc-command-station/src/Progetto/ArduinoMaster/ArduinoMaster.cpp.

I am not familiar with Platformio, and not 100% sure, but I think platformio ci SRC copies the sources specified as parameters to a transient project (inside build). So what is being compiled and what is being analyzed is not quite the same.

Would replacing platformio ci with platformio run work in your case? I think that may work. i.e

- build-wrapper/build-wrapper-linux-x86-64 --out-dir "${BUILD_WRAPPER_OUT_DIR}" platformio run -e "ArduinoMaster" --disable-auto-clean --project-conf=platformio.ini  ./src/

IIUC the documentation, it should use ./build as a building directory as well.

Thank you so much for your interest! :smile:

I transitioned to using platformio ci after experimenting with platformio run.

In fact, using

- build-wrapper/build-wrapper-linux-x86-64 --out-dir "${BUILD_WRAPPER_OUT_DIR}" platformio run -e"ArduinoMaster" --disable-auto-clean --project-conf=platformio.ini`

I would get the same problem:

java.lang.IllegalStateException: The "build-wrapper-dump.json" file was found but 0 C/C++/Objective-C files were analyzed.

but also several warnings like this one:

 WARN: Provided compiler is not compatible.

I was thinking these warnings were connected to the main problem and for some reason build-wrapper works only with platformio ci.

Anyway, you can have a look at the build-wrapper dump using platformio run
build-wrapper-dump.json (6.9 MB)

and at the sonar log
Log.txt (1.6 MB)

Any suggestions to solve this issue? :thinking:

From the logs:

15:24:51.902 DEBUG: Probing compiler: [/root/.platformio/packages/toolchain-gccarmnoneeabi/bin/arm-none-eabi-c++, -x, c++, --std, gnu++14, -mcpu=cortex-m7, -funsigned-char, -fno-rtti, -mfloat-abi=soft, -mfloat-abi=softfp, -mfpu=fpv5-d16, -mthumb, -v, -dM, -E, -]
15:24:51.911 DEBUG: stdout:

15:24:51.912 DEBUG: stderr:
[Error] Couldn't run program "/root/.platformio/packages/toolchain-gccarmnoneeabi/bin/arm-none-eabi-c++" in directory "/builds/paolino625/dcc-command-station" because: No such file or directory

The error is kind of confusing, but I believe it means the directory /builds/paolino625/dcc-command-station is missing. Since you have the flag that should keep the build directory after finishing, my guess would be that you need to tell GitLab CI to preserve it for the next step: try adding it to artifacts

In fact, the example you used as a base, does have build/sonar_scanner_example on the artifact list. In your case it would be /build/paolino625/...

Hello, I’ve conducted some tests and found that while the folder /builds/paolino625/dcc-command-station is present, the ‘packages’ folder within .platformio is missing. It appears that these files vanish between stages. I attempted to include .platformio in the artifacts, but without success.

Ultimately, I resolved the issue by consolidating everything into a single stage. Although not the most elegant solution, I opted for pragmatism.

For reference, I’m attaching the complete .yml file here, which may prove helpful to others.
gitlab-ci.yml.txt (2.6 KB)

1 Like

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