SonarScanner for MSBuild: How do you clear down MSBuild after a failure?

SonarQube 6.7.1
SonarScanner for MSBuild 4.3.1
MSBuild 15.7

I am using SonarScanner for MSBuild to analyse a Visual Studio solution containing many C# projects. I am using the instructions at [].
There are broadly three steps:

  1. SonarScanner.MSBuild.exe begin
  2. MSBuild.exe
  3. SonarScanner.MSBuild.exe end

Step one adds hooks to the MSBuild installation on the build agent.
If step 2 fails, how can I remove the hooks from the MSBuild installation so that other builds not involving SonarQube can go ahead?

I cannot find anything in the documentation that describes how to handle a failure during step 2 of the process.
I was expecting to find a ‘clear’ option to go alongside ‘begin’ and ‘end’.
Is it always necessary to run step 3 after successful execution of step 1, even if step 2 fails?


Check out the documentation on concurrent analysis on the same build machine:


Hi Colin,

Thanks for the reply, but I don’t understand how enabling concurrent analysis might help. Our build agents (TeamCity) always execute builds serially, never concurrently. I don’t think that TeamCity supports any other execution model. The TeamCity server delegates a build to an agent and will not delegate another build to the same agent until the first build has completed.
I am trying to find out how I go about ensuring that subsequent builds on an agent are not affected by a failure during stage 2 on a previous build, since stage 1 has affected the MSBuild engine.


Apologies! I made a false assumption about what you’re trying to achieve.

I can’t exactly answer your question, but I can tell you that failing builds (on step 2) has never caused an issue with our build pipelines so far as leftover hooks into MSBuild.

Perhaps somebody will come along with a more authoritative answer. :slight_smile:


Hi @David_Yi,

I am not sure how much of a detailed answer you are looking for so I will start with a small one. Each time you run a new begin step we clear the data and redo everything as if it was the first time you run the command.

For example:

First build of the project on your CI

  1. begin step (wipe, download and setup the analysis)
  2. build => fails. As the end step was not called the targets are not cleared so the only impact would be if you re-build without running an analysis.

Second build on your CI

  1. begin step (wipe, download and setup the analysis)
  2. build => success
  3. end step (cleans the targets installed)

Hope that was enough for you.


Hi Amaury,

Thank you for the reply. You said “…so the only impact would be if you re-build without running an analysis”
This is exactly the case I am asking about.

I start a build run for Sonar via the ‘begin’ step
I perform the build and it fails.
Currently my ‘end’ step is not called due to the earlier build failure.

I now want to perform a normal (non-Sonar) build on a completely unrelated project on the same build agent.

In order for the normal build to be performed, must I force the ‘end’ step from the previous Sonar build to be executed?

In other words, is it necessary to always perform the ‘end’ step during a Sonar build (regardless of the success or failure of the build itself) in order to ensure that any Sonar hooks within the MSBuild engine are released and that subsequent unrelated builds can proceed?

If so, what are the consequences of running the ‘end’ step if a build has not succeeded? Might I end up with partial results within Sonar?

I really expected there to be a ‘clear’ stage on SonarScanner.MSBuild, so that I could write my builds along these logical lines:

try {
} catch(build failure) {

Hi David,

Sorry I didn’t get your exact scenario at first.

The target that is dropped in the msbuild import before (by the begin step) is only a kind of bootstrapper that will try to execute the target in the .sonarqube of the project. So even if the end step is not called, all non-sonar projects remain unaffected.

Thank you. That answers my question.

All I have to do is to ensure that there is no .sonarqube folder in order to proceed with a non-sonar build.