Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

SonarQube reports duplicated lines and fails the quality gate

ErrorDuplicated Lines (%) is high on the project overview

Short answer

SonarQube flags a block as duplicated when it has at least 100 successive duplicated tokens spread over at least 10 lines, ignoring indentation and string literals. Either refactor the repeated code into one component or function, or exclude files that should not be checked, such as tests, with sonar.cpd.exclusions.

  • Applies to SonarQube Server 2026.x, SonarScanner CLI, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the project's Duplications measure and list the files. Decide for each one whether the repeated code is real or whether the file should not be checked at all.

    # SonarQube: Project > Measures > Duplications > Duplicated lines
  2. 2Match what you see, then jump to its fix

    1. 1Two or more components or utilities contain the same long block with different names or stringsThe same block really is repeatedFix
    2. 2The flagged files are tests, generated code or fixturesFiles that should not be checked for duplication are includedFix
    3. 3You set Duplication Exclusions in the project settings, but the same files still countThe UI setting is overridden by the scannerFix

What does the error look like?

  • Duplicated Lines (%) is high on the project overview
  • Duplicated blocks reported in several React components or test files
  • A Duplication Exclusions value set in the UI has no effect

Cause 1 of 3

The same block really is repeated

1Check

Duplicated lines is the number of lines involved in duplications. Outside Java, a block counts when it has at least 100 successive duplicated tokens over a minimum number of lines (10 for most languages). Differences in indentation and string literals are ignored.

# SonarQube: open the file > Duplications tab shows the matching blocks

2Fix

Move the shared block into one component, hook or function and import it where needed.

// before: the same 15 lines in OrdersTable and UsersTable
// after
import { DataTable } from '../components/DataTable';
export const OrdersTable = (p) => <DataTable columns={orderColumns} {...p} />;

3Verify

Run the analysis again and compare the Duplicated lines (%) value.

sonar-scanner

Confirmed by docs.sonarsource.com

Did this fix it?

Cause 2 of 3

Files that should not be checked for duplication are included

1Check

sonar.cpd.exclusions defines the source files excluded from the duplication check. It takes a comma-separated list of path patterns relative to sonar.projectBaseDir: * stays inside one directory, ** crosses directories.

grep -n "sonar\." sonar-project.properties

2Fix

Exclude only those paths from the duplication check.

# sonar-project.properties
sonar.cpd.exclusions=**/*.test.tsx,**/__generated__/**

3Verify

Run the analysis; the excluded files no longer appear under Duplications.

sonar-scanner

Confirmed by docs.sonarsource.com

Did this fix it?

Cause 3 of 3

The UI setting is overridden by the scanner

1Check

A parameter set on the CI/CD host takes precedence over the UI setting of the same name, and scanner arguments override scanner configuration files.

grep -rn "sonar.cpd" . --include=sonar-project.properties --include=*.yml

2Fix

Change the value where the scanner reads it, in the CI configuration or in sonar-project.properties, or remove it there so the UI value applies.

# put the value where the scanner reads it (CI configuration or sonar-project.properties)
sonar.cpd.exclusions=**/*.test.tsx

3Verify

Check the analysis log and the Duplications measure after the next run.

sonar-scanner

Confirmed by docs.sonarsource.com

Did this fix it?

How do I stop it coming back?

  • Extract shared UI into components as soon as a second copy appears.
  • Keep generated code in a folder that is excluded from duplication.
  • Keep analysis parameters in one place, either CI or the properties file, so there is no hidden override.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.