LOC duplication of shared library in dotnet monorepo

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.