A few extra steps are needed for SonarCloud to analyze your code coverage

Hey,

I need some help with adjusting our build pipeline for our C# .NET 8 project. I need some rubber-duck debugging, because I’m not sure what I need to do to fix this :slight_smile:

We are using Azure Devops build pipelines and we are gradually moving over to linux (ubuntu-latest) for all the build steps. For this we are trying to move over to the dotnet-coverage

We are still using the SonarCloudPrepare@1 and SonarCloudAnalyze@1 tasks. The prepare tasks looks like this:

    - task: SonarCloudPrepare@1
      inputs:
        SonarCloud: "xxx"
        organization: "xxx"
        scannerMode: "MSBuild"
        projectKey: "xxx"
        projectVersion: "$(Version.MajorMinor)"
        extraProperties: |
          sonar.analysis.buildNumber=$(Build.BuildId)
          sonar.analysis.pipeline=$(Build.BuildId)
          sonar.analysis.sha1=$(Build.SourceVersion)
          sonar.analysis.repository=$(Build.Repository.ID)
          sonar.cs.vstest.reportsPaths=**/TestResults/*.trx
          sonar.cs.vscoveragexml.reportsPaths=**/coverage.xml

(there are probably some unnecessary values there that we can remove, we are using sonarcloud since the project was .NET Framework 4.7)

The section where we run our unit tests and create the code coverage looks like this:

  - script: |
      dotnet tool install --global dotnet-coverage
      dotnet-coverage collect "dotnet test $(Build.SourcesDirectory)/src/xxx/ --configuration Release --nologo --no-build --no-restore --logger trx" -f xml -o "coverage.xml"

The test results and code coverage are shown correctly in azure devops with these two steps:

  - task: PublishTestResults@2
    inputs:
      testResultsFormat: 'VSTest' 
      testResultsFiles: '**/TestResults/*.trx'
      searchFolder: '$(System.DefaultWorkingDirectory)'
      failTaskOnFailedTests: true

  - task: PublishCodeCoverageResults@2
    inputs:
      summaryFileLocation: $(Build.SourcesDirectory)/coverage.xml

The script tasks is generating the coverage.xml file and I’ve verified this. Also the sonarcloud analyze step is showing this in the logs:

INFO: Parsing the Visual Studio coverage XML report /home/vsts/work/1/./s/coverage.xml
INFO: Adding this code coverage report to the cache for later reuse: /home/vsts/work/1/./s/coverage.xml
INFO: Coverage Report Statistics: 2877 files, 2512 main files, 2512 main files with coverage, 365 test files, 0 project excluded files, 0 other language files.
INFO: Sensor C# Tests Coverage Report Import [csharp] (done) | time=14316ms
INFO: Sensor C# Unit Test Results Import [csharp]
INFO: Parsing the Visual Studio Test Results file '/home/vsts/work/1/./s/src/xxx.Tests/TestResults/_fv-az630-59_2024-05-14_06_28_23.trx'.
about 15 other trx files are being found here
INFO: Sensor C# Unit Test Results Import [csharp] (done) | time=879ms

So, what can be a reason for sonarcloud to keep saying this on the PR Summary?

Coverage
A few extra steps are needed for SonarCloud to analyze your code coverage

[Setup coverage analysis](https://docs.sonarsource.com/sonarcloud/enriching/test-coverage/overview/)

While azure devops received everything in order and shows me:

Tests and coverage
99.9% passed
41.27% covered

Best regards,
Freddy

Hi Freddy,

Does your PR have code changes?

It was recently pointed out to me that what you’re seeing is what SonarCloud shows when

  • no coverage report was passed.
  • no coverable lines were changed.

That’s unfortunate, and I’ve raised it internally.

Perhaps it’s what you’re facing?

 
:duck:
Ann

Thank you for your reply. This is easily tested :slight_smile:

Hmm, I’ve added some minor code coverage to a class.
In sonarcloud I can see this on that specific file:

Lines - 45
Coverage - 81.8%
Duplications - 0.0%

But the code tab shows a nice for the file.

And the PR summary still says:

Coverage

A few extra steps are needed for SonarCloud to analyze your code coverage

[Setup coverage analysis](https://docs.sonarsource.com/sonarcloud/enriching/test-coverage/overview/)

As I see this, sonarcloud knows about the code coverage of the individual files but something is not allowing it to generate the summary information.

Might there be some other issue interfering with my setup? (the SonarCloudPrepare@1 task in combination with the modern dotnet commands?)

And maybe this might also be useful during the sonarcloud analyze step: (so we know it is processing these files)

INFO: SCM Publisher SCM provider for this project is: git
INFO: SCM Publisher 3 source files to be analyzed
INFO: SCM Publisher 3/3 source files have been analyzed (done) | time=293ms

Best regards,
Freddy

Hi Freddy,

Could I have a screenshot of the file (with coverage) changed in your PR, either from the Measures tab or the Code tab? E.G.

 
Thx,
Ann

Hi!

I’ve send you two screenshots as a personal message.

Best regards,
Freddy

Hi Freddy,

Thanks for the screenshots.

What I see changed in the screenshot of the file is the namespace declaration. So, yes that’s a code change, but it’s not coverable. :smile:

Further down the file, did you change lines that could actually be executed in a unit test?

 
Ann

1 Like

Aaah, I read again what you said before. :slight_smile:

So, to summarize:
When only adding unit-tests and no changes to the code that I’m testing will cause this issue.

To bypass this, I’ve renamed some variables and I suddenly see this:

Coverage

100%

No conditions set
on 7 New Lines to cover

**27.8%**Estimated after merge

Is there a bug that I can track @ganncamp ?
I suppose this is considered a bug and will be fixed when possible?

Best regards,
Freddy

Hi Freddy,

Unfortunately, the SonarCloud Jira project is private. And to be honest, I haven’t gotten them to create a ticket yet. But your experience will help with that.

 
Ann

1 Like

If you/they need additional information, I don’t mind helping out providing additional information.

Thank you for pointing me in the right direction!

Best regards,
Freddy

1 Like

Hi Freddy,

As a followup, there’s a ticket, but it’s in a private project so I won’t bother pointing you to it. Unfortunately, I don’t have a timeline on it, but hopefully it’ll be soon.

 
Thx,
Ann

1 Like

Hi, we have the same issue but with jest tests. We followed your instructions and we get the same message “A few extra steps are needed for SonarCloud to analyze your code coverage”. Any idea what could fix it? Having code changes and tests in a pull request didn’t help. We can’t seem to get the coverage even when running the scan on our main branch.

Hi @katerina,

Welcome to the community!

As stated above,

If you feel this isn’t applicable to your situation, please create a new thread with all your details.

 
Ann

Goodmorning @ganncamp,

Would it be possible to drop an update here when the issue is resolved?
We can work around it for the pull requests, but I notice that the main branches/non-PR builds are also suffering from this issue.
Our team is marked with some ‘failed’ badges :slight_smile:

Best regards,
Freddy

Hi Freddy,

I can’t make any promises, but I’ve added a request to the ticket.

 
Ann

1 Like

Hi Freddy

We have fixed this issue.

Thanks for the feedback.

2 Likes