Code Coverage Using Coverlet for .NET core applications but Project version not displaying on Sonar

echo off
REM Set Environment Variables
set SONAR_HOST_URL=http://192.168.2.184:9011
set SONAR_LOGIN=xxxxxxx5ddsde20dxxxx9c542
set PROJECT_DIR=C:\Demo2\repository\demo_api\demo_api
set COVERAGE_FILE=%PROJECT_DIR%\XUnitTest\coverage.xml

REM Ensure BUILD_NUMBER exists
if “%BUILD_NUMBER%”==“” set BUILD_NUMBER=%RANDOM%
set BUILD_VERSION=1.0.%BUILD_NUMBER%
echo Build Version: %BUILD_VERSION%

REM Run Code Coverage with Coverlet
coverlet “%PROJECT_DIR%\XUnitTest\bin\Debug\net6.0\XUnitTest.dll” ^
–target “dotnet” ^
–targetargs “test %PROJECT_DIR%\XUnitTest --no-build” ^
-f “opencover” ^
-o “%COVERAGE_FILE%”

REM Start SonarScanner analysis
dotnet sonarscanner begin ^
/k:“cni_sonarqube” ^
/d:sonar.cs.opencover.reportsPaths=“%COVERAGE_FILE%” ^
/d:sonar.host.url=“%SONAR_HOST_URL%” ^
/d:sonar.login=“%SONAR_LOGIN%” ^
/d:sonar.sourceEncoding=“UTF-8” ^
/d:sonar.projectVersion=“%BUILD_VERSION%”

REM Add .NET tools to PATH
set PATH=%PATH%;C:\Users\Administrator.dotnet\tools

REM Build the project
dotnet build --no-incremental

REM Complete SonarScanner analysis
dotnet sonarscanner end /d:sonar.login=“%SONAR_LOGIN%”

echo Build and SonarQube scan completed.

All these steps are implemeted in jenkins free-style project.

This step is properly creating results for me including code -coverage and other security related issues. Problem is that Project version on sonar-scanner is not showing, I tried everything which I can from past 1 week, but not getting results.

Where are you expecting it to show up, and what do you see instead?

If sonar.projectVersion is provided, you should see the value in the Activity tab of your project.

And in the main project overview:

Hi Colin, Me and Ammar are colleagues. The problem that we are facing is that when we are trying to get code coverage in dot net core application with the tools Ammar shard it shows code coverage but with the above command it doesn’t detect New Code and we are not able to see Code Coverage in New Code as the version got disturbed with the commands Ammar shared above.

Please suggest how to overcome

Hey there.

I’m having a hard time parsing the details you’re sharing. Maybe some screenshots would help demonstrate the issue?

Hi Colin, can you guide us how can we achieve code coverage for Dot Net Project. I mean if you can share some commands to run on Jenkins, it will be a great help for us. Actually, we are really struggling to get Code Coverage for “New Code”. Based on your suggestions, we can try from scratch.

Hi Colin,

We are unable to retrieve the build version corresponding to the Sonar scan of our .NET application on the SonarQube dashboard. The script we are using was previously shared by Ammar Suhail.

I am attaching a screenshot of the Sonar scan results for reference. Could you please suggest a way to resolve this version issue

Here are the details of the issue:

  1. When using the mentioned script and running the job from Jenkins, code coverage is displayed, but the build number does not update according to the job.
  2. The build number appears correctly only when we use the SonarScanner for MSBuild - Begin Analysis plugin, but in this case, the code coverage is not reflected.

Could you please suggest a solution to resolve this issue? Also, if there is an alternative or recommended approach for setting up a .NET project with SonarQube, please guide us.

Hi Colin, Any suggestion or input?

that is the problem , even after passing here, this value is not getting on my Terminal,