Tool dotnet sonarscanner complains of missing optional parameter /s

  • versions used: dotnet-sonarscanner 5.0.4
  • error observed
Failed to request and parse 'org.azurewebsites.net/api/qualityprofiles/search?project=gain-feedservice&organization=org': Value cannot be null. (Parameter 's')
  • steps to reproduce

After successful install of dotnet global tool sonarscanner, an attempt to run fails for lack of a settings file.

dotnet-sonarscanner begin /key:"gain-feedservice" /o:"org" /d:sonar.login="redacted"  /d:sonar.host.url="https://org.azurewebsites.net/"
SonarScanner for MSBuild 5.0.4
Using the .NET Core version of the Scanner for MSBuild
Pre-processing started.
Preparing working directories...
12:08:58.466  Updating build integration targets...
12:08:58.886  Fetching analysis configuration settings...
12:08:59.148  Failed to request and parse 'https://org/api/qualityprofiles/search?project=gain-feedservice&organization=org': Value cannot be null. (Parameter 's')
Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 's')
   at System.IO.StringReader..ctor(String s)
   at Newtonsoft.Json.Linq.JObject.Parse(String json, JsonLoadSettings settings)
   at Newtonsoft.Json.Linq.JObject.Parse(String json)
   at SonarScanner.MSBuild.PreProcessor.SonarWebService.<>c__DisplayClass6_0.<<TryGetQualityProfile>b__0>d.MoveNext()
   at SonarScanner.MSBuild.PreProcessor.SonarWebService.TryGetQualityProfile(String projectKey, String projectBranch, String organization, String language)
   at SonarScanner.MSBuild.PreProcessor.TeamBuildPreProcessor.FetchArgumentsAndRulesets(ISonarQubeServer server, ProcessedArgs args, TeamBuildSettings settings)
   at SonarScanner.MSBuild.PreProcessor.TeamBuildPreProcessor.DoExecute(ProcessedArgs localSettings)
   at SonarScanner.MSBuild.PreProcessor.TeamBuildPreProcessor.Execute(String[] args)
   at SonarScanner.MSBuild.BootstrapperClass.PreProcess()
   at SonarScanner.MSBuild.BootstrapperClass.Execute()
   at SonarScanner.MSBuild.Program.Execute(String[] args, ILogger logger)
   at SonarScanner.MSBuild.Program.Execute(String[] args)
   at SonarScanner.MSBuild.Program.Main(String[] args)
   at SonarScanner.MSBuild.Program.<Main>(String[] args)

Docs say, /s is OPTIONAL

D:\src\Feeds> dotnet sonarscanner -h
SonarScanner for MSBuild 5.0.4
Using the .NET Core version of the Scanner for MSBuild

Usage:

  SonarScanner.MSBuild [begin|end] /key:project_key [/name:project_name] [/version:project_version] [/d:sonar.key=value] [/s:settings_file]

    When executing the begin phase, at least the project key must be defined.
    Other properties can dynamically be defined with '/d:'. For example, '/d:sonar.verbose=true'.
    A settings file can be used to define properties. If no settings file path is given, the file SonarQube.Analysis.xml in the installation directory will be used.
    Only the token should be passed during the end phase, if it was used during the begin phase.
  • potential workaround: Create a settings file from scratch (never used SonarQube, just wanting to give it a quick spin).

Hey there.

If you’re using SonarQube, try leaving this out entirely.

This is only for SonarCloud.

Thank you, that was it. Maybe this should b clarified in the instructions…

Can you confirm which instructions you were following? I don’t see any reference to /o: in our SonarQube docs (https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-msbuild/)


The .NET Core version can also be used as a .NET Core Global Tool. After installing the Scanner as a global tool as described above it can be invoked as follows:

dotnet tool install --global dotnet-sonarscanner
dotnet sonarscanner begin /k:"project-key" /o:"<organization>" /d:sonar.login="<token>" 
dotnet build <path to solution.sln>
dotnet sonarscanner end /d:sonar.login="<token>" 

As you can see, there are other problems with these instructions, such as missing /d:sonar.host.url parameter. I was able to overcome these reading the docs. Also, I remember the token I generated had a project-key: prefix which also tripped me up.

SonarQube and SonarCloud are two different products – if you are using SonarQube, you should follow the SonarQube docs. :slight_smile:

I did at first (see https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-msbuild/):

dotnet sonarscanner begin /k:"project-key" 
dotnet build <path to solution.sln>
dotnet sonarscanner end 

When i got the error below, I thought that I needed the cloud version:

D:\src\Feeds> dotnet sonarscanner begin /k:"gain-feedservice"
SonarScanner for MSBuild 5.0.4
Using the .NET Core version of the Scanner for MSBuild
Pre-processing started.
Preparing working directories...
14:57:28.949  Updating build integration targets...
14:57:33.104  Failed to request and parse 'http://localhost:9000/api/server/version': No connection could be made because the target machine actively refused it. (localhost:9000)
14:57:33.107  No connection could be made because the target machine actively refused it. (localhost:9000)
14:57:33.11  Pre-processing failed. Exit code: 1

Are you telling me I am posting this in the wrong forum or that I followed the wrong instructions?

BTW, I found something else. The CLI help is completely wrong, too:

D:\src\Feeds> dotnet sonarscanner begin -h
SonarScanner for MSBuild 5.0.4
Using the .NET Core version of the Scanner for MSBuild

Usage:

  SonarScanner.MSBuild [begin|end] /key:project_key [/name:project_name] [/version:project_version] [/d:sonar.key=value] [/s:settings_file]

    When executing the begin phase, at least the project key must be defined.
    Other properties can dynamically be defined with '/d:'. For example, '/d:sonar.verbose=true'.
    A settings file can be used to define properties. If no settings file path is given, the file SonarQube.Analysis.xml in the installation directory will be used.
    Only the token should be passed during the end phase, if it was used during the begin phase.

There is mention of a “token” that appears nowhere in the usage. One needs to figure out first that sonar.key is the token.(why not /key?).

If you compare the proposed usage with the one that works,

dotnet-sonarscanner begin /key:"gain-feedservice" /d:sonar.login="redacted"  /d:sonar.host.url="https://org.azurewebsites.net/"

they are worlds apart.

Hello @Monte-Christo

From what I can tell, you found the solution to your problem?

sonar.key is not the token, it just a generic reference for Analysis Parameters, where key can be replaced by login, for example. I agree this is not clear enough from the “help” message, so I opened issue #968 to improve it.

/key is the SonarQube Project key, i.e. identifier. As mentioned on the docs page.

The token is the SonarQube token you generate (see Generating and Using Tokens) - sonar.login.

SonarScanner.MSBuild.exe begin /k:"SQ Project Name" /d:sonar.login="<TOKEN>"

... MSBuild.exe /t:"Restore,Rebuild" X.sln ....

SonarScanner.MSBuild.exe end /d:sonar.login=<TOKEN>

Thank you for your feedback.