Jenkins DotNet SonarScanner

  • Which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
    SonarQube 9.4 (build 54424)
    SonarQube Scanner for Jenkins - 2.14

  • What are you trying to achieve
    Unable to complete initial scanning of a Unity3D C# repository. Receiving errors attempting to execute the scanner

  • What have you tried so far to achieve this

  1. Tutorial for Linux + .NET Core which says to create a
node {
  stage('SCM') {
    checkout scm
  }
  stage('SonarQube Analysis') {
    def scannerHome = tool 'SonarScanner for MSBuild'
    withSonarQubeEnv() {
      bat "dotnet ${scannerHome}\\SonarScanner.MSBuild.dll begin /k:\"<PROJECT_KEY>\""
      bat "dotnet build"
      bat "dotnet ${scannerHome}\\SonarScanner.MSBuild.dll end"
    }
  }
}

I’m unable to use invoke a DLL, as a DLL file was not installed by the Jenkins plugin.

+ ls /var/jenkins/tools/hudson.plugins.sonar.MsBuildSQRunnerInstallation/SonarScanner_for_MSBuild
Microsoft.VisualStudio.Setup.Configuration.Interop.dll
Newtonsoft.Json.dll
SonarQube.Analysis.xml
SonarScanner.MSBuild.Common.dll
SonarScanner.MSBuild.PostProcessor.dll
SonarScanner.MSBuild.PreProcessor.dll
SonarScanner.MSBuild.Shim.dll
SonarScanner.MSBuild.TFS.dll
SonarScanner.MSBuild.TFSProcessor.exe
SonarScanner.MSBuild.TFSProcessor.exe.config
SonarScanner.MSBuild.Tasks.dll
SonarScanner.MSBuild.exe
SonarScanner.MSBuild.exe.config
System.Net.Http.dll
System.Runtime.InteropServices.RuntimeInformation.dll
System.Security.Cryptography.Algorithms.dll
System.Security.Cryptography.Encoding.dll
System.Security.Cryptography.Primitives.dll
System.Security.Cryptography.X509Certificates.dll
System.ValueTuple.dll
Targets
hostfxr.dll
hostpolicy.dll
sonar-scanner-4.6.2.2472

I receive the following error:

+ /usr/local/share/dotnet/dotnet /var/jenkins/tools/hudson.plugins.sonar.MsBuildSQRunnerInstallation/SonarScanner_for_MSBuildSonarScanner.MSBuild.dll begin /k:<PROJECT_KEY>
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-/var/jenkins/tools/hudson.plugins.sonar.MsBuildSQRunnerInstallation/SonarScanner_for_MSBuildSonarScanner.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.

Attempting to execute the exe results in the following error:

+ /var/jenkins/tools/hudson.plugins.sonar.MsBuildSQRunnerInstallation/SonarScanner_for_MSBuild/SonarScanner.MSBuild.exe begin /k:<PROJECT_KEY>
/var/jenkins/workspace/cgs-sonarqube_main@tmp/durable-4eaca1c9/script.sh: line 1: /var/jenkins/tools/hudson.plugins.sonar.MsBuildSQRunnerInstallation/SonarScanner_for_MSBuild/SonarScanner.MSBuild.exe: cannot execute binary file

Hi Joshua, welcome to the community :slight_smile:

It’s been a couple of weeks since you posted, are you still having an issue with getting this running? We don’t have a lot of experience analyzing Unity projects and I’d be really interested to hear about your experiences. If you are still having this issue let me know and I will try and offer some suggestions.

Thanks

Tom

@Tom_Howlett - Thanks for following up! I am still having issues getting this setup running. I can’t seem to figure out how to resolve the fact that the plugin installed an exe instead of a dll on a Mac.
I feel like there is a very simple step that I have either missed or done incorrectly, but I can’t seem to figure it out.

Hi Joshua

I think you may have a windows only version of the scanner installed. In the tutorial in step 3 it says:

Under Install from GitHub select the corresponding .NET Core scanner required for you project.

Depending on the version of .NET installed on the agent you need to select the correct scanner. I think you currently have the .NET Framework 4.6 Version of the scanner selected that is Windows only.

Hope that helps, let me know how you get on

Tom

@Tom_Howlett - After some discussion with our DevOps group, it looks like we had installed the incorrect SonarScanner.

Been doing a bit more work on trying to get this setup, and am running into a few more issues. Would appreciate any additional guidance.

We now have “SonarScanner for MSBuild - 5.5.3.43281 - .NET 5.0” installed. I am able to get further in the process.

It appears that Unity is currently on .NETFramework 4.7.1 for the .csproj files. I receive this error:
/usr/local/share/dotnet/sdk/5.0.201/Microsoft.Common.CurrentVersion.targets(1216,5): error MSB3644: The reference assemblies for .NETFramework,Version=v4.7.1 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks

We are doing some of our own digging to work out how to install these developer packs, but will take any guidance you have.

Thanks!

@Tom_Howlett - Here’s another attempt we’ve made.

Using SonarScanner .NET FWK 4.6 …

sh "<PATH_TO_MONO>/mono ${scannerHome}/SonarScanner.MSBuild.exe begin /k:\"<PROJECT_ID>\" /d:sonar.host.url=<HOST_URL> /d:sonar.login=<ACCESS_TOKEN>"
sh "<PATH_TO_MONO>/mono ${scannerHome}/SonarScanner.MSBuild.exe <PATH_TO_SLN> /t:Rebuild"
sh "<PATH_TO_MONO>/mono ${scannerHome}/SonarScanner.MSBuild.exe end"

Receive the following error:

WARNING: Please specify the command 'begin' or 'end' to indicate whether pre- or post-processing is required. These parameters will become mandatory in a later release.
Pre-processing started.
Preparing working directories...
13:49:49.859  13:49:49.812  Unrecognized command line argument: <PATH_TO_SLN>
13:49:49.859  13:49:49.847  Unrecognized command line argument: /t:Rebuild
13:49:49.859  Expecting at least the following command line argument:
- SonarQube/SonarCloud project key
The full path to a settings file can also be supplied. If it is not supplied, the exe will attempt to locate a default settings file in the same directory as the SonarQube Scanner for MSBuild.
Use '/?' or '/h' to see the help message.
13:49:49.86  Pre-processing failed. Exit code: 1

@Tom_Howlett - One step further. Realized I was using SonarScanner.MSBuild.exe and not the Mono msbuild. Dumb Mistake.

I am now onto dealing with Unity needing to handle the sln and csproj files. Will report back when I have determined a solution

1 Like