No coverage in AzureDevops with .NET5

Hello,

I couldn’t find anything that fits my issue so I’ll be posting instead.

We’re on a commercial subscription of sonarcloud, project stored and pipeline running though azure devops with scannerMode on msbuild, we’re using the sonarcloud for azuredevops extension. The project is a .NET 5.0 application with coverlet.msbuild & opencover for coverage.

The pipeline use a template based on an other project that works perfectly and already have coverage and so on for several months.

Here are the pipeline tasks :

  steps:
    - checkout: self
      submodules: recursive  

    - task: UseDotNet@2
      displayName: 'Use .Net Core sdk 3.1.x'
      inputs:
        version: 3.1.x
    - task: UseDotNet@2
      displayName: 'Use .Net sdk 5.0.x'
      inputs:
        version: 5.0.x
    - task: NuGetToolInstaller@1
      displayName: 'Use NuGet 5.4.0'
      inputs:
        versionSpec: 5.4.0
        checkLatest: true
    - task: SonarCloudPrepare@1
      displayName: 'Prepare analysis on SonarCloud'
      inputs:
        scannerMode: 'MSBuild'
        SonarCloud: 'sonarcloud'
        organization: *****
        projectKey: *****
        projectName: *****
        extraProperties: |
         
sonar.cs.opencover.reportsPaths=$(Agent.BuildDirectory)/results_sonar/coverage.opencover.xml
          sonar.cpd.exclusions=**/Program.cs,**/Startup.cs,**/*.pem
          sonar.exclusions=**/Program.cs,**/Startup.cs,**/*.pem
    - task: DotNetCoreCLI@2
      displayName: 'dotnet restore'
      inputs:
        command: restore
        projects: '$(Build.SourcesDirectory)/path_to_sln'
        verbosityRestore: 'quiet'
    - task: DotNetCoreCLI@2
      displayName: 'dotnet build'
      inputs:
        projects: '$(Build.SourcesDirectory)/path_to_sln'
        arguments: '--no-restore -v q'
    - script: 'dotnet test $(Build.SourcesDirectory)/path_to_sln -v m --no-build -l trx -r $(Agent.BuildDirectory)/results_sonar /p:CollectCoverage=true /p:Exclude=''[xunit.*]*'' /p:CoverletOutputFormat=''json%2copencover'' /p:CoverletOutput=$(Agent.BuildDirectory)/results_sonar/coverage /p:MergeWith=''$(Agent.BuildDirectory)/results_sonar/coverage.json'''
      displayName: dotnet test
    - task: SonarCloudAnalyze@1
      displayName: 'Run Code Analysis'
    - task: SonarCloudPublish@1
      displayName: 'Publish Quality Gate Result'
      continueOnError: true
    - task: Docker@2
      displayName: Build and push an image to container registry
      condition: ne('${{ variables['Build.Reason'] }}', 'PullRequest')
      inputs:
        command: buildAndPush
        repository: '${{ parameters.imageRepository }}'
        dockerfile: '$(Build.SourcesDirectory)/path_to_Dockerfile'
        containerRegistry: '${{ parameters.containerRegistry }}'
        tags: '${{ parameters.buildNumber }}'
        buildContext: src
    - task: mspremier.PostBuildCleanup.PostBuildCleanup-task.PostBuildCleanup@3
      displayName: 'Clean Agent Directories'

coverlet.msbuild and opencover assemblies are included within the test projects & their names are formatted like so “ProjectName.UnitTests”.
However when running the pipeline, sonarcloud does not seem to see any code, the coverage is left blank, and issues & code smells that should be thrown dont appear.
We can see this warning on the project page on sonarcloud :

Your project contains only TEST-code for language C# and no MAIN-code for any language, so only TEST-code related results are imported. Many of our rules (e.g. vulnerabilities) are raised only on MAIN-code. Read more about how the SonarScanner for .NET detects test projects: https://github.com/SonarSource/sonar-scanner-msbuild/wiki/Analysis-of-product-projects-vs.-test-projects

The ‘dotnet test’ task show coverage by project, and coverage.opencover.xml/coverage.json are written where they should be, so the issue does not seem to be here.

We can see this on the “Run Code Analysis” task :

15:50:03.515 INFO: Coverage Report Statistics: 29 files, 0 main files, 0 main files with coverage, 29 test files, 0 project excluded files, 0 other language files.
15:50:03.515 WARN: The Code Coverage report doesn't contain any coverage data for the included files. Troubleshooting guide: https://community.sonarsource.com/t/37151

When using verbose=true, it seems that for each project, the “sonar.sources” param is always empty, all files are appened in the “sonar.tests” param, when they have no “test” in their filepath.
This behavior only happens with this project, we do not see this on our long lived project this is based on. Or any other new project using the same pipeline template.

I’ve followed and exausted all tutorials and topics I could find, hope someone can help.

Thanks in advance !

UP :
Here is what I see on sonarcloud page for my main branch


Keeps telling me “0 lines of code” although in the “Code” tab I can see my project fine

Hey there.

This is the first key thing to solve – if all your code is being categorized as test code, the vast majority of issues won’t be raised (and no coverage will be reported on those files).

While in the past, having Test in the project name was the definitive way that projects would get categorized as main/test code, that is no longer the case.

I would suggest taking a look at this documentation on how projects get categorized as one or the other (a rogue dependency or wrong ProjectTypeGuid can trigger it)

The documentation also offers some pointers for understanding why a project was categorized in a particular way.

I would also say that more often than not, it’s having a unit test dependency in your product projects that isn’t supposed to be there that results in the miscategorization.

Hi again,

I already saw this tutorial with no success whatsoever, seems I add trouble identifying the source of the problem.
So I decided to go back from square one.

There was a small confusion on my side, Sonar categorises project as product or test in the build task, so far from where I was looking. If this can help others, just enable verbosity in your build task and you’ll see things like this :

SonarCategoriseProject:
  Sonar: (****.csproj) Categorizing project as test or product code...

This is what helped me identify the issue, there was one of our toolbox that referenced the Moq library.

Anyway, things are working great now, thanks for the help !

Have a great day.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.