Main branch has no lines of code - C# AzureDevops .NET 5

Template for a good bug report, formatted with Markdown:

  • SonarCloud, AzureDevops
  • We are using .NET 5
  • We are building on Release

BackgroudnTaskID:
Runned manually on main branch: AXbxxSUgE7LNxveghNOt
Runned automatically on PR against main branch: AXbxyUXIOqas3cnA-fGk

This is the configuration on azure pipeline:

- task: SonarSource.sonarcloud.*********.SonarCloudPrepare@1
  displayName: 'Prepare analysis on SonarCloud'
  inputs:
    SonarCloud: SonarCloud
    organization: '******'
    projectKey: '******'
    projectName: '******'
    scannerMode: CLI
    configFile: sonar-project.properties

- task: SonarSource.sonarcloud.*********.SonarCloudAnalyze@1
  displayName: 'Run Code Analysis'
  condition: always()
  continueOnError: true

- task: SonarSource.sonarcloud.*********.SonarCloudPublish@1
  displayName: 'Publish Quality Gate Result'
  condition: always()
  continueOnError: true

And this is sonar config file:

# Project identification
sonar.projectKey=********
sonar.projectVersion=1.0
sonar.projectName=********

# Info required for Sonar
sonar.sources=.
sonar.language=cs
sonar.sourceEncoding=UTF-8

sonar.exclusions=**/obj/**,**/bin/**,**/*.dll
sonar.flex.cobertura.reportPaths=TestResults/**/coverage.cobertura.xml
sonar.cs.vstest.reportsPaths=TestResults/**/*.trx

Issues:
On main branch it states that “The main branch of this project is empty.”
On PRs, it does not find any issue/duplication/smell, which seems wierd.

Hi @Juan_Carrey1. Full disclaimer, I am currently working to set up SonarQube and then integrate it with Azure DevOps, but have not done so yet, so I do not have the experience of getting this flow working.

When I set up an empty YAML Pipeline in Azure DevOps and search for the SonarQube Prepare task, these are the options I am presented with:

This configuration results in:

- task: SonarQubePrepare@4
  inputs:
    SonarQube: 'SonarQube Coverage Test'
    scannerMode: 'MSBuild'
    projectKey: 'MyProjectKey'
    projectName: 'MyProjectName'
    projectVersion: '1.0'
    extraProperties: 'MyCustomProperty=MyCustomValue'

The task version is 4.17.0 (More Information page):
image

So I think your scannerMode property should be set to MSBuild to run the SonarScanner for AzureDevOps. I am guessing when run in MSBuild mode, it is a wrapper around the SonarScanner for .NET. I bring that up to point out that SonarScanner for .NET does not have any documentation around a sonar-project.properties file, so I don’t think .NET projects use this file.

Update: yeah, @ganncamp just confirmed in another thread that sonar-project.properties are not used in .NET analysis:

2 Likes

Thank you, I will give it a try.

Edit:

I was able to make it work, by adding NET5 and NETCore2.0 SDKs so sonarcloud could use 2.0 for MSBuild scanner while at the same time building the project with net5

- task: UseDotNet@2
  displayName: 'Use .NET Core 2.0 SDK (SonarCloud)'
  inputs:
    packageType: sdk
    version: 2.x
    installationPath: $(Agent.ToolsDirectory)/dotnet
  
- task: UseDotNet@2
  displayName: 'Use .NET 5.0 sdk'
  inputs:
    packageType: sdk
    version: 5.0.x
    installationPath: $(Agent.ToolsDirectory)/dotnet


- task: SonarSource.sonarcloud.******.SonarCloudPrepare@1
  displayName: 'Prepare analysis on SonarCloud'
  continueOnError: true
  inputs:
    SonarCloud: SonarCloud
    organization: agreeable
    projectKey: '********************'
    projectName: '********************'
    extraProperties: |
      sonar.exclusions=**/obj/**,**/bin/**,**/*.dll
      sonar.flex.cobertura.reportPaths=TestResults/**/coverage.cobertura.xml
      sonar.cs.vstest.reportsPaths=TestResults/**/*.trx

It seems to be adding code smells back to PRs :slight_smile: