Ruby coverage analysis fails - indexed files are not found in the filesystem

CI system used: Gitlab

Scanner command

  • sonar-scanner -X

Languages of the repository

  • Ruby

Error observed

...
...
12:45:22.395 INFO: Sensor SimpleCov Sensor for Ruby coverage [ruby]
12:45:22.559 WARN: Importing SimpleCov resultset JSON will not be supported from simplecov 18.0. 
...
...
12:44:58.975 DEBUG: 'app/models/product_target.rb' indexed with language 'ruby'
...
...
...
12:45:22.630 WARN: File 'app/models/product_target.rb' is present in coverage report but cannot be found in filesystem
...
...

As a result, the coverage analysis indicates 0% coverage.

Steps to reproduce

  • Setup a Ruby on Rails project using the following gems for the coverage report:
gem 'simplecov', '~> 0.17.1', require: false
gem 'simplecov-json', require: false

adding to your test helper file:

require 'simplecov'
require 'simplecov-json'
SimpleCov.formatter = SimpleCov::Formatter::JSONFormatter
SimpleCov.start 'rails'
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
sonar.sources=app/
sonar.exclusions=app/assets/**/*

sonar.tests=test/
#sonar.test.inclusions=

# Test coverage
#sonar.coverage.exclusions=

# SimpleCov report
sonar.ruby.coverage.reportPaths=./coverage/.resultset.json
ms2_test:
  stage: test
  ...
  script:
    - bundle exec rake db:create db:schema:load
    - bundle exec rails test -d -b
  after_script:
    - sed -i 's@'"$CI_PROJECT_DIR"'/@@g' coverage/coverage.json
  artifacts:
    when: always
    expire_in: 30 min
    paths:
      - coverage

sonarcloud-check:
  stage: analyze
  image:
    name: sonarsource/sonar-scanner-cli:latest
    entrypoint: [""]
  cache:
    key: "${CI_COMMIT_SHORT_SHA}"
    paths:
        - .sonar/cache
  dependencies:
    - ms2_test
  script:
    - sonar-scanner -X
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_REF_PROTECTED == "true"

Potential workarounds

  • Replacing the absolute paths in the coverage file using sed -inspired on the solution used with Github- does not solve the issue.

Related

Hi,

Welcome to the community!

Thanks for the thorough report! :slight_smile:

The warning you’re seeing happens when the paths in the coverage report don’t match the paths analysis is seeing.

You’ve set sonar.sources to app/ so presumably the path analysis sees is models/product_target.rb. That doesn’t match the path in the coverage report: app/models/product_target.rb.

Would it be possible to generate your coverage report from inside app? Then you should get matching paths. Otherwise, you may need to edit the report to strip out those leading app/ directories in the report paths.

 
HTH,
Ann

Hi Ann, thanks for the support.

I’ve removed the leading app/ from the report paths. Still getting the same warning:

11:48:57.214 DEBUG: 'app/models/product_target.rb' generated metadata with charset 'UTF-8'
...
11:48:51.237 DEBUG: 'app/models/product_target.rb' indexed with language 'ruby'
...
11:49:16.422 WARN: File 'models/product_target.rb' is present in coverage report but cannot be found in filesystem

with analysis reporting 0% coverage.

Could you please elaborate on the first alternative --to generate the coverage report from inside app?

Best,
Danilo

Problem solved =)

I’ve changed the sonar.sources to the project root folder (+lots of exclusions) and removed the path replacement command to get absolute paths in the coverage report.

Thanks & regards
Danilo

1 Like