Skip to content
Latchkey

SonarQube "Duplicated Lines on New Code" gate failure in CI

The quality gate caps the percentage of duplicated lines on new code, commonly at 3%. Your change added blocks that Sonar detected as duplicates, pushing new-code duplication over the limit.

What this error means

The gate reports "Duplicated Lines (%) on New Code" above the required maximum. The measures tab lists the duplicated blocks Sonar found in the changed files.

sonar-scanner
QUALITY GATE STATUS: FAILED
  6.8% Duplicated Lines (%) on New Code (required <= 3.0%)

Common causes

Copy-pasted code in the changed files

The change repeats a block that already exists, or repeats within the same change, and Sonar flags the duplicated lines.

Generated or vendored files are being analyzed

Large generated files can look duplicated. If they are not excluded, they inflate the duplication ratio on new code.

How to fix it

Refactor the duplicated blocks

  1. Open the Duplications view for the changed files on the dashboard.
  2. Extract the repeated logic into a shared function or module.
  3. Re-run the scan to confirm new-code duplication is back under the cap.

Exclude generated code from duplication detection

If the duplication comes from generated or vendored files, exclude them so real code is measured accurately.

sonar-project.properties
sonar.cpd.exclusions=**/generated/**,**/*_pb2.py

How to prevent it

  • Factor shared logic into helpers instead of copying blocks.
  • Exclude generated and vendored code from analysis.
  • Review the Duplications view on the PR before merging.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →