SonarQube Duplication Code Issue

Hello,

We are using Sonarqube Developer EditionVersion 8.9.9 (build 56886) and we have a problem with the code duplication process in SonarQube where we cannot think of the best solution.

When we used the import statement, Sonarqube identified it as duplicated code. But we need to import those dependencies into each file.

Also, there are some instances where we want the exact code structure in different files by passing different values. But SonarQube captures it as a duplicate code without reading the passing values.

Also, we noticed that when we use the same code line in multiple files, it is captured as a duplicate code.

I appreciate your support in figuring this out.

Thanks,
Gimhan

Hi Gimhan,

Welcome to the community!

Are you interpreting the yellow highlight as a duplication marker? Because it’s not. The yellow highlight indicates code that has been added or edited in the New Code Period.

 
HTH,
Ann

1 Like

Hi Ann,

Thank you for the response.
Sorry for the confusion there. Didn’t mean to indicate highlighted lines as duplicates.

We want to have the same line of code in multiple files in some scenarios (Ex: import …). When we push the code with that sort of requirement, SonarQube captures it as duplication code. Can we avoid that cross-file duplication-checking?

Thanks,
Gimhan

Hi Gimhan,

Your best option is to set duplications exclusions. Unfortunately, by doing that, you’ll exclude the entire file from duplications detection.

BTW, just for clarity, what language is this?

 
Thx!
Ann

1 Like

Hi Ann,

Thanks for the information.

We are using Node.js (typeScript).

Thanks,
Gimhan

1 Like

Is there a similar method of ignoring code blocks in typescript as shown below for Java. My quality gate is failing due to using the same nestjs decorators for a number different controllers, I would prefer not to exclude the entire file from duplication checking if possible?

 public class MyClass {
    public MyClass() {
        ...
    }

    public void doSomething() {
        ...
    }

    // BEGIN-NOSCAN
    public void doSomethingElse()
    {
        ...
    }
    // END-NOSCAN
 }

Hi @Aiden_Murphy,

Welcome to the community!

There isn’t a block-level duplication exclusion. It’s the whole file or nothing.

 
:woman_shrugging:
Ann

thanks Ann, does // NOSONAR work with typescript? I had an odd case where my quality gate passed on branch, but failed when merged into main.

Hi @Aiden_Murphy,

We try to keep it to one topic per thread. Otherwise, things get messy, fast.

//NOSONAR is implemented on a language-by-language basis, and not for all languages. So my guess would be ‘no’. If you want to discuss it further, please create a new thread.

 
Ann