PR annotation - Code Coverage always zero - dotnet core

We have the following commands in our build system to capture tests and code coverage (We use coverlet as a NuGet package)
We build on AWS Codebuild on Linux

dotnet sonarscanner begin /k:"${SONAR_PROJECT_KEY}" /n:"${SONAR_PROJECT_NAME}" /d:sonar.host.url="${SONAR_URL}" /o:"${SONAR_ORGANIZATION}" /d:sonar.token="${SONAR_TOKEN}" /d:sonar.cs.xunit.reportsPaths="${XUNIT_RESULTS_PATH}" /d:sonar.cs.opencover.reportsPaths="${OPENCOVER_CODE_COVERAGE_PATH}" /d:sonar.scm.exclusions.disabled=true /d:sonar.dotnet.excludeTestProjects=true /d:sonar.pullrequest.base="${SONAR_PULLREQUEST_BASE}" /d:sonar.pullrequest.key="${SONAR_PULLREQUEST_KEY}" /d:sonar.pullrequest.branch="${SONAR_PULLREQUEST_BRANCH}"

dotnet build ${SOLUTION_FILE} -p:Version=${VERSION} -c Release -v q --version-suffix ${VERSION_SUFFIX} 

dotnet test ${SOLUTION_FILE} --no-build -c Release --collect:"XPlat Code Coverage" --logger:"${TEST_LOGGER};LogFileName=TestResults.xml" --logger:"console" --logger:"trx;LogFileName=TestResults.trx" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover,cobertura -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Exclude=[${TEST_LOGGER}.*]* 

dotnet sonarscanner end /d:sonar.token="${SONAR_TOKEN}" 

We see coverage data collected from various files and post-merge; the % calculated is correct, but the PR annotations always show zero.

Is there an issue where multiple open cover files, one per project, are being generated and combined isn’t calculated correctly?

Hi,

Are the files/lines changed in your PRs coverable? PR analysis reporting is limited strictly to what changed in the PR. So if, e.g., your PR changes only comments, then the coverage reported on the PR is going to be -.

 
HTH,
Ann

The developers add code, standard development process. No team so far has told me that code coverage data appears on PR annotation.

However, when we look at SonarCloud the estimate is correct and when PR is merged the values show up correctly

Is there any way to debug ?

Hi,

Let’s narrow this down to a single PR:

  • are the lines changed in the PR code or non-code?
  • please provide the analysis log for the PR.

The analysis / scanner log is what’s output from the analysis command. Hopefully, the log you provide - redacted as necessary - will include that command as well.

This guide will help you find them.

 
Thx,
Ann

If anyone comes looking, the issue is related to the AWS Codebuild feature to cache the source.
LOCAL_SOURCE_CACHE should be disabled.

LOCAL_SOURCE_CACHE
Caches Git metadata for primary and secondary sources. 
After the cache is created, subsequent builds pull only the change between commits. T
his mode is a good choice for projects with a clean working directory and a source that is a large Git repository. 
If you choose this option and your project does not use a Git repository (GitHub, GitHub Enterprise, or Bitbucket), the option is ignored.
1 Like

Thanks for finding this, @HaroonSaid, I appreciate that you replied with this post! :smiley:
I’ve created a ticket to add this to our documentation.

1 Like