stride3d/stride · error · DuplicateAnchorException

The anchor ' ' already exists

Error message

The anchor '{0}' already exists

What it means

DocumentLoadingState.AddAnchor registers an anchor-to-node mapping while loading a YAML document; when the same anchor name appears twice it throws DuplicateAnchorException because aliases would become ambiguous. YAML anchors must be unique within a document for alias resolution to be deterministic.

Solutions

  1. Rename duplicate anchors in the YAML document so every &name is unique
  2. Remove the second anchor declaration if it is not referenced by any alias
  3. Preprocess/validate the document before loading to detect duplicate anchor names
  4. If documents are merged, namespace the anchors per document before concatenating

Example fix

// before
item: &x {a: 1}
other: &x {b: 2}
// after
item: &x {a: 1}
other: &y {b: 2}
Defensive patterns

Strategy: validation

Validate before calling

// before load: scan the YAML text for anchor definitions and assert uniqueness
var names = Regex.Matches(yaml, "&([A-Za-z0-9_-]+)").Select(m => m.Groups[1].Value);
if (names.GroupBy(n => n).Any(g => g.Count() > 1)) throw new InvalidDataException("Duplicate YAML anchors");

Try / catch

try { return serializer.Deserialize(stream, typeof(Config)); }
catch (DuplicateAnchorException ex) { throw new InvalidDataException($"Duplicate anchor in YAML at {ex.Start}", ex); }

Prevention

When it happens

Trigger: Loading a YAML document containing two nodes that both declare the same &anchor name; programmatically calling AddAnchor twice with nodes sharing one Anchor value; concatenating or merging YAML documents that reuse anchor names.

Common situations: Hand-edited YAML where a copy/paste duplicated an &anchor; template generation that emits anchors from list items with identical names; combining partial config files that each reused the same anchor.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/764e110d9faa2ca8. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Yaml/Serialization/DocumentLoadingState.cs:73

    internal class DocumentLoadingState
    {
        private readonly IDictionary<string, YamlNode> anchors = new Dictionary<string, YamlNode>();
        private readonly IList<YamlNode> nodesWithUnresolvedAliases = new List<YamlNode>();

        /// <summary>
        /// Adds the specified node to the anchor list.
        /// </summary>
        /// <param name="node">The node.</param>
        public void AddAnchor(YamlNode node)
        {
            if (node.Anchor == null)
            {
                throw new ArgumentException("The specified node does not have an anchor");
            }

            if (anchors.ContainsKey(node.Anchor))
            {
                throw new DuplicateAnchorException(node.Start, node.End, string.Format(CultureInfo.InvariantCulture, "The anchor '{0}' already exists", node.Anchor));
            }

            anchors.Add(node.Anchor, node);
        }

        /// <summary>
        /// Gets the node with the specified anchor.
        /// </summary>
        /// <param name="anchor">The anchor.</param>
        /// <param name="throwException">if set to <c>true</c>, the method should throw an exception if there is no node with that anchor.</param>
        /// <param name="start">The start position.</param>
        /// <param name="end">The end position.</param>
        /// <returns></returns>
        public YamlNode GetNode(string anchor, bool throwException, Mark start, Mark end)
        {
            YamlNode target;
            if (anchors.TryGetValue(anchor, out target))
            {

View on GitHub (pinned to 96fad776d2)