LOC duplication of shared library in dotnet monorepo

Hello,

We have a repository containing solutions for frontend and backend dotnet projects.

I’ve created two projects in SonarCloud for these, and everything is running as we want it to, as far as I can tell. The frontend CI pipeline runs on Windows to generate an MSI. The backend CI pipeline runs on Linux to crreate NuGet packages and Docker images. I’d run it all on Linux if I was able to.

The only concern I have is around LOC counts when both solutions share a common library project from the same repository. I remember reading somewhere that this common dependency would not be counted twice, but I can’t find that reference now.

backend.sln (SonarCloud Backend project)
├───Shared.Library/
├───Backend.Library/
└───Api/
frontend.sln (SonarCloud Frontend project)
├───Shared.Library/
├───Client.Library/
└───Client.Installer/

When I look at each project in SonarCloud, they both contain Shared.Library with slightly different line counts (10,512 and 10,533).

So I have two questions:

  1. Is there anyway that I can confirm that Shared.Library has not been counted twice?
  2. How do I determine which branch is the “longest” in SonarCloud (without clicking into every single branch in the UI)?

Hi,

If you’re analyzing these as two different projects, then Shared.Library is being counted twice. I have no idea why they would have different LOC counts. I’m going to put that down to the intricacies of .NET analysis.

You can use exclusions to keep one of the copies from being analyzed.

 
HTH,
Ann

That’s a shame. The docs definitely said duplications like this would not be counted - I guess that was changed at some point.

To help anyone else in the future, I’ve achieved this with the following approach:

  1. Within the csproj file which is being counted twice by SonarCloud, add a custom property wrapping SonarQubeExclude

      <PropertyGroup Condition="'$(SonarQubeExcludeDuplicates)' == 'true'">
          <SonarQubeExclude>true</SonarQubeExclude>
      </PropertyGroup>
    
  2. During your CI build, pick one of the jobs to apply the exclusion by passing the SonarQubeExcludeDuplicates flag to msbuild or dotnet

    MSBuild -p:SonarQubeExcludeDuplicates="true" MySolution.sln
    dotnet build -p:SonarQubeExcludeDuplicates="true" MySolution.sln
    

A good way to check that this is working as intended locally is to run the Sonar Scanner in Simulation mode by adding <Property Name="sonar.scanner.dumpToFile">sonar-scanner.log</Property> to the SonarQube.Analysis.xml. Run the begin, build, and end commands and then find the .sonarqube/out/ProjectInfo.log file. If it worked, the project will be listed under the “Skipped projects” section.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.