SonarCloud new code coverage NOT accurate

There seems to be a bug on “New Code” Coverage, were all our PRs are failing Sonar Cloud checks for Coverage, it seems to be always picking some files in our repo that were not part of the PR and since those files dont have enough code coverage, for some reason it is failing the PR

Template for a good new topic, formatted with Markdown:

  • ALM used: GitHub

  • Steps to reproduce:
    This is a private repo, but basically any PR created on GitHub has this error even if the PR doesnt touch the file from the error message below

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

Failed conditions
 76.8% Coverage on New Code (required ≥ 80%)

The template post lays out information that would be really helpful in answering a question like this.

  • ALM used (GitHub, Bitbucket Cloud, Azure DevOps)
  • CI system used (Bitbucket Cloud, Azure DevOps, Travis CI, Circle CI…)

Can you provide some more details about your setup? Any analysis warnings?

thanks, updated!

Thanks. Sometimes this happens if a shallow clone is performed. Is your fetch-depth set to 0? Feel free to share your GitHub Actions YML.

yeah I see fetch-depth set to 0:

name: Sonar Scan

env:
  NX_CLOUD_ACCESS_TOKEN: ${{ secrets.NX_CLOUD_ACCESS_TOKEN }}

on:
  workflow_call:
    secrets:
      SONAR_TOKEN:
        required: true
      PERSONAL_GITHUB_TOKEN:
        required: true

jobs:
  sonar:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          persist-credentials: false
          ref: ${{ github.event.pull_request.head.sha }}
          fetch-depth: 0

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'yarn'

      - run: yarn
      - run: yarn nx run-many -t test --all

      - name: Sonar Scan
        uses: sonarsource/sonarcloud-github-action@master
        env:
          GITHUB_TOKEN: ${{ secrets.PERSONAL_GITHUB_TOKEN }}
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
          usingProperties: true