SonarScanner .Net supplying analysis parameters in .csproj

After spending a good few hours researching the topic on my own, I found some pointers that SonarQube.Analysis.xml might be what I’m looking for, however it didn’t seem to be picked up by the dotnet-sonarscanner globally installed tool (not surprising considering that official docs mention it ONLY in the context of the Standalone Executable).

After a bit more of the hopeless research I came across this thread that talked about a seemingly undocumented /s:settings_file argument (no, my bad, it’s only missing from the online docs, but appears in the help output of the dotnet-sonarscanner). So I added SonarQube.Analysis.xml to the project root and moved all key settings to it:

<?xml version="1.0" encoding="utf-8" ?>
<SonarQubeAnalysisProperties
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="http://www.sonarsource.com/msbuild/integration/2015/1">
  <Property Name="sonar.verbose">true</Property>
  <Property Name="sonar.host.url">https://sonarcloud.io</Property>
  <Property Name="sonar.projectKey">myorg_myproj</Property>
  <Property Name="sonar.organization">myorg</Property>
  <Property Name="sonar.projectName">MYPROJ</Property>
  <Property Name="sonar.exclusions">**/*.json, **/*.py, **/*.js, **/*.xml, **/*.html, **/*.yml, **/*.yaml</Property>
  <Property Name="sonar.cs.vscoveragexml.reportsPaths">dist/coverage.xml</Property>
</SonarQubeAnalysisProperties>

And just when I thought my project settings nightmare was over, this is what I see in the stdout after setting the /s:settings_file argument:

$ /root/.dotnet/tools/dotnet-sonarscanner "begin" "/s:SonarQube.Analysis.xml" "/d:sonar.token=..."
SonarScanner for MSBuild 6.2
Using the .NET Core version of the Scanner for MSBuild
Loading analysis properties from /data/work/.../myproject/SonarQube.Analysis.xml
sonar.verbose=true was specified - setting the log verbosity to 'Debug'
Pre-processing started.
Preparing working directories...
Using environment variables to determine the download directory...
07:33:02.81  07:33:02.808  A required argument is missing: /key:[SonarQube/SonarCloud project key]
07:33:02.81  07:33:02.81  Unable to find the analysis settings file '/data/work/.../myproject/.sonarqube/SonarQube.Analysis.xml'. Please fix the path to this settings file.
07:33:02.81  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.
07:33:02.81  Pre-processing failed. Exit code: 1

Loading analysis properties from /data/work/.../myproject/SonarQube.Analysis.xml
  • Ok, great, it seems to have picked up the verbose flag!
07:33:02.81  07:33:02.81  Unable to find the analysis settings file '/data/work/.../myproject/.sonarqube/SonarQube.Analysis.xml'. Please fix the path to this settings file.
  • Huh?

Is this a bug? Is it a feature? Please consider streamlining the project configuration. It should NOT be that difficult, especially for someone who comes with the previous experience of configuring SonarCloud in 10+ other projects. I understand that different ecosystems may have impact on how the scan is implemented, but the documentation should smooth out all the bumps.

2 Likes