Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SonarQube dotnet core: how to avoid the duplicate guid error without altering the default build task

I will preface this that my current solution to this is very easy but one I do not want to keep implementing.


The Problem

Below you will see an image outlining my current build steps. Each of these contain the default settings, with the Prepare analysis on SonarQube setup to point to my endpoint.

When I run this, again just default settings, I am presented with the following errors

WARNING: Duplicate ProjectGuid: "00000000-0000-0000-0000-000000000000". The project will not be analyzed by SonarQube. Project file: "D:\a\1\s\API.Tests.csproj"
WARNING: Duplicate ProjectGuid: "00000000-0000-0000-0000-000000000000". The project will not be analyzed by SonarQube. Project file: "D:\a\1\s\API.csproj"

This is because the build step for dotnet core, by default, looks for **/*.csproj using the linked setting (Parameters.RestoreBuildProjects) - with the update to the csproj format the project guid is no longer stored in the csproj files. What I suspect is happening is that SonarQube just defaults the guids when it finds nothing defaults to 000... and then throws this error.


The Fix

Unlinking the Path to project(s) parameter and pointing to **/.*.sln fixed the issue, because now SonarQube can see the project guids (defined the .sln)


The Question, finally

After that long winded explanation I am lead to ask if there is a better way to get SonarQube to recognise dotnet core projects.

I do not want to change the default build task every time I create a project to satisfy SonarQube's requirements.


Sonarqube with a dotnet core project

like image 788
Questioning Avatar asked Jul 08 '18 10:07

Questioning


2 Answers

$paths = Get-ChildItem -include *.csproj -Recurse
foreach($pathobject in $paths) 
{
    $path = $pathobject.fullname
    $doc = New-Object System.Xml.XmlDocument
    $doc.Load($path)
    $child = $doc.CreateElement("ProjectGuid")
    $child.InnerText = [guid]::NewGuid().ToString().ToUpper()
    $node = $doc.SelectSingleNode("//Project/PropertyGroup")
    $node.AppendChild($child)
    $doc.Save($path)
}
like image 167
Artur Mustafin Avatar answered Nov 13 '22 01:11

Artur Mustafin


Building on Artur Mustafin solution, I took that and put it inside a powershell step. I also check for the node incase you have a mix of .net and core

- powershell: |
   $paths = Get-ChildItem -include *.csproj -Recurse
   foreach($pathobject in $paths) 
   {
       $path = $pathobject.fullname
       $doc = New-Object System.Xml.XmlDocument
       $doc.Load($path)
       $child = $doc.CreateElement("ProjectGuid")
       $child.InnerText = [guid]::NewGuid().ToString().ToUpper()
       $node = $doc.SelectSingleNode("//Project/PropertyGroup")
       if ($node)
       {
           $node.AppendChild($child)
           $doc.Save($path)
       }
   }
  workingDirectory: '$(Build.SourcesDirectory)'
  displayName: 'Add Project GUIDs'
like image 31
Nicholas Mayne Avatar answered Nov 13 '22 00:11

Nicholas Mayne