2
votes

I'm setting up a new build server using TeamCity and Cake to build a c# class library and test project.

I'm trying to add SonarQube to the Cake build script. There already exists a plugin for Cake for this (https://github.com/AgileArchitect/Cake.Sonar) and I have this all working just fine when running the cake build script manually from within the TeamCity BuildAgent's working folder, however when it runs as a TeamCity build step the integration between MSBuild and SonarQube seems to break.

The error I get in the build log when running through TC is:

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 12.0 upwards are supported
  3. The begin, build or end steps have not all been launched from the same folder

None of these apply to me. The cake script is running the begin, build and end steps in both situations. I've logged out the current directory and it's as expected for both cases and the MSBuild version is exactly the same.

In the logs, the SonarBegin task logs identically for both commandline and TC. Here's the only significant differences in the two logs is in the Build step:

It looks to me, like the integration should work by MSBuild picking up some settings from Sonar like setting the RunCodeAnalysis target and adding parameters like /ruleset to the csc.exe call and this is not happening when run from TC.

Anyone got any advice for how to work out what's going on?

(Working - run from powershell commandline from within the TeamCity agent working directory)

ValidateSolutionConfiguration:
  Building solution configuration "Release|Any CPU".
The target "RunCodeAnalysis" listed in a BeforeTargets attribute at "C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\.sonarqube\bin\targets\SonarQube.Integration.targets (340,11)" does not exist in the project, and will be ignored.
The target "RunCodeAnalysis" listed in an AfterTargets attribute at "C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\.sonarqube\bin\targets\SonarQube.Integration.targets (391,11)" does not exist in the project, and will be ignored.
Project "C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\ContractManagement.Common.sln" (1) is building "C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\src\ContractManagement.Common\ContractManagement.Common.csproj" (2) on node 1 (default targets).
GenerateTargetFrameworkMonikerAttribute:
Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
CreateProjectSpecificDirs:
  Directory "C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\.sonarqube\out\\ContractManagement.Common_AnyCPU_Release_5799" doesn't exist. Skipping.
  Creating directory "C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\.sonarqube\out\\ContractManagement.Common_AnyCPU_Release_5799".
  Directory "C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\.sonarqube\conf\\ContractManagement.Common_AnyCPU_Release_5799" doesn't exist. Skipping.
  Creating directory "C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\.sonarqube\conf\\ContractManagement.Common_AnyCPU_Release_5799".
CoreCompile:
  C:\Program Files (x86)\MSBuild\14.0\bin\amd64\csc.exe /noconfig /nowarn:1701,1702 /nostdlib+ /errorreport:prompt /warn:4 /define:TRACE /highentropyva+ /reference:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\packages\Dapper.1.50.2\lib\net451\Dapper.dll /reference:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\packages\Microsoft.ApplicationInsights.2.2.0\lib\net45\Microsoft.ApplicationInsights.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\Microsoft.CSharp.dll" /reference:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\packages\morelinq.2.1.0\lib\net35\MoreLinq.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\mscorlib.dll" /reference:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Configuration.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Core.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Data.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Xml.dll" /debug:pdbonly /filealign:512 /optimize+ /out:obj\Release\UdGroup.ContractManagement.Common.dll /ruleset:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\.sonarqube\conf\SonarQubeRoslyn-cs.ruleset /errorlog:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\src\ContractManagement.Common\bin\Release\UdGroup.ContractManagement.Common.dll.RoslynCA.json /subsystemversion:6.00 /target:library /warnaserror- /utf8output /analyzer:C:\Users\michael.tong\AppData\Local\Temp\.sonarqube\.static\csharp_1.22.0.1631\SonarAnalyzer-1.22.0.1631.zip\Google.Protobuf.dll /analyzer:C:\Users\michael.tong\AppData\Local\Temp\.sonarqube\.static\csharp_1.22.0.1631\SonarAnalyzer-1.22.0.1631.zip\SonarAnalyzer.CSharp.dll /analyzer:C:\Users\michael.tong\AppData\Local\Temp\.sonarqube\.static\csharp_1.22.0.1631\SonarAnalyzer-1.22.0.1631.zip\SonarAnalyzer.dll /additionalfile:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\.sonarqube\conf\cs\SonarLint.xml /additionalfile:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\.sonarqube\conf\\ContractManagement.Common_AnyCPU_Release_5799\ProjectOutFolderPath.txt Authentication\AuthDetails.cs Authentication\Authenticator.cs Authentication\AuthResult.cs Authentication\IAuthenticator.cs Dto\BaseRuleSet.cs Dto\CompanyCheckResult.cs Dto\CompanyTypeEnum.cs Dto\Contract.cs Dto\ContractHeader.cs Dto\ContractSubmission.cs Dto\CreditCheckData.cs Dto\CreditCheckDecisionEnum.cs Dto\CreditCheckResult.cs Dto\CreditDecision.cs Dto\DbDataValue.cs Dto\DatabaseLocation.cs Dto\DbMetaData.cs Dto\DbMetaDataDictionary.cs Repositories\BaseContractRepository.cs Managers\ContractManager.cs Dto\ContractStatus.cs Dto\DbDataValueDictionary.cs Dto\FieldValueDictionary.cs Managers\ContractStatusManager.cs Managers\IContractStatusManager.cs Managers\ICreditVetRuleSet.cs Util\AppInsights.cs Util\Constants.cs Util\ContractSubmissionGenerator.cs Repositories\IContractRepository.cs Dto\IFieldWithDependencySupport.cs Dto\ISiteFieldValidator.cs Dto\PopulatedField.cs Dto\FieldMetaData.cs Dto\StatusResponse.cs Dto\ValidatedSiteField.cs Dto\ValidationFailureType.cs Dto\ValidationRequest.cs Dto\ValidationResponse.cs Properties\AssemblyInfo.cs Util\IContractSubmissionGenerator.cs Repositories\IEntityDataRepository.cs Repositories\IFieldMetaDataRepository.cs Repositories\QuickQuoteRepository.cs Repositories\RegistrationRepository.cs Repositories\ContractDataRepository.cs Repositories\FieldMetaDataRepository.cs Dto\EntityType.cs Util\AppConfigReader.cs Util\EnumHelper.cs Util\Extensions.cs Repositories\IEntityRepository.cs Util\FieldDependencyHelper.cs Util\IUDLogger.cs Util\SiteDetailsConfigDecorator.cs Util\IConfigReader.cs Util\TypeHelper.cs "C:\Users\michael.tong\AppData\Local\Temp\.NETFramework,Version=v4.5.2.AssemblyAttributes.cs"
... Warnings here

(Not Working - run by TeamCity)

ValidateSolutionConfiguration:
  Building solution configuration "Release|Any CPU".
Project "C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\ContractManagement.Common.sln" (1) is building "C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\src\ContractManagement.Common\ContractManagement.Common.csproj" (2) on node 1 (default targets).
CoreCompile:
  C:\Program Files (x86)\MSBuild\14.0\bin\amd64\csc.exe /noconfig /nowarn:1701,1702 /nostdlib+ /errorreport:prompt /warn:4 /define:TRACE /highentropyva+ /reference:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\packages\Dapper.1.50.2\lib\net451\Dapper.dll /reference:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\packages\Microsoft.ApplicationInsights.2.2.0\lib\net45\Microsoft.ApplicationInsights.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\Microsoft.CSharp.dll" /reference:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\packages\morelinq.2.1.0\lib\net35\MoreLinq.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\mscorlib.dll" /reference:C:\TeamCity\buildAgent\work\fd1026da6d1bbdb9\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Configuration.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Core.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Data.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Xml.dll" /debug:pdbonly /filealign:512 /optimize+ /out:obj\Release\UdGroup.ContractManagement.Common.dll /subsystemversion:6.00 /target:library /utf8output Authentication\AuthDetails.cs Authentication\Authenticator.cs Authentication\AuthResult.cs Authentication\IAuthenticator.cs Dto\BaseRuleSet.cs Dto\CompanyCheckResult.cs Dto\CompanyTypeEnum.cs Dto\Contract.cs Dto\ContractHeader.cs Dto\ContractSubmission.cs Dto\CreditCheckData.cs Dto\CreditCheckDecisionEnum.cs Dto\CreditCheckResult.cs Dto\CreditDecision.cs Dto\DbDataValue.cs Dto\DatabaseLocation.cs Dto\DbMetaData.cs Dto\DbMetaDataDictionary.cs Repositories\BaseContractRepository.cs Managers\ContractManager.cs Dto\ContractStatus.cs Dto\DbDataValueDictionary.cs Dto\FieldValueDictionary.cs Managers\ContractStatusManager.cs Managers\IContractStatusManager.cs Managers\ICreditVetRuleSet.cs Util\AppInsights.cs Util\Constants.cs Util\ContractSubmissionGenerator.cs Repositories\IContractRepository.cs Dto\IFieldWithDependencySupport.cs Dto\ISiteFieldValidator.cs Dto\PopulatedField.cs Dto\FieldMetaData.cs Dto\StatusResponse.cs Dto\ValidatedSiteField.cs Dto\ValidationFailureType.cs Dto\ValidationRequest.cs Dto\ValidationResponse.cs Properties\AssemblyInfo.cs Util\IContractSubmissionGenerator.cs Repositories\IEntityDataRepository.cs Repositories\IFieldMetaDataRepository.cs Repositories\QuickQuoteRepository.cs Repositories\RegistrationRepository.cs Repositories\ContractDataRepository.cs Repositories\FieldMetaDataRepository.cs Dto\EntityType.cs Util\AppConfigReader.cs Util\EnumHelper.cs Util\Extensions.cs Repositories\IEntityRepository.cs Util\FieldDependencyHelper.cs Util\IUDLogger.cs Util\SiteDetailsConfigDecorator.cs Util\IConfigReader.cs Util\TypeHelper.cs "C:\TeamCity\buildAgent\temp\buildTmp\.NETFramework,Version=v4.5.2.AssemblyAttributes.cs"

1

1 Answers

2
votes

After a couple of days of detective work, I've tracked down the problem. The root cause was the sonar targets which are required to integrate with MSBuild were not being picked up when MSBuild ran.

The reason for this was the TeamCity build agent was running as Local System Account, this means the MSBuild directory it's using to store target files is:

C:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\MSBuild

However, the SonarQube.Scanner.MSBuild.exe is compiled for 32bit, so this path gets change during the emulation on 64bit OS's to:

C:\Windows\sysWOW4\config\systemprofile\AppData\Local\Microsoft\MSBuild

However, when the MSBuild step runs, that is 64bit, so it looks in the system32 folder!

You can try to change the user that the build agent runs under, but that doesn't work. This is a quirk of how TeamCity Build agents work I believe. Basically, the appData folders in the environment settings don't get adjusted correctly.

The way I got it to work was to download the source for the sonarqube scanner and compile a 64bit version.

Other options that might work, which I'm going to look into now:

  1. Use a 32bit version of msbuild
  2. Manually change the env variables that point to the folder used by msbuild for targets so a folder outside of c:\windows\system32