Skip to content
Latchkey

yamllint "duplication of key" error in CI

yamllint's key-duplicates rule catches a mapping that defines the same key twice. YAML keeps only the last one, so the duplicate quietly discards your earlier value - a real bug, not just style.

What this error means

yamllint reports "duplication of key \"env\" in mapping (key-duplicates)" pointing at the second occurrence. The config parses but behaves unexpectedly because one value won.

yamllint
service.yml
  12:3      error    duplication of key "env" in mapping (key-duplicates)

Common causes

A merged or copy-pasted block repeated a key

Editing two sections that each set env: (or ports:) leaves the same key twice in one mapping.

Anchors and overrides expanded to a duplicate

A merge key or copied anchor reintroduced a key already present at the same level.

How to fix it

Merge the duplicate keys into one

  1. Open the mapping at the reported line and find both occurrences.
  2. Combine their values under a single key, or rename one if they are distinct.
  3. Re-run yamllint to confirm the duplicate is gone.
service.yml
# wrong: env defined twice, first is discarded
env:
  A: "1"
env:
  B: "2"
# right: one mapping
env:
  A: "1"
  B: "2"

Keep key-duplicates enabled everywhere

Ensure the rule is on (it is by default) so silent overrides never ship.

.yamllint
rules:
  key-duplicates: enable

How to prevent it

  • Run yamllint in a pre-commit hook so duplicates fail before merge.
  • Review large YAML diffs for repeated top-level keys.
  • Prefer one mapping over anchors when they risk key collisions.

Frequently asked questions

What causes ""duplication of key ... in mapping""?
Editing two sections that each set env: (or ports:) leaves the same key twice in one mapping.
How do I fix "duplication of key ... in mapping"?
Merge the duplicate keys into one

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card