Code analysis randomly fails on Azure Devops

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.