Cannot ignore specific code duplication (only option is to turn off the check?)

Hi, we have started using the SonarCloud service recently and are still tweaking the settings to make sure how to get the most out of the analysis without blocking our work too much. So far pretty good but still things pop up here and there that are really annoying.

This case is regarding code duplication. Overall I think it’s a great idea to have an option to prevent our PRs from being merged before code duplications are solved. However, sometimes code duplication are acceptable and even desirable. Below is a case like so. Its two data models that happen to share the same first part of the file in look, even though the tableName does differ.

See screenshots below.

My suggestion (if there are no good existing solution to this problem) would be to make this an issue that can be ignored, just like most of the other problems the analysis finds out.
Please tell me you have an even better way to solve this problem, without us having to turn off the Merge blocker in BB for having code duplication.

Details:

  • ALM used: Bitbucket Cloud
  • CI system used: Bitbucket Cloud
  • Languages: JavaScript (Node)
  • Potential workaround: Turn of the blocking feature

PS
Why preventing new users from uploading multiple images, that just makes me spend more time having to arrange one screenshot of two windows (like above)?
DS

Best regards,
Marcus Bergman

1 Like

Have you looked at this guidance?
https://sonarcloud.io/documentation/project-administration/narrowing-the-focus/#ignore-duplications

1 Like

Hi @dp-bergman, and welcome to our community! :slight_smile:

I understand your case, and here are some ways to get around such situation which might happen from time to time:

  • like @jamesrgregg said, you can exclude these files from the duplication detection in the project settings
    • And once the PR is merged, you might want to remove this exclusion
  • you can also relax the % of code duplication authorized on the quality gate you are using for your project
    • And again here, you might want to set it back to its previous value once the PR is merged
  • I don’t know how that works on BB, but maybe there is a way for project admins to force the merge even when some checks are failed (this is possible on GitHub for instance). This allows teams to overcome edge-case situations like this.

As you said, managing duplication through issues would be a solution to “properly” handle such situation. This is something we’ve already talked in the past but there was not enough traction to move forward.

2 Likes

Is it possible to have different % ratios for different set of files (like ‘.test.js’ have one percentage and the rest of '.js’ have another)?

It’s possible to have BB not requiring passes but that makes it easy to just go for “merge” without realising something is afoot.

–M

I’m afraid that this is not possible.

+1 for this if that gives you more traction :stuck_out_tongue:

The other methods of setting something, merging and then setting something back are so convoluted its enough to put any developer off.

Also +1 for this approach

Also +1 for this approach.

Also +1 for this approach.

And another +1 …

+1 for marking code blocks as not duplicates

1 Like

this is odd! Please change this!

+1 - It would be great to have a way to mark specific blocks as not duplicates.

1 Like

+1 this would remove by far our largest pain point with using sonarcloud.

The link is stale and I could not find it’s replacement: https://sonarcloud.io/documentation/project-administration/narrowing-the-focus/#ignore-duplications

@jamesrgregg or @Fabrice_Bellingard - Do you have another link that covers this topic of duplication, and how to disable/configure per file or per project?

Another +1 for a solution to this problem. And an updated link would be appreciated.

1 Like

I believe you’re looking for Analysis Scope | SonarCloud Docs

Another +1 for a solution to this problem. And an updated link would be appreciated.

Another +1 - It would be great to ignore a duplicated block or line or mark it as not an issue.

We’ve been struggling with this issue. While code duplication is important to avoid it seems crazy to apply it to such short snippets (when pulling them out into a separate function would require almost as many duplicated characters to call)

Hi! Is there a way to ignore blocks of code marked with a specific annotation rather than using start/end comments as explained here?

I’m already marking generated code with @Generated and more comments would be redundant. At the same time, I don’t want to ignore entire files only to suppress duplicate warnings in some legit parts.

I’m getting this:

only because we have DTOs implementing Builders, and lots of initialization functions are identical.

If it’s not possible, what other solutions can you think ok? Is SonarCloud reading Jacoco XML reports? Should I implement custom reports?

ChatGPT suggests writing something like:

@Rule(key = "GeneratedCodeDetectionRule")
public class GeneratedCodeDetectionRule extends IssuableSubscriptionVisitor {

Is this a valid suggestion? Where should I place this?