Search code examples
xamarinmsbuildmonosonarqube-msbuild-runner

SonarQube Scanner for MSBuild on macOS using mono


I'm trying to use SonarQube Scanner for MSBuild on a Xamarin project on macOS using mono. As MSBuild 15.0 is now shipped with Xamarin, I figured this could actually work.

I can successfully run the scanner's "begin" using mono like so:

mono sonar-scanner-msbuild-2/SonarQube.Scanner.MSBuild.exe begin /k:"KEY"

The command creates the following files:

.sonarqube/conf/SonarQubeAnalysisConfig.xml
.sonarqube/conf/SonarQubeRoslyn-cs.ruleset
.sonarqube/conf/cs/SonarLint.xml

Before running MSBuild, I've added the following import to the csproj files of my solution:

<Import Project="/Users/someuser/.local/share/Microsoft/MSBuild/14.0/Microsoft.Common.targets/ImportBefore/SonarQube.Integration.ImportBefore.targets" />

I'm running MSBuild using:

msbuild /t:Rebuild

Now the build starts correctly but eventually terminates with two errors:

"/Users/someuser/Project/Project.sln" (Rebuild target) (1) ->
"/Users/someuser/Project/Project.UI.iOS/Project.UI.iOS.csproj" (Rebuild target) (2) ->
"/Users/someuser/Project/Project.Core/Project.Core.csproj" (default target) (3:3) ->
(CoreCompile target) ->
  CSC : error CS2001: Source file `/additionalfile:/Users/someuser/Project/.sonarqube/conf/cs/SonarLint.xml' could not be found [/Users/someuser/Project/Project.Core/Project.Core.csproj]
  CSC : error CS2001: Source file `/additionalfile:/Users/someuser/Project/.sonarqube/conf/Project.Core_AnyCPU_Debug_1267/ProjectOutFolderPath.txt' could not be found [/Users/someuser/Project/Project.Core/Project.Core.csproj]

Both files do exist in the filesystem, the first file was created in the scanner's begin invocation and the second file was created during the MSBuild execution.

Why would MSBuild not be able to find / access these two files? Is there anything that can be done about it?


Solution

  • After checking with the folks taking care of MSBuild, the solution has been provided in this Github issue thread:

    To quote radical's comment from the Github issue:

    You could try building with csc by passing /p:CscToolExe=csc.exe /p:CscToolPath=/Library/Frameworks/Mono.framework/Versions/4.8.0/lib/mono/msbuild/15.0/bin/Roslyn/ to msbuild

    Unfortunately, even though MSBuild works with the above adjustments, SonarQube fails afterwards. I will follow up with the SonarQube people to see whether this can be worked around.