1
votes

I'm programmatically able to call MsBuild.SonarQube.Runner begin (by making calls into referenced assemblies directly), Microsoft.Build.BuildManager.Build and MsBuild.SonarQube.Runner end. But, the issue is Runner end reports that No ProjectInfo.xml files were found. Possible causes: you specified an invalid build configuration or the custom MSBuild analysis targets were not imported.

Is MsBuild.SonarQube.Runner tightly coupled with MSBuild.exe commandline tool? I'm not clear on how exactly the runner gets details of build events.

Is it even possible what I'm trying to achieve?

I must add that runner begin and build are succeeding. bin\Debug folder is populated with built binaries.

3

3 Answers

0
votes

You can build projects programmatically using the MSBuild API and have SonarQube analysis performed as part of the build. The SonarQube Scanner for MSBuild is not tightly coupled to the MSBuild.exe command line.

ImportBefore functionality is implemented in the standard Microsoft targets files. For example, have a look at %ProgramFiles(x86)%\MSBuild\14.0\Bin\Microsoft.Common.CurrentVersion.targets which contains the following line:

  <Import Project="$(MSBuildUserExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.targets\ImportBefore\*" Condition="'$(ImportUserLocationsByWildcardBeforeMicrosoftCommonTargets)' == 'true' and exists('$(MSBuildUserExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.targets\ImportBefore')"/>
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.targets\ImportBefore\*" Condition="'$(ImportByWildcardBeforeMicrosoftCommonTargets)' == 'true' and exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.targets\ImportBefore')"/>

All these lines do is import any target files that exist in well-known locations. This mechanism can be used to automatically include targets into every project, without having to explicitly include the targets file in the project. This mechanism is independent of MSBuild.exe. However, it does matter which version of standard targets files you are using: the v4.0 standard target files don't include this mechanism, but the v12.0 and v14.0 versions do.

The simplest way to trigger a build programmatically is to use MSBuild v12.0 or v14.0 and rely on the standard ImportBefore behaviour.

Alternatively, you could do one of the following:

  1. manually edit your project files to explicitly include the required SonarQube targets, or

  2. programmatically import the required SonarQube targets using the MSBuild API before triggering the build (by adding an "Item" of type "Import" with the include parameter pointing to the location of the targets file to import). The SonarQube Scanner for MSBuild integration tests use this approach.

0
votes

The SonarQube Scanner for MSBuild is an open source project, so you can see for yourself how everything works under the hood: https://github.com/SonarSource/sonar-msbuild-runner

I don't think it is tighly coupled to the MSBuild.exe command line, however it does depend on the ImportsBefore mechanism of MSBuild, and only works for specific MSBuild versions (12 and 14 currently).

Your question is about the implementation of a solution to some problem, however you don't say what this initial problem is. This makes it difficult to give a good answer.

0
votes

Since MSBuild Runner for SonarQube depends on ImportsBefore feature of MSBuild and because Microsoft.Build API has no comparable for ImportsBefore, at least as of now there is no way to use Sonar Runner for MSBuild in conjunction with the API. It has to be used along with MsBuild.exe by means of process invocation.