Sonar not analyzing VB.NET C# file

  • ALM used: Azure DevOps
  • CI system: Azure DevOps
  • Languages of the repository: .Net framework 4.8, C#
  • Version: Developer Edition License

Issue: Analyze not running for C# files. Only CSS file are showing in lines count.

I am having an issue in running analysis on C# files with the below error in Sitefinity application. I tried inclusion of .cs files and adding scannerMode: ‘MSBuild’ in additional properties but both didn’t help.

WARN: Your project contains VB.NET 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

I am using Sonar Cloud extension for Azure DevOps where I don’t see option to update the sonar scanner version. Kindly advise where I have to update the scanner.


Prepare for Sonar Analysis YAML in Azure DevOps build pipeline:

steps:
- task: SonarSource.sonarcloud.14d9cde6-c1da-4d55-aa01-2965cd301255.SonarCloudPrepare@1
  displayName: 'Prepare analysis on SonarCloud'
  inputs:
    SonarCloud: 'Sonar Cloud'
    organization: 'kxxxxk'
    projectKey: 'KXXXXK_KXXXK'
    projectName: KxxxxxK
    extraProperties: |
     # Additional properties that will be passed to the scanner, 
     # Put one key=value per line, example:
     # sonar.exclusions=**/*.bin
     scannerMode: 'MSBuild'  --> 'tried adding but didnt help.'

Attaching Log file
Sonar_RunCodeAnalysis_Log.txt (41.0 KB)

Hi,

Your pipeline indicates that you’re running the prepare step. What comes after that? Because the error you’re seeing is not about scanner version, but about scanner flavor. Perhaps the docs will help.

 
Ann

Thanks for the reply. Here is the build pipeline yaml

pool:
  name: Azure Pipelines
  demands:
  - msbuild
  - visualstudio
  - vstest
  - java

variables:
  environment: 'Development'
  env: 'DEV'

steps:
- task: NuGetRestore@1
  displayName: 'NuGet restore **/*.sln'

- task: SonarSource.sonarcloud.14d9cde6-c1da-4d55-aa01-2965cd301255.SonarCloudPrepare@1
  displayName: 'Prepare analysis on SonarCloud'
  inputs:
    SonarCloud: 'Sonar Cloud'
    organization: 'kxxxp'
    projectKey: 'kxxxp_kxxxpcxx'
    projectName: kxxxpcxx
    extraProperties: |
     scannerMode: 'MSBuild'

- task: VSBuild@1
  displayName: 'Build solution **\*.sln'
  inputs:
    vsVersion: 16.0
    msbuildArgs: '/t:rebuild /p:Configuration=Release /p:RunOctopack=true  /p:OctoPackProjectName=kxxxpcxx_$(environment) /p:OctoPackPublishPackageToFileShare=D:\TFS\CI\packages\kxxxpcxx\$(env)'
    clean: true
    restoreNugetPackages: true

- task: VSTest@2
  displayName: 'VsTest - testAssemblies'
  inputs:
    testAssemblyVer2: |
     **\*test*.dll
     !**\obj\**
    vsTestVersion: 16.0
    runTestsInIsolation: true
    codeCoverageEnabled: true
    diagnosticsEnabled: True
  continueOnError: true

- task: PublishTestResults@1
  displayName: 'Publish Test Results **/TEST-*.xml'
  inputs:
    testRunner: NUnit

- task: SonarSource.sonarcloud.ce096e50-6155-4de8-8800-4221aaeed4a1.SonarCloudAnalyze@1
  displayName: 'Run Code Analysis'

- task: SonarSource.sonarcloud.38b27399-a642-40af-bb7d-9971f69712e8.SonarCloudPublish@1
  displayName: 'Publish Quality Gate Result'

- task: octopusdeploy.octopus-deploy-build-release-tasks.octopus-push.OctopusPush@4
  displayName: 'Push Packages to Octopus Cloud'
  inputs:
    OctoConnectedServiceName: 'Octopus Cloud'
    Space: 'Spaces-2'
    Package: |
     D:\TFS\CI\packages\kxxxpcxx\$(env)\kxxxpcxx_$(environment).nupkg
    Replace: true
  condition: ne(variables['Build.Reason'], 'PullRequest')


Prepare step for reference:

Hi,

One more thing: could you provide the full job log, please?

 
Ann

Hello Ann

Sure. Please find it here. Thanks in Advance
Build Log.txt (3.1 MB)

Hi,

Thanks for the log. I can’t believe I didn’t remember earlier, but one common cause of C# code being ignored is that it gets characterized as test code. And sure enough, here it is in your log:

2023-07-20T14:12:37.6386031Z   Sonar: (kxxxxx.Services.csproj) Categorizing project as test or product code...
2023-07-20T14:12:37.6961707Z   Sonar: (kxxxxx.Services.csproj) project is evaluated as a test project based on the 'Moq' reference.
2023-07-20T14:12:37.6962528Z   Sonar: (kxxxxx.Services.csproj) categorized as TEST project (test code).

It happens over and over again, once for each project.

The docs will help you fix it.

 
Ann

2 Likes

Thanks Ann. It worked for us. Thanks for the quick support.

Thanks Again
Nag

1 Like

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