I’m using SonarScanner for MSBuild 7.1.1 and I am trying to apply some custom settings to my project. The project runs on a CI/CD pipeline with some global setups.
dotnet sonarscanner begin /d:sonar.host.url="https://sonar.domain.com:443" /d:sonar.login=password /d:sonar.cs.opencover.reportsPaths=**/coverage.opencover.xml /k:project /v:1.0.0
dotnet build
dotnet sonarscanner end /d:sonar.login=password
I am trying to see the Unit Test count in sonar so I read I need to apply the “sonar.cs.vstest.reportsPaths” setting. Running locally if I add /d:sonar.cs.vstest.reportsPaths=**/testResults.trx it works but I don’t have access to edit the pipeline and since the pipeline is used by several other projects, the pipeline managers are reluctant to add this command line argument in so I tried to add in my main csproj these lines
But it’s not working. I’m guessing it’s because this property is applied on a per-item basis from what I can tell when looking at the .sonarqube/out/sonar-project.properties file. I also read that sonar should have automatically detected the TestResults/testResults.trx file for sonar.cs.vstest.reportsPaths but it hasn’t. In my test project’s csproj I’ve added <SonarQubeTestProject>true</SonarQubeTestProject> but it didn’t work.
I’ve also tried a relative path and absolute path instead of **/testResults.trx. I don’t know what am I doing wrong.
Apologies for the late reply, I was investigating the available options.
Would the pipeline admins be willing to allow people to pass their own custom SonarQube.Analysis.xml?
This way you can override or pass any parameter you wish to the pipeline.
You need to create this XML (here is the template) and set inside <Property Name="sonar.cs.vstest.reportsPaths">**/*.trx</Property> and then pass it during the begin step like dotnet sonarscanner begin /k:key /d:sonar.host.url="http://localhost:9000" /d:sonar.token=token /s:fullPath\SonarQube.Analysis.xml.
Let me know if this is an option.
Unfortunately, setting this parameter through SonarQubeSetting does not work per project. I’ll check the documentation to make this explicit.
Thanks for the reply Mary. I’ve tested locally this option with the full path and can confirm it’s working. I could potentially discuss this with the pipeline managers but I can see a caveat though as this expects a full path. We’re running the pipeline on multiple environments with the same setup.
I tried using /s:./SonarQube.Analysis.xml but it’s erroring with Unable to find the analysis settings file '/root/project/.sonarqube/SonarQube.Analysis.xml' Please fix the path to this settings file.
So I tried to outsmart it by doing /s:../SonarQube.Analysis.xml to navigate to the project folder but it then gave
Unable to find the analysis settings file '/root/SonarQube.Analysis.xml' Please fix the path to this settings file.
I’m assuming there’s multiple locations in the cli that use this argument so when some instances ran they found the correct /project/SonarQube.Analysis.xml file but when the directory changed to .sonarqube the second reference thought ./SonarQube.Analysis.xml was supposed to be in the current working directory.
And in the second case scenario (…/SonarQube.Analysis.xml), the cli errored on the first reference.
Is there a way to parse the relative path to the project instead? Or some variable that sonar would understand as the project directory?
Something like /s:$projDir/SonarQube.Analysis.xml maybe that sonar could always interpret the variable as the project root?
It’s more of a hack, but maybe after the begin step runs, you can edit the SonarQube.Analysis.xml, which is created automatically under the projectRoot\.sonarqube\conf.