.Net Sonar Scanner in Jenkins - You must install .NET to run this application

Hello,

I have a Project with a Frontand (Javascript) and a Backaned (C#). The normal SonarScanner runs without problems but gave me the Hint that I need the .Net Scanner to scan my C# Files.
I currently try to get the .Net Scanner of SonarQube running on my Jenkins but it feels like no matter what I do I can’t get it running.
My Jenkins is running on a Linux as a hint.

which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)

  • Sonarqube version: * Community Edition Version 9.5 (build 56709)
  • SonarQube Scanner for Jenkins Version 2.14
    → Nomal Scanner: 4.7.0, .Net Scanner 5.7.2 Net Fwk 4.6
    → Manually installed the newest version

what are you trying to achieve
Analys commits to check for errors in my code

what have you tried so far to achieve this:

  • Use the SonarScanner .Net Jenkins Plugin and the .Net Jenkins Plugin
    → Used the Jenkins “Freestyle” Project, selected “SonarScanner for MS - Beginn Analysis”, then a Build and then “SonarScanner for MS - EndAnalysis”
    Sadly this resulted in Jenkins trying to acces a .exe File whis is not possible in Linux and gave me this error:
/var/lib/jenkins/tools/hudson.plugins.sonar.MsBuildSQRunnerInstallation/SonarScannerMS/SonarScanner.MSBuild.exe: 1: MZ����@���: not found
/var/lib/jenkins/tools/hudson.plugins.sonar.MsBuildSQRunnerInstallation/SonarScannerMS/SonarScanner.MSBuild.exe: 1: ��O�Ta�L�8: not found
/var/lib/jenkins/tools/hudson.plugins.sonar.MsBuildSQRunnerInstallation/SonarScannerMS/SonarScanner.MSBuild.exe: 21: ����0l
>� �@ ���.rsrcTa�n@@.reloc�v@B�H5 I	*,~�
̈�0aos

rp�8%rp�(0s
o
rp�8%r-p�(1s
o
~e
o
~
o
�*0=�8%r5p�%r9p�(

+a�
	(+,*Xa�i2�*0�-r=ps
z-r]ps
zQ~s 

o!
a(: File name too long
/var/lib/jenkins/tools/hudson.plugins.sonar.MsBuildSQRunnerInstallation/SonarScannerMS/SonarScanner.MSBuild.exe: 23: Syntax error: newline unexpected (expecting ")")
ERROR: Execution of SonarScanner for MSBuild failed (exit code 2)
  • When I tried to link to the .dll file through a scripted pipline (in the same path as I got from the error above) I still get an error:
+ dotnet /SonarScanner.MSBuild.dll begin /k:myKey
Could not execute because the specified command or file was not found.
Possible reasons for this include:
  * You misspelled a built-in dotnet command.
  * You intended to execute a .NET program, but dotnet-/SonarScanner.MSBuild.dll does not exist.
  * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.

This seems to me that the path is wrong, but I don’t know which the right one whould be. So I tried something different

  • I installed the sonarscanner myself with the following command::
    dotnet tool install --global dotnet-sonarscanner

    → The installation work without problems. The file is here. When I try to run the scanner I get this error (PS: I treid it with both “dotnet sonarscanner begin” and “dotnet-sonarscanner begin”, same problem):

+ dotnet-sonarscanner begin /k:sap_fehlertracking-db_AYHNNuMNuXqET0luRRRS
You must install .NET to run this application.

App: /home/jenkins/.dotnet/tools/dotnet-sonarscanner
Architecture: x64
App host version: 6.0.7
.NET location: Not found

Learn about runtime installation:
https://aka.ms/dotnet/app-launch-failed

Download the .NET runtime:
https://aka.ms/dotnet-core-applaunch?missing_runtime=true&arch=x64&rid=debian.11-x64&apphost_version=6.0.7

Here is my jenkinsfile pipeline script (Catch block is not properly working, even tho I get an error it still succeeds, it’s still a WIP)

stage('SonarQube Analysis') {
      updateGitlabCommitStatus name: 'SonarQube Analysis', state: 'pending'
  
      nodejs(nodeJSInstallationName: 'nodejs') {
        sh 'node -v'
        withSonarQubeEnv() {
          env.PATH = "$PATH:/home/jenkins/.dotnet"
          env.PATH = "$PATH:/home/jenkins/.dotnet/tools"
          script {
            try {
              sh '''
                dotnet sonarscanner  begin /k:"MyKey"          
                dotnet build fehlertracking.sln
                dotnet sonarscanner  end        
              '''
              updateGitlabCommitStatus name: 'SonarQube Analysis', state: 'success'
            } catch (ex) {
              updateGitlabCommitStatus name: 'SonarQube Analysis', state: 'failed'
            }
          }
        }
      }
    }

If someone here could help me out what the problem is/ how I can fix it or get it running I whould really appreciate. The documentation on the page for Linux systems is sadly missing a lot of key infos (or I’m just to bad to read it)

Hey there.

Did you follow the tutorial in the UI for Jenkins + Linux + .NET Core when creating your project? It includes instructions such as managing the Global Tool Configuration


c

When I initially set the project up I chose “other” because of the JS files I have, so I never saw this screen.
Please give me a bit to checj it out

  • Under Install from GitHub select the corresponding .NET Core scanner required for you project.
    → Does this mean it only work with .NET Core 2.0, .NET Core 3.0 .NET 5? I think we installed .NET 6, this could be the problem

Okay new reply
The Scan finally worked, thanks for the help @Colin
I still have 2 questions if possible:

  1. def scannerHome = tool ‘SonarScanner for MSBuild’ → When I used ${scannerHome} it did not give back the proper path, for now I used the hard coded path to get it working. Any ideas how to fix this? → FIXED
  2. Now every project with c# code got checked, but my one project with js, html etc. did not get checked (.njsproj). What do I need to do that these files also get checked? Can I shomehow integrate it into my .sln file?

EDIT: Found out the first problem, I uses shdo Stuff where it apparently can’t use these variables

My pipeline now looks like this:

stage('SonarQube Analysis') {
      updateGitlabCommitStatus name: 'SonarQube Analysis', state: 'pending'
  
      nodejs(nodeJSInstallationName: 'nodejs') {
        sh 'node -v'
        def scannerHome = tool 'SonarScannerMS'
        withSonarQubeEnv() {
          env.PATH = "$PATH:/home/jenkins/.dotnet"
          env.PATH = "$PATH:/home/jenkins/.dotnet/tools"
          sh "dotnet ${scannerHome}/SonarScanner.MSBuild.dll begin /k:\"sap_fehlertracking-db_AYIBDL0ZccYnbt4oP4o9\""      
          sh "dotnet build fehlertracking.sln"
          sh "dotnet ${scannerHome}/SonarScanner.MSBuild.dll end"           
        }
      }

      updateGitlabCommitStatus name: 'SonarQube Analysis', state: 'success'
    }

Great work getting further along!

I’m not familiar with this project type – does it only contain frontend code, or also some C#?

It only contains .js, scss, .json etc. files, no C# Files.
For now I made a new Project, copy’d that empty project in my frontent project and added that .csproj file to the .sln file. With that it works but it feels weird having an empty program.cs file now in there. Is there a cleaner solution? Or whould the best solution be to split my project into “backend” and “frontend”?

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