0

There is a fileA on master (say it consists of 1000 lines)

We are on feature_branch, where fileA has length of say 1050 lines.

On our CI pipeline, we are running coverage (it is a Python project) and it produces coverage.xml, to be used as cov input to SQ.

Our SQ preview stage is as follows:

    - git config --global user.name "SonarQube"
    - git config --global user.email "sonarqube@somedomain.com"
    - git checkout origin/master
    - git merge $CI_BUILD_REF --no-commit --no-ff
    - sonar-scanner -Dsonar.analysis.mode=preview -Dsonar.gitlab.project_id=$CI_PROJECT_PATH -Dsonar.login=$SONARQUBE_TOKEN -Dsonar.gitlab.commit_sha=$CI_COMMIT_SHA -Dsonar.gitlab.ref_name=$CI_COMMIT_REF_NAME
  except:
   - master@mainprojectnamespace

The problem is the following:

AFTER the merge, fileA ends up having (again) 1000 lines

HOWEVER, coverage was run BEFORE the merge (in a previous job) so it includes results for lines > 1000 (which is fileA's length after the merge)

This causes (I believe) the following error:

ERROR: Error during SonarQube Scanner execution
java.lang.IllegalStateException: Line 1040 is out of range in the file path/to/fileA.py (lines: 1000)

How do we go about this?

pkaramol
  • 16,451
  • 43
  • 149
  • 324

1 Answers1

3

SonarPython, like all code analyzers that import coverage report, requires that analyzed source code is strictly identical to the one used to generate the coverage report. First, you should challenge the need to merge your branch before running sonar-scanner. And, if you really need to run sonar-scanner on a branch merged with master, you also need to run the coverage analysis on the same branch merged with master.