Issue Analysis and project reference for .net core

  • CI used : Azure DevOps with sonar cloud task
  • Languages of the repository : c#

Hi,

I recently move on a project with a SonarCloud integration and I have an issue for a specific project architecture.

I have 3 projects .net core 3.1

  • Project1
  • Project2
  • ProjectShared

And for each, a test project associated.

Project 1 and 2 reference ProjectShared by project reference and they share the same naming convention for file and folder (So, it’s complex to create pattern for file exclusion).

When I launch the analysis on each project I would like to have result scoped on the current analysed project and his test project associated.

I can not add a <SonarQubeExclude>true</SonarQubeExclude> on ProjectShared otherwise I won’t be able to analyse it.

Example log in Run Code Analysis for Project1

INFO: Project configuration:
INFO:   Excluded sources: **/build-wrapper-dump.json, **/obj/**, **/*.dll
INFO: Indexing files of module 'ProjectShared'
INFO:   Base dir: D:\a\1\s\ProjectShared
INFO:   Source paths: Some files, Model/class1.cs...
INFO:   Excluded sources: **/build-wrapper-dump.json, **/obj/**, **/*.dll
INFO: Indexing files of module 'Project1'
INFO:   Base dir: D:\a\1\s\Project1
INFO:   Source paths: Some files, Model/class2.cs...
INFO:   Excluded sources: **/build-wrapper-dump.json, **/obj/**, **/*.dll

How to correctly analyse each project in this case without use SonarQubeExclude on the shared project?

Thanks

Hi,

Welcome to the community!

I’m not understanding what the difference is between what you’re getting and what you want. Can you explain the undesirable behavior you’re currently seeing?

 
Thx,
Ann

Hi,

Thank you for your welcome

Yeah, I’ll explain the best I can.

The full actual and expected behaviour

If I run 3 build (One for each project) I will have

Actual

Project1

INFO: Project configuration:
INFO:   Excluded sources: **/build-wrapper-dump.json, **/obj/**, **/*.dll
INFO: Indexing files of module 'ProjectShared'
INFO:   Base dir: D:\a\1\s\ProjectShared
INFO:   Source paths: Some files, Model/class1.cs...
INFO:   Excluded sources: **/build-wrapper-dump.json, **/obj/**, **/*.dll
INFO: Indexing files of module 'Project1'
INFO:   Base dir: D:\a\1\s\Project1
INFO:   Source paths: Some files, Model/class2.cs...
INFO:   Excluded sources: **/build-wrapper-dump.json, **/obj/**, **/*.dll

Project2

INFO: Project configuration:
INFO:   Excluded sources: **/build-wrapper-dump.json, **/obj/**, **/*.dll
INFO: Indexing files of module 'ProjectShared'
INFO:   Base dir: D:\a\1\s\ProjectShared
INFO:   Source paths: Some files, Model/class1.cs...
INFO:   Excluded sources: **/build-wrapper-dump.json, **/obj/**, **/*.dll
INFO: Indexing files of module 'Project2'
INFO:   Base dir: D:\a\1\s\Project2
INFO:   Source paths: Some files, Model/class2.cs...
INFO:   Excluded sources: **/build-wrapper-dump.json, **/obj/**, **/*.dll

ProjectShared

INFO: Project configuration:
INFO:   Excluded sources: **/build-wrapper-dump.json, **/obj/**, **/*.dll
INFO: Indexing files of module 'ProjectShared'
INFO:   Base dir: D:\a\1\s\ProjectShared
INFO:   Source paths: Some files, Model/class1.cs...
INFO:   Excluded sources: **/build-wrapper-dump.json, **/obj/**, **/*.dll

Expected

Project1

INFO: Project configuration:
INFO:   Excluded sources: **/build-wrapper-dump.json, **/obj/**, **/*.dll
INFO: Indexing files of module 'Project1'
INFO:   Base dir: D:\a\1\s\Project1
INFO:   Source paths: Some files, Model/class2.cs...
INFO:   Excluded sources: **/build-wrapper-dump.json, **/obj/**, **/*.dll

Project2

INFO: Project configuration:
INFO:   Excluded sources: **/build-wrapper-dump.json, **/obj/**, **/*.dll
INFO: Indexing files of module 'Project2'
INFO:   Base dir: D:\a\1\s\Project2
INFO:   Source paths: Some files, Model/class2.cs...
INFO:   Excluded sources: **/build-wrapper-dump.json, **/obj/**, **/*.dll

ProjectShared

INFO: Project configuration:
INFO:   Excluded sources: **/build-wrapper-dump.json, **/obj/**, **/*.dll
INFO: Indexing files of module 'ProjectShared'
INFO:   Base dir: D:\a\1\s\ProjectShared
INFO:   Source paths: Some files, Model/class1.cs...
INFO:   Excluded sources: **/build-wrapper-dump.json, **/obj/**, **/*.dll

If I add <SonarQubeExclude>true</SonarQubeExclude> on Projectshared I won’t have a result for it And I can’t find a filter which is only applyed to ProjectShared on Analysis setting for Project1 and 2

I miss probably some knowledge to reach my goal.

How can I have the expected result?

Thanks

Hi,

Can you try setting an exclusion for **/ProjectShared/**/*.* when you’re analyzing Project1 and Project2?

 
Ann

I already try, it doesn’t work.

I don’t know how the analysis works with the task sonarcloud, but I guess the “modules” are treated separately, so the exclusion works in the module and not on it.

The result we will have

INFO: Project configuration:
INFO:   Excluded sources: **/build-wrapper-dump.json, **/obj/**, **/*.dll, **/ProjectShared/**/*.*
INFO: Indexing files of module 'ProjectShared'
INFO:   Base dir: D:\a\1\s\ProjectShared
INFO:   Source paths: Model/Class10.cs, Model/Class2.cs...
INFO:   Excluded sources: **/build-wrapper-dump.json, **/obj/**, **/*.dll, **/ProjectShared/**/*.*
INFO: Indexing files of module 'Project1'
INFO:   Base dir: D:\a\1\s\Project1
INFO:   Source paths: Model/Class10.cs, Model/Class1a.cs...
INFO:   Excluded sources: **/build-wrapper-dump.json, **/obj/**, **/*.dll, **/ProjectShared/**/*.*
1 Like

Hi,

Thanks for getting back to me. I’ve flagged this for more expert attention.

 
Ann

@Youru just to make sure I understand the scenario: you’re issuing three separate build commands:

  1. build Project1 (which builds ProjectShared as a dependency);
  2. build Project2 (which also builds ProjectShared as a dependency); and
  3. build ProjectShared directly.

If that is the case, you could try the following:

  • add <SonarQubeExclude>true</SonarQubeExclude> to ProjectShared.
  • when you build ProjectShared directly in step 3, pass -p:SonarQubeExclude=false as an argument to MSBuild.

In MSBuild, command line parameters take precedence over project properties, so the shared project should be excluded in the first two builds.

2 Likes

Hi duncanp,

I tested it and it works.

Do you know if there is only this solution to achieve this result ?
Actually I’ve a task group on azure devops with the logic to send result to sonarcloud and it’s use by more than tewnty project. I would like to avoid to modify it with a new parameter to pass this argument when building the project (Task group is a bit sensitive : / ).

But if this the only way I will just clone it and add this specific behaviour.

Thanks for the tips

Ps: Do you know if there is a possibility for creating customs dashboards on sonar cloud to help the top level to quickly view the evolution of project in a specific perimeter ?

@Youru it’s difficult to say, as it depends on how you have defined your builds and Azure tasks.

SonarQubeExclude is just a normal MSBuild property, so you can use any standard MSBuild/Azure feature to configure or set the property e.g. MSBuild Conditions.

A quick test with MSBuild suggests that the following sample might be one way to set the `SonarQubeExclude’ property from the calling project:

Calling project:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
      <ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" >
          <Properties>SonarQubeExclude=true</Properties>
      </ProjectReference>
  </ItemGroup>
</Project>

Referenced project:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <SonarQubeExclude>false</SonarQubeExclude>
    </PropertyGroup>

    <Target Name="XXX" BeforeTargets="Build" >
        <Message Importance="high" Text="ZZZ: SonarQubeExclude=$(SonarQubeExclude)" />
    </Target>
</Project>

It’s best to start a new thread for a new question, particularly since this thread has is marked as having a solution.

I don’t think SonarCloud supports custom dashboards, but it’s not my area of expertise.

1 Like

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