Main branch has no lines of code SonarQube

I’m new to sonarQube.
I did setup azure pipeline successfully with SonarQube. However I get main branch has no lines of code when I use azure devops scanner.
When I analyse the same repo with the SonarScanner the code lines appear in the web page.
I don’t know what I’m doing wrong:
Here is my azure pipeline

steps:

  • task: JavaToolInstaller@0

    inputs:

    versionSpec: ‘11’

    jdkArchitectureOption: ‘x64’

    jdkSourceOption: LocalDirectory

    jdkFile: “C:/builds/openjdk-11.0.2_windows-x64_bin.zip”

  • task: SonarQubePrepare@4

    inputs:

    SonarQube: ‘test-SonarQube’

    scannerMode: ‘CLI’

    configMode: ‘manual’

    cliProjectKey: ‘test-project’

    cliSources: ‘.’

  • task: SonarQubeAnalyze@4

  • task: SonarQubePublish@4

    inputs:

    pollingTimeoutSec: ‘300’

Any help is appreciated !!

Hello @Roua_Mok
thanks for reaching out to the community with your question.
The difference in behavior between your command line analysis and your Azure Devops ones are due to the branch names that are set for them

  • The SonarScanner for Azure DevOps extension detects the branch name and sets it for the analysis (as main or develop or whatever is used on Azure Devops side)
  • you are leaving the parameter empty when using the SonarScanner command line and ‘master’ is the default when not set
  • SonarQube also uses ‘master’ as default main branch for every project on both SonarQube server side.

Your projects are seemingly better equipped when you run the command line analysis because you are on the (wrong) default name for your project main branch on both scanner and server sides.

To overcome the default use of ‘master’ as main branch, there are several things you might do:

  • Use SonarQube Azure Devops integration to create your projects. This will automatically align your project main branch name with the one set on Azure Devops side.
  • Always set the sonar.branch.name analysis parameter when you use the command line scanner for your tests
  • On existing projects, make sure the main branch of each SonarQube project is aligned with the one on Azure Devops. And it is important that you do so. For existing projects with ‘master’ wrongly used as main branch name, this will require you to:
    • delete the existing branch created on SonarQube with this name , e.g. delete the branch called ‘develop’ or ‘main’. This will cause the loss of all this branch data (history and issues)
    • rename the main branch of this project in SonarQube from ‘master’ to the right one (e.g. ‘main’ or ‘develop’)
    • reanalyze this branch from Azure Devops, or manually with the branch name parameter set

Let me know how it goes.
Best regards
Sylvain

FYI a couple of points for anyone else who has a similar issue: there are a couple of issues with the above steps:

  1. To analyse .NET code, you must use ScannerMode : 'MSBuild'
  2. You must use MSBuild or dotnet to build your code between the SonarQubePrepare and SonarQubeAnalyze steps.
1 Like