New Code, Clean-as-You-Code, Baselines, and Legacy Modernization: Diagnostics, Failure Modes, and Production Practices
Diagnose baseline laundering, incorrect version semantics, shallow-history defects, PR/main-branch confusion, hidden severe legacy risk, and invalid cross-project comparisons without moving the baseline to make results green.
Learning objectives
- Detect baseline resets performed after a failure and prove the policy change from history instead of accepting a green badge.
- Explain why new code includes modified old lines, not just newly created files.
- Separate pull-request comparison semantics from main-branch time/version baselines.
- Preserve and escalate severe legacy vulnerabilities even when the new-code gate is green.
- Reject cross-project quality comparisons when the projects use materially different new-code definitions.
- Follow an evidence-first diagnostic sequence that preserves the original failing revision, task, measures, and baseline.
1. Diagnostics start with the failed evidence, not a new baseline
When a New Code gate surprises you, preserve the failing analysis before changing version, time window, reference, or specific-analysis key. The first job is to determine whether the failure came from source, scanner/SCM evidence, Compute Engine, quality policy, or the baseline itself.
scanner/server/CI/API evidence → edition/version/runtime → source
revision/effective parameters → indexing/report/ceTaskId → CE
completion → profile/gate/New Code definition → SCM/auth/provider
→ database/search/JVM/host only if relevant → least-destructive
correction → smallest equivalent rerun
2. Failure mode: reset the baseline after a red result
Suppose a project uses Previous version and fails on a new issue.
The release owner changes sonar.projectVersion from
3.4.0 to 3.5.0 without fixing the issue,
then reanalyzes. If the failing issue becomes part of overall code
under the new period, the new-code gate can turn green even though
the source defect remains.
Repair by restoring the approved version/baseline, preserving both analysis keys and gate results, fixing or explicitly governing the code risk, and recording the unauthorized policy change.
3. Failure mode: treating new code as only new files
A 2,000-line legacy file edited on ten lines can contribute those changed lines to New Code. Teams that mentally equate New Code with “files created after migration” will misread coverage and issue evidence. Inspect changed-line highlights, SCM blame, revision history, and the configured definition.
4. Failure mode: applying PR semantics to main-branch history
Pull-request analysis compares the PR’s changes to a target branch. Main-branch New Code may be based on version, time, a specific analysis, or another supported definition. Do not assume that “new” on main means “the diff from the previous commit.”
Current Community Build and commercial Server have different branch/PR capability envelopes. Record edition and analysis type before using PR observations to diagnose a main-branch period.
5. Failure mode: green New Code, ignored severe legacy risk
A green new-code gate is not a waiver for a critical inherited vulnerability, exposed secret, safety defect, or regulatory breach. New-code policy is one release control. Legacy security/reliability programs may require separate gates, manual controls, remediation deadlines, or emergency work.
| Signal | Correct interpretation | Additional control |
|---|---|---|
| New code clean, old coverage low | Current change is not adding coverage debt | Legacy coverage/refactoring roadmap |
| New code clean, critical old vulnerability | New-code gate passes; severe historical risk remains | Security escalation/SLA/compensating control |
| Overall metric improves, new gate fails | Codebase trend improved but current change violates policy | Fix current change before merge/release |
6. Failure mode: comparing projects with different definitions
Project A uses 7 days, Project B uses Previous version with quarterly releases, and Project C uses a specific migration baseline. Their “new issues” and “new coverage” populations are not the same unit of measurement. Cross-project reporting must include definition metadata or normalize the comparison.
7. Failure mode: shallow SCM history harms New Code evidence
# Diagnostic only: prove whether the current repository is shallow.
git rev-parse --is-shallow-repository
git log --oneline --decorate -10
# In CI, prefer a full checkout. Example GitHub Actions concept:
# fetch-depth: 0
Do not “repair” a shallow-history warning by disabling SCM. Restore trustworthy history, then rerun the smallest equivalent analysis. Preserve the original scanner warning/log first.
8. Intentionally broken example: result-driven version bump
Run only against the disposable Chapter 12 project. Start from a
known failing new-code state under Previous version, preserve
report-task.txt, the version value, and gate response,
then deliberately increment the version without changing source.
# Baseline evidence from the failed run
PRE_FAIL_SHA=$(git rev-parse HEAD)
cp .scannerwork/report-task.txt evidence/pre-bump-failure-task.txt
# Intentionally broken policy action: same source, new release boundary
sonar-scanner \
-Dsonar.projectVersion="2.0.0" \
-Dsonar.buildString="ch12-broken-version-bump"
POST_BUMP_SHA=$(git rev-parse HEAD)
test "$PRE_FAIL_SHA" = "$POST_BUMP_SHA"
If the New Code population or gate changes while SHA remains identical, you have proven that policy/version—not remediation—caused the signal change. Repair by returning to the approved baseline/version and addressing the source or using a formally approved risk decision.
9. Troubleshooting shortcuts to reject
- Do not change the window/reference/version until the failing evidence is preserved.
- Do not lower gate thresholds to match current metrics.
- Do not delete a project and recreate it with a new key to erase history.
- Do not disable SCM to silence blame or shallow-clone warnings.
- Do not mass-accept or suppress findings as a substitute for baseline governance.
- Do not directly edit SonarQube database/search state.
- Do not use an administrator token for routine scanning.
10. Production practices
- Own New Code policy as configuration with named approvers.
- Record definition/value next to every release-quality report.
- Require full SCM history and stable version sourcing in CI.
- Keep a separate severe-legacy-risk register even when the new-code gate is green.
- Alert on unplanned baseline changes or project overrides when possible.
- Review time windows periodically based on delivery cadence, not current gate results.
- For upgrades, revalidate New Code semantics and APIs before assuming prior automation remains valid.
Knowledge check
Same SHA, different New Code status after a version change: where is the first suspect?
The New Code/version-policy layer, not the source code.
Why not disable SCM when blame warnings appear?
SCM data is evidence used for New Code and issue dating. Disabling it hides the symptom rather than restoring the missing history.
Can a modified line in a ten-year-old file be new code?
Yes. New Code is based on additions/modifications under the selected definition, not file creation age.
Why retain severe legacy risk outside the new-code gate?
Because some inherited risks require remediation regardless of whether the current change introduced them.
What makes two new-coverage percentages comparable?
Comparable definitions/values, observation dates, SCM/analysis semantics, and quality configurations—or an explicit normalization method.
Official references and version notes
- Quality standards and new code — current New Code concepts, four definition modes, default baseline, and Clean-as-You-Code framing.
- Configuring new code calculation — project overrides, Specific analysis API-only behavior, and Previous version configuration.
- Global New Code baseline — global inheritance and the default Previous version baseline.
- Checked-out code — full Git history requirements for New Code, blame, and issue backdating.
- Issue management solution — issue identity/date/backdating and how current analysis relates findings to new code.
- Understanding quality gates — Sonar way and new-code-focused quality conditions.
- Feature comparison table — current Community Build versus Server/Cloud branch and pull-request boundaries.
- Analysis overview — scanner/report/Compute Engine processing and new/overall result computation.
- Web API — authenticated API usage and release-sensitive endpoint guidance.
- SonarQube downloads — current Community Build release identity.
- SonarScanner CLI 8.1.0.6389 — scanner baseline used by the local lab.
Rechecked 2026-09-07. Mandatory executable examples target SonarQube Community Build 26.9.0.129388 and SonarScanner CLI 8.1.0.6389. Current Community Build documentation lists Previous version, Number of days, Specific analysis, and Reference branch as New Code definition modes; the project-level definition overrides the global baseline, whose default is Previous version. Specific analysis is configured through the Web API. Commercial branch analysis and broader enterprise capabilities are not required by this chapter. Re-check the linked primary documentation and your instance’s built-in Web API before automating against another release.
Keep the academy open
Support free, practical DevOps education.
Every lesson is designed to remain readable in a browser, downloadable from GitHub, and usable without a paid learning platform. Contributions help expand and maintain the curriculum.
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0Send only Ethereum/ERC-20 compatible assets to this
address.