37

When you are building a dotnet core project with SonarQube you may be facing the error in the log:

WARNING: The following projects do not have a valid ProjectGuid and were not built using a valid solution (.sln) thus will be skipped from analysis...

What should you do?

LockeCJ
  • 558
  • 1
  • 4
  • 21
Daniel
  • 9,491
  • 12
  • 50
  • 66

4 Answers4

72

As dotnet core projects (.csproj) will not have <ProjectGuid>...</ProjectGuid> tag specified in the default template this needs to be manually added.

So you need to edit the .csproj file like this:

<PropertyGroup>
  <!-- other properties here -->

  <!-- SonarQube needs this -->
  <ProjectGuid>{E2CEBBAF-6DF7-41E9-815D-9AD4CF90C844}</ProjectGuid>

Make sure to place your own GUID inside the <ProjectGuid>...</ProjectGuid> Tag

Daniel
  • 9,491
  • 12
  • 50
  • 66
  • Hey, little late to the party, but how do you generate the project GUID? Or do you use the one generated in the .sln file ? – Kobek Jul 13 '18 at 12:12
  • 1
    create a new one using e.g. https://www.guidgenerator.com/online-guid-generator.aspx – Daniel Jul 13 '18 at 12:17
  • 1
    So there is absolutely no connection between this GUID and the ones in the .sln file – Kobek Jul 13 '18 at 12:53
  • 3
    Correct. There is no Connection. – Daniel Jul 13 '18 at 13:24
  • 3
    Powershell to fix it write-host "{$([guid]::NewGuid().ToString().ToUpper())}" – Patrik Lindström Sep 06 '18 at 13:21
  • @PatrikLindström Thank you! And you don't even need `Write-Host` if you're at a PowerShell command prompt – JamesQMurphy Jul 02 '19 at 17:27
  • If a solution reference the project, you find the project's guid in the .sln. – vernou Oct 01 '19 at 07:37
  • 1
    Thank you so much for your guidance. This is a very critical issue which has no proper solution. I have checked all possible way but didn't get proper solution. Applying your solution, i get to know this issue belongs to VS2017. Before that i was feeling so helpless. Thanks again to post a very useful solution for us. – Sapnandu May 17 '20 at 10:41
  • @JamesQMurphy Yes you are right you could hence pipe it to clip to get it into the clipboard directly "{$([guid]::NewGuid().ToString().ToUpper())}" |clip – Patrik Lindström Oct 31 '20 at 12:07
  • @Kobek, I believe there _is_ a connection, unlike what Daniel said in the comment. I just added a new answer to Daniel's old question here. – Jeppe Stig Nielsen Feb 06 '23 at 12:16
27

Here's a solution to adding the missing xml elements in 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")
    $node.AppendChild($child)
    $doc.Save($path)
}
activout.se
  • 6,058
  • 4
  • 27
  • 37
chrisevett
  • 611
  • 8
  • 15
  • 1
    Thanks a bunch! I think though that the GUID should be enclosed in {} to match Visual Studio, but maybe it works anyway? – activout.se Jan 30 '19 at 15:12
8

The solution I came up with is to build the solution, not the project files. If using Azure DevOps, use **/*.sln instead of **/*.csproj in your DotNetCoreCLI build step.

Frank Hoffman
  • 887
  • 1
  • 11
  • 16
0

Note that the ProjectGuid can be found in the .sln file if you use such a file.

The .sln file has a long line similar to:

Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Project1",
"path\Project1.csproj", "{307CB8DB-DC9C-4830-AA92-D842D89E17E0}"

In it, the first Guid (inside Project(...)) specifies the type of the project, and you can find a "dictionary" on the web that translates each Guid there to a human-readable type. For example, {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} means "C#" project type.

But the last Guid (given after the filename) in the .sln entry is the ProjectGuid.

So if you use an .sln file always, also from the SonarQube runs, there should be no need to insert a Guid in the .csproj file.

The other place the ProjectGuid can be specified is in the .csproj. Like other answers say, it looks like this:

<PropertyGroup>
    <!-- possibly other properties -->
    <ProjectGuid>{307CB8DB-DC9C-4830-AA92-D842D89E17E0}</ProjectGuid>
</PropertyGroup>

So if you never use .sln file in any context, you can just create any new Guid. In Visual Studio, you can use "Tools" - "Create GUID" - "Registry Format" to create a fresh Guid and and have it formatted correctly for this purpose.

However, from the above, it is clear that if you do use an .sln file in some contexts (like from Visual Studio during development/maintenance), but do not use that .sln file from the SonarQube run, then the ProjectGuid you put in the .csproj should match the one present in the .sln. One way you could achieve it, when creating new projects, is to first add the new project to the .sln, then do a diff ("Compare with Unmodified" or similar) on your .sln file using your version control software (like Git) to obtain the ProjectGuid, and copy/paste that Guid, including braces {...} into the new .csproj file.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181