How to exclude common projects in "Connected Mode"

We have one solution file per product and each of these solution has corresponding SonarQube project. These solutions has some projects which are common. Something like below.

Product1.sln
      Common.Infra.csproj
      Product1.Business.csproj
      Product1.UI.csproj
Product2.sln
      Common.Infra.csproj
      Product2.Business.csproj
      Product2.UI.csproj

We have configured conditional analysis as given here. So, in our case we do not run analysis on Common.Infra.csproj when either of given solution files are built in pipeline. Instead we have Common.sln where Common.Infra.csproj is analyzed.
Question is when we try using connected mode, ruleset from both the SonarQube project is getting added into these common project file. How do we ensure that when we bind SonarQube project with solution common projects are excluded?

Please note we can’t exclude them by adding <SonarQubeExclude>True</SonarQubeExclude> as these common projects are being analyzed in Common.sln.

The condition in the linked snippet will never evaluate to true in the IDE because SQProjectKey won’t be set in the IDE scenario.

You need to modify or add a condition so that SonarQubeExclude is set appropriately in the IDE and in your CI scenarios. You have the full range of options available in MSBuild to conditionally set the values (e.g. using ImportBefore files, Directory.Build.props files, property precendence, using other MSBuild properties in the condition etc) so there are lots of different ways you could do this.

One simple approach would be to add something like the following to the Common.Infra.csproj:

  <PropertyGroup>
    <SonarQubeExclude Condition=" $(SolutionName) != 'Common' ">true</SonarQubeExclude>
  </PropertyGroup>
1 Like

Thank you. Look like a new way to try out. :slight_smile: I will give it a try.

@duncanp While trying this out along with “Connected Mode” I come across another issue which is side effect of having common projects. So, now we have 3 different SQ projects.

  1. Product1
  2. Product2
  3. Common.Infra

We are trying to bind solutions to SQ projects. We started with Common.Infra which we could successfully bind. Then we tried binding Product1 and SonarLint complained about Common.Infra.csproj. Error as below.

Unexpected error during workflow execution: Failed to bind project 'C:\main\Common\Infra\Common.Infra.csproj'.
A conflicting version of SonarLint.xml has been found. Please delete the file 'C:\main\Common\Infra\.sonarlint\Common.Infra\CSharp\SonarLint.xml' and remove references to it from your projects, then re-open the solution and try again..

So question is how do we solve this problem? working without “Connected Mode” is definitely not a good option.

Look like this was raised earlier as well Support single .csproj in multiple .sln files · Issue #1788 · SonarSource/sonarlint-visualstudio · GitHub

Try using a condition similar to the one I suggested above to the Include in the Common.Infra project file that imports the Common.Infra SonarLint.xml file
i.e. so that that SonarLint.xml file is only included if the solution being opened is Common.Infra.

1 Like

Not sure if this is right solution but I think SonarLint should not try bind project which is already has binding and give some warning. Again not sure if this has any side effects.

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