Dotnet SonarScanner failing with "dotnet failed for sonarscanner end"

Our code repositories are hosted in Github, but we use TeamCity to build our code. I am in the process of setting up our SonarCloud uploads for all of our 22 repositories and I have succeeded in using dotnet sonarscanner to upload most of the repositories, but some are, due to legacy code, building a bit different, and I have trouble with the uploads to SonarCloud.

I run this command to start dotnet sonarscanner

C:\Program Files\dotnet\dotnet.exe sonarscanner begin /k:"STAR-Code_jobag" /o:"star-code" /d:sonar.token="8d730fb433f3ae3290c5a5926d144ea9a77f60a1" /d:sonar.host.url="[https://sonarcloud.io](https://sonarcloud.io/)" /d:sonar.branch.name="2024-1/master" /d:sonar.verbose="true" from D:\TC\A2\work\8db6c7b507676697\JobAg\JobAg

then I run these commands to build the solution:

RemoveFiles -Files $logFiles
    
Invoke-Expression "& $NugetExePath restore $MSBuildProjFile"

$msbArgs = "/nr:false /noconlog /nologo /m $(
     )/flp1:Verbosity=Detailed;LogFile=$logDebug /flp2:errorsonly;LogFile=$logError $(
     )/p:Configuration=$configuration /p:Platform=""Any CPU"" $(
     )$MSBuildProjFile";

RunMSBuild -Arguments "/t:clean,build $msbArgs" -WorkingDir $slnDir -latestInstalled $true

and finally, I run this command end sonarscanner

C:\Program Files\dotnet\dotnet.exe sonarscanner end /d:sonar.token="TOKEN" from D:\TC\A2\work\8db6c7b507676697\JobAg\JobAg

I then get this error, which doesn’t tell me much about what is going wrong:

[10:06:56]**[Step 1/1]* 2024-01-19 10:06:56,874 [ERROR] dotnet failed for sonarscanner end /d:sonar.token="TOKEN"*

[10:07:00]**[Step 1/1]* Error message: 2024-01-19 10:06:56,874 [ERROR] dotnet failed for sonarscanner end /d:sonar.token="TOKEN"*

The setup has worked for other projects, but here we get this error. There is no background task created in SonarCloud, so it does not seem like the end step has any success at all.

The sonar cloud project key, token and branch are retrieved from a configuration file, which uses the same paths as those used for the build. I have made sure that they use the same paths and that sonarscanner is used in the right directory.

Hi,

First, I hope you’ve already revoked that token. :smiley:

Can you share a verbose log?

Share the Scanner for .NET verbose logs

  • Add /d:"sonar.verbose=true" to the…
    • SonarScanner.MSBuild.exe or dotnet sonarscanner begin command to get more detailed logs
      • For example: SonarScanner.MSBuild.exe begin /k:"MyProject" /d:"sonar.verbose=true"
    • “SonarQubePrepare” or “SonarCloudPrepare” task’s extraProperties argument if you are using Azure DevOps
      • For example:
        - task: SonarCloudPrepare@1
            inputs:
              SonarCloud: 'sonarcloud'
              organization: 'foo'
              scannerMode: 'MSBuild'
              projectKey: 'foo_sonar-scanning-someconsoleapp'
              projectName: 'sonar-scanning-someconsoleapp'
              extraProperties: |
                sonar.verbose=true
        
  • The important logs are in the END step (i.e. SonarQubeAnalyze / SonarCloudAnalyze / “Run Code Analysis”)

Share the msbuild detailed logs

MsBuild.exe /t:Rebuild /v:d

or

dotnet build -v:d

 
Thx,
Ann

We are already running the command with /d:sonar.verbose=“true”, as can be seen in my post. It does not help give any useful information.

Adding /v:d to the build arguments does not make any difference.

Related to me accidentally putting the tokens into my post: I cannot see any options to revoke them, how do I do that?

Hi,

Great! Then can you provide the full log, please?

Click on your avatar, at the top-right → My Account → Security & you’ll find a list of your tokens with controls to revoke them.

 
HTH,
Ann

JobAg_PR_Build_1.0.0.1738.log (77.6 KB)

This is the full log file, but as far as I can see, it does not say anything relevant to the error not already included.

Hi,

Thanks for the log. There’s very little in it about the actual build. I guess that’s because of this part…?

It looks like you’re sending the build logging to another file. Can you either provide that file, or give us a log from a run without the log redirection? (The latter is preferable.)

 
Thx,
Ann

Sorry for the late response.

I managed to find the logfile with some help from a coworker.

Build.Debug.log (5.6 MB)

Hi,

Thanks for this. Nothing’s jumping out at me. Could we also have the error log? I suppose it’s Build.Error.log…?

 
Thx,
Ann

The Build.Error.log file is empty, which is why I didn’t include it. For everything else, information is logged properly. We successfully setup and used SonarScanner with 20+ other repositories using a similar setup, with the only difference, aside from being different repositories, being the command

Invoke-Expression “& $NugetExePath restore $MSBuildProjFile”

which is necessary for this build.
Could there be anything in that command that SonarScanner could have trouble with?

Hi,

Thanks for checking the error log.

To be honest, I’m out of my depth at this point. I’m going to flag this for the language experts.

 
Ann

Thank you, I don’t quite understand what is going on either. I have set this up for 21 other repositories without seeing an issue like this now, using the same setup.

I have attached the debug log from the newest attempt at running SonarScanner, just in case there should be anything new in it.
Build.Debug.log (5.7 MB)

1 Like

Hello @twoday-jongislason,

I looked into the CI logs in JobAg_PR_Build_1.0.0.1738.log. I’m missing the console output of the dotnet sonarscanner begin command. It should start about like this:

> dotnet sonarscanner begin /k:"<<project>>" /d:sonar.host.url="<<url>>"  /d:sonar.token="<<token>>" /d:"sonar.verbose=true"
SonarScanner for MSBuild 5.13
Using the .NET Core version of the Scanner for MSBuild
Default properties file was found at C:\Users\<<user>>\.dotnet\tools\.store\dotnet-sonarscanner\5.13.0\dotnet-sonarscanner\5.13.0\tools\net5.0\any\SonarQube.Analysis.xml
Loading analysis properties from C:\Users\<<user>>\.dotnet\tools\.store\dotnet-sonarscanner\5.13.0\dotnet-sonarscanner\5.13.0\tools\net5.0\any\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...
16:03:11.476  16:03:11.466  Loading analysis properties from C:\Users\<<user>>\.dotnet\tools\.store\dotnet-sonarscanner\5.13.0\dotnet-sonarscanner\5.13.0\tools\net5.0\any\SonarQube.Analysis.xml
16:03:11.476  16:03:11.476  sonar.verbose=true was specified - setting the log verbosity to 'Debug'
16:03:11.482  Updating build integration targets...
16:03:11.485  The file SonarQube.Integration.ImportBefore.targets is up to date at C:\Users\<<user>>\AppData\Local\Microsoft\MSBuild\4.0\Microsoft.Common.targets\ImportBefore
16:03:11.486  The file SonarQube.Integration.ImportBefore.targets is up to date at C:\Users\<<user>>\AppData\Local\Microsoft\MSBuild\10.0\Microsoft.Common.targets\ImportBefore
...

The output for the dotnet sonarscanner end command is missing as well. Can you configure your pipeline in a way that the console output is included in the log?

Best regards, Martin

We do log the console output, which is why we’re baffled by this error. We have set this up in the same way as all of our other repositories, which print the information that you’re asking for, but for this one repository, we get this error.

Compare to our Kontaktforloeb repository, where we use these commands. The only differnece is the Invoke-Expression command:

Write-Info “Starting Sonar Step”

Write-Info “Start Sonar Scanner”
$msbArgs = “sonarscanner begin /k:"$sonarcloudProjectkey” /o:"star-code" /d:sonar.token="$Env:SONAR_TOKEN" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.branch.name="$sonarcloudBranch" /d:sonar.verbose="true""
RunDotnet -Arguments $msbArgs -WorkingDir $slnDir

Write-Info “Build Solution”
RemoveFiles -Files $logFiles
$msbArgs = “build Star.Kontaktforloeb.sln -flp1:Verbosity=Detailed;LogFile=$logDebug -flp2:errorsonly;LogFile=$logError --configuration $configuration --nologo”;
RunDotnet -Arguments $msbArgs -WorkingDir $slnDir

Write-Info “End Sonar Scanner”
$msbArgs = “sonarscanner end /d:sonar.token="$Env:SONAR_TOKEN”"
RunDotnet -Arguments $msbArgs -WorkingDir $slnDir

where this is logged

[12:11:44]**[Step 2/2]* 2024-01-31 12:11:44,518 [INFO ] Running & .\Build.SonarCloud.ps1*

[12:11:44]**[Step 2/2]* 2024-01-31 12:11:44,538 [INFO ] Starting Sonar Step*

[12:11:44]**[Step 2/2]* 2024-01-31 12:11:44,539 [INFO ] Start Sonar Scanner*

[12:11:44]**[Step 2/2]* 2024-01-31 12:11:44,541 [INFO ] dotnet.exe found here: C:\Program Files\dotnet\dotnet.exe*

[12:11:44]**[Step 2/2]* 2024-01-31 12:11:44,542 [INFO ] Running C:\Program Files\dotnet\dotnet.exe sonarscanner begin /k:“STAR-Code_Kontaktforloeb” /o:“star-code” /d:sonar.token=“3271a7046842847437f0ce92bf58edf90d6b2077” /d:sonar.host.url=“https://sonarcloud.io” /d:sonar.branch.name=“2024-1/master” /d:sonar.verbose=“true” from D:\TC\A1\work\5a9888ea1f85aa5d\Source*

[12:11:47]**[Step 2/2]* 2024-01-31 12:11:47,446 [INFO ] Stdout: SonarScanner for MSBuild 6.0*

[12:11:47]**[Step 2/2]* Using the .NET Core version of the Scanner for MSBuild*

[12:11:47]**[Step 2/2]* Default properties file was found at C:\Users\sa.app.starbuild.dotnet\tools.store\dotnet-sonarscanner\6.0.0\dotnet-sonarscanner\6.0.0\tools\netcoreapp3.1\any\SonarQube.Analysis.xml*

[12:11:47]**[Step 2/2]* Loading analysis properties from C:\Users\sa.app.starbuild.dotnet\tools.store\dotnet-sonarscanner\6.0.0\dotnet-sonarscanner\6.0.0\tools\netcoreapp3.1\any\SonarQube.Analysis.xml*

[12:11:47]**[Step 2/2]* sonar.verbose=true was specified - setting the log verbosity to ‘Debug’*

irrelevant solution build logging

[12:13:05]**[Step 2/2]* 2024-01-31 12:13:05,054 [INFO ] End Sonar Scanner*

[12:13:05]**[Step 2/2]* 2024-01-31 12:13:05,057 [INFO ] dotnet.exe found here: C:\Program Files\dotnet\dotnet.exe*

[12:13:05]**[Step 2/2]* 2024-01-31 12:13:05,057 [INFO ] Running C:\Program Files\dotnet\dotnet.exe sonarscanner end /d:sonar.token=“3271a7046842847437f0ce92bf58edf90d6b2077” from D:\TC\A1\work\5a9888ea1f85aa5d\Source*

[12:15:18]**[Step 2/2]* 2024-01-31 12:15:18,927 [INFO ] Stdout: SonarScanner for MSBuild 6.0*

[12:15:18]**[Step 2/2]* Using the .NET Core version of the Scanner for MSBuild*

As you can see, this logs everything as it should, and uploads to SonarCloud, despite using the same setup.

That is strange. In the CI log you posted, I can only see

[INFO ] dotnet.exe found here: C:\Program Files\dotnet\dotnet.exe  
[05:47:06] :	 [Step 1/1] 2024-01-24 05:47:06,854 [INFO ] Running C:\Program Files\dotnet\dotnet.exe sonarscanner begin /k:"STAR-Code_jobag" /o:"star-code" /d:sonar.token="TOKEN" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.branch.name="2024-1/master" /d:sonar.verbose="true" from D:\TC\A1\work\8db6c7b507676697\JobAg\JobAg  
[05:47:12] :	 [Step 1/1] 2024-01-24 05:47:12,010 [INFO ] vswhere.exe found here: C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe  

while in the other log I can see the expected output

[12:11:44]**[Step 2/2]* 2024-01-31 12:11:44,541 [INFO ] dotnet.exe found here: C:\Program Files\dotnet\dotnet.exe*
[12:11:44]**[Step 2/2]* 2024-01-31 12:11:44,542 [INFO ] Running C:\Program Files\dotnet\dotnet.exe sonarscanner begin /k:“STAR-Code_Kontaktforloeb” /o:“star-code” /d:sonar.token=“TOKEN” /d:sonar.host.url=“https://sonarcloud.io” /d:sonar.branch.name=“2024-1/master” /d:sonar.verbose=“true” from D:\TC\A1\work\5a9888ea1f85aa5d\Source*

[12:11:47]**[Step 2/2]* 2024-01-31 12:11:47,446 [INFO ] Stdout: SonarScanner for MSBuild 6.0*

[12:11:47]**[Step 2/2]* Using the .NET Core version of the Scanner for MSBuild*

The second log shows the Stdout: entry with all the capture console output. This entry is missing in the first log.

From the build log you also sent, I can tell that the begin step was likely running because all our build integrations are there (e.g., Target "SonarCategoriseProject" is executed). There are also some rule violations from our analyzer in the logs:

D:\..\CompanyController.cs(606,21): warning S2589: Change this expression, which always evaluates to the same result. [D:\..\JobAG.Presentation.csproj]

So, it looks like the begin step was executed successfully. Can you please check on your side why the console output is missing for the begin and end step?

The two different builds and SonarScanner calls are set up in the same way in our system. There is nothing different between the two that should make it so that SonarScanner doesn’t write the console output.

I will continue digging into the build code and see if I can find something together with my coworkers, but so far, the issue seems to be with SonarScanner not sending the output.

1 Like