Code analysis randomly fails on Azure Devops

  • SonarQube 8.0
  • SonarScanner for MSBuild 4.7.1
  • Prepare Analysis Configuration : 4.8.0
  • MSbuild: Microsoft ® Build Engine version 15.9.20+g88f5fadfbe for .NET Core

Hi, we’re tyring to build .Net Core projects on Azure Devops and have had this problem for some time also in previous versions. During the code analysis step randomly fails reporting log below.

When running the same build definition using the same version of the source code several times, randomly one or more of the projects fail code analysis. In the next build some other might fail. So, a build that succeeds this time, might fail next time and succeeds later succeeds.
Thus none of the mentioned possible causes seem to really be the cause.

The SonarQube MSBuild integration failed: SonarQube was unable to collect the required information about your projects.
Possible causes:
 1. The project has not been built - the project must be built in between the begin and end steps
 2. An unsupported version of MSBuild has been used to build the project. Currently MSBuild 14.0.25420.1 and higher are supported.
 3. The begin, build and end steps have not all been launched from the same folder
 4. None of the analyzed projects have a valid ProjectGuid and you have not used a solution (.sln)
11:59:11.199  Post-processing failed. Exit code: 1

Any help on this?

Are you using Microsoft-hosted build machines, or your own?
If you are using your own, do you have more than one build agent running on the machine?

We’re using our own buildmachine. Indeed running several build agents in parallel.

If you have multiple build agents installed on the same machine and those agents are running analysis builds at the same then it’s possible for the analysis build being performed by Agent 1 to interfere with the analysis performed by Agent 2.

This thread is also about a scenario with multiple build agents on the same machine and contains more information about what happens at each stage.

I think the following sequence of events would give the warnings you are seeing:
(1) Agent 1 starts Analysis Build A -> installs the shared Sonar targets to ImportBefore
(2) Agent 1 runs MSBuild -> picks up shared targets
(3) Agent 2 starts Analysis Build B -> sees shared targets exists so doesn’t copy them
(4) Agent 1 runs the analysis end step -> deletes the shared targets in ImportBefore
(5) Agent 2 runs MSBuild -> shared targets do not exist -> analysis does not happen
(6) Agent 2 runs the analysis end step -> warning message

A possible workaround is as follows:
(a) make a copy of the SonarQube.Integration.ImportBefore.targets file with a different name e.g. Workaround.SonarQube.Integration.ImportBefore.targets
(b) copy the workaround targets file into the appropriate ImportBefore folder(s) for the versions of the MSBuild being used on your build machine
e.g. for MSBuild 15: %localappdata%\Microsoft\MSBuild\15.0\Microsoft.Common.targets\ImportBefore
for MSBuild 16+ %localappdata%\Microsoft\MSBuild\current\Microsoft.Common.targets\ImportBefore

That way the shared targets will always be present and will be picked up by every build run on the machine.

To be on the safe side, you should delete the “.sonarqube” folder created for each analysis build after the build has finished.