Azure Devops C# dotnet analysis with CLI mode

  • Sonarqube version 9.3
  • Sonarscanner version 5.11.1
  • I want to use the CLI mode of the scanner, instead of MSBuild, because CLI mode allows me to use the sonar-project.properties file, which is easier to maintain through code.

This is my yaml file:

trigger:
  batch: true
  branches:
    include:
    - master

pool:
  vmImage: ubuntu-latest

variables:
  solution: 'SolutionXxx.sln'
  testSolution: 'UnitTests.sln' 
  sonarConnection: 'xxx-sonarQube'

steps:

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

- task: DotNetCoreCLI@2
  displayName: Restore nuget packages
  inputs:
    command: 'restore'
    projects: $(solution)
    verbosityRestore: 'Quiet'

- task: SonarQubePrepare@5
  displayName: Sonar prepare
  inputs:
    SonarQube: $(sonarConnection)
    scannerMode: CLI

- task: DotNetCoreCLI@2
  displayName: Build $(solution)
  inputs:
    command: build
    projects: $(solution)
    arguments: --no-restore

- script: |
    dotnet test $(testSolution) --no-build --no-restore -c debug --results-directory $(Agent.TempDirectory)/TestResults /p:CollectCoverage=true -p:CoverletOutput="$(Agent.TempDirectory)/TestResults/" -p:MergeWith="$(Agent.TempDirectory)/TestResults/coverage.json" -p:CoverletOutputFormat=\"json,opencover\"
  displayName: Unit testing solution

- task: DotNetCoreCLI@2
  displayName: Install ReportGenerator Global Tool
  inputs:
    command: custom
    custom: tool
    arguments: install dotnet-reportgenerator-globaltool -g

- script: echo "##vso[task.prependpath]$HOME/.dotnet/tools"
  displayName: 'Update PATH'

- script: |
    reportgenerator "-reports:$(Agent.TempDirectory)/TestResults/coverage.opencover.xml" "-targetdir:$(Agent.TempDirectory)/TestResults/Cobertura" "-reporttypes:Cobertura;HTMLInline;HTMLChart"
  displayName: Running report-generator (coverage)

- task: SonarQubeAnalyze@5
  displayName: Updating SonarQube

- task: SonarQubePublish@5
  displayName: Scanning SonarQube for results
  inputs:
    pollingTimeoutSec: '300'

But what happens is that on master only a handful of files are recognized and scanned. This is the log I get for C#:

INFO: Sensor C# [csharp]
WARN: Your project contains C# files which cannot be analyzed with the scanner you are using. To analyze C# or VB.NET, you must use the SonarScanner for .NET 5.x or higher, see https://redirect.sonarsource.com/doc/install-configure-scanner-msbuild.html
INFO: Sensor C# [csharp] (done) | time=2ms

Hi,

:warning: Make sure you upgrade to SonarQube v9.9 LTS soon, not only to benefit from our Best LTS Ever™, but because soon we will systematically ask users to upgrade when they ask questions about earlier versions of SonarQube, which are now considered unsupported. :smiley:

Sorry, but for C# analysis it just doesn’t work that way.

That said, you can add properties to your command line in your version-controlled pipeline file. The docs should help.

 
Ann