SonarQube reports duplicated lines and fails the quality gate
Duplicated Lines (%) is high on the project overviewShort 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.
Fix it fast
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 lines2Match what you see, then jump to its fix
- 1
Two or more components or utilities contain the same long block with different names or stringsThe same block really is repeatedFix - 2
The flagged files are tests, generated code or fixturesFiles that should not be checked for duplication are includedFix - 3
You set Duplication Exclusions in the project settings, but the same files still countThe UI setting is overridden by the scannerFix
- 1
What does the error look like?
Duplicated Lines (%) is high on the project overviewDuplicated blocks reported in several React components or test filesA 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 blocks2Fix
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-scannerConfirmed by docs.sonarsource.com
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.properties2Fix
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-scannerConfirmed by docs.sonarsource.com
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=*.yml2Fix
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.tsx3Verify
Check the analysis log and the Duplications measure after the next run.
sonar-scannerConfirmed by docs.sonarsource.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix