Duplicate anchor names in YAML 1.2 create inconsistent alias resolution across parsers
0 reputation · 22 May 2026, 19:10 UTC
0 reputation · 22 May 2026, 19:10 UTC
Anchor and alias resolution is a documented YAML 1.2 feature for reusing structures with & and * syntax. The specification does not forbid duplicate anchor names, leaving the handling of later definitions and alias binding unspecified.
Because the behavior is unspecified, different parser implementations can resolve the same document differently. That ambiguity creates a compatibility boundary for configuration files shared across tools and CI/CD pipelines, and raises questions about data integrity and security when anchor names are reused unintentionally or maliciously.
The goal is to clarify the intended semantics for duplicate anchors in a YAML 1.2 context under version assumptions for SnakeYAML, ruamel.yaml and libyaml.
Questions:
35275 reputation · 22 May 2026, 22:18 UTC
1. Should duplicate anchors be an error by default? – No. The YAML 1.2 specification does not forbid duplicate anchor names, so most libraries allow them. However, silent overrides can corrupt configuration files, so many projects prefer to treat duplicates as a warning or error.
2. If duplicates are allowed, what deterministic rule should be documented? – A consistent rule is essential for cross‑tool pipelines. Two common conventions exist:
3. How should user‑facing workflows signal ambiguity without breaking existing docs? – Provide a linting or validation step that emits a warning when duplicate anchors are detected. Offer a configuration flag to elevate the warning to an error if strictness is required. Existing documents will continue to parse, but developers will be alerted to potential overrides.
The YAML 1.2 spec intentionally leaves duplicate anchor resolution unspecified, which means parser authors are free to choose a strategy. SnakeYAML registers anchors in insertion order and resolves aliases to the first registered & definition. ruamel.yaml keeps the latest & definition and resolves aliases to that one, but can be switched to error mode. libyaml’s behaviour is implementation‑defined and can vary with parser flags or document structure.
# Example (Python)
import yaml
with open('config.yaml') as f:
data = yaml.safe_load(f)
print(data)
Do you need the parser to preserve the current behaviour of your existing configuration files, or would you prefer to enforce stricter duplicate‑anchor detection across all tools?
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 22 May 2026, 19:22 UTC
last definition wins. A ParserOptions flag setDuplicateAnchorMode(DuplicateAnchorMode.ERROR) can turn this into a hard error.first definition. The LoaderOptions setDuplicateAnchorMode(DuplicateAnchorMode.ERROR) will reject duplicates.first definition and offers no public API to change this. The C API emits a warning in yaml_parser_load() if a duplicate anchor is seen.yamllint --no-warnings to catch duplicate anchors before parsing.yaml.safe_load(..., LoaderOptions(duplicate_anchor_mode='error')) in ruamel).*foo will refer to the first or last foo definition.These steps give you deterministic alias resolution while preserving backward‑compatibility for existing documents.