{"record":{"id":"764e110d9faa2ca8","repo":"stride3d/stride","slug":"the-anchor-0-already-exists","errorCode":null,"errorMessage":"The anchor '{0}' already exists","messagePattern":"The anchor '(.+?)' already exists","errorType":"exception","errorClass":"DuplicateAnchorException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Yaml/Serialization/DocumentLoadingState.cs","lineNumber":73,"sourceCode":"    internal class DocumentLoadingState\n    {\n        private readonly IDictionary<string, YamlNode> anchors = new Dictionary<string, YamlNode>();\n        private readonly IList<YamlNode> nodesWithUnresolvedAliases = new List<YamlNode>();\n\n        /// <summary>\n        /// Adds the specified node to the anchor list.\n        /// </summary>\n        /// <param name=\"node\">The node.</param>\n        public void AddAnchor(YamlNode node)\n        {\n            if (node.Anchor == null)\n            {\n                throw new ArgumentException(\"The specified node does not have an anchor\");\n            }\n\n            if (anchors.ContainsKey(node.Anchor))\n            {\n                throw new DuplicateAnchorException(node.Start, node.End, string.Format(CultureInfo.InvariantCulture, \"The anchor '{0}' already exists\", node.Anchor));\n            }\n\n            anchors.Add(node.Anchor, node);\n        }\n\n        /// <summary>\n        /// Gets the node with the specified anchor.\n        /// </summary>\n        /// <param name=\"anchor\">The anchor.</param>\n        /// <param name=\"throwException\">if set to <c>true</c>, the method should throw an exception if there is no node with that anchor.</param>\n        /// <param name=\"start\">The start position.</param>\n        /// <param name=\"end\">The end position.</param>\n        /// <returns></returns>\n        public YamlNode GetNode(string anchor, bool throwException, Mark start, Mark end)\n        {\n            YamlNode target;\n            if (anchors.TryGetValue(anchor, out target))\n            {","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Yaml/Serialization/DocumentLoadingState.cs#L55-L91","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Rename duplicate anchors in the YAML document so every &name is unique","Remove the second anchor declaration if it is not referenced by any alias","Preprocess/validate the document before loading to detect duplicate anchor names","If documents are merged, namespace the anchors per document before concatenating"],"exampleFix":"// before\nitem: &x {a: 1}\nother: &x {b: 2}\n// after\nitem: &x {a: 1}\nother: &y {b: 2}","handlingStrategy":"validation","validationCode":"// before load: scan the YAML text for anchor definitions and assert uniqueness\nvar names = Regex.Matches(yaml, \"&([A-Za-z0-9_-]+)\").Select(m => m.Groups[1].Value);\nif (names.GroupBy(n => n).Any(g => g.Count() > 1)) throw new InvalidDataException(\"Duplicate YAML anchors\");","typeGuard":null,"tryCatchPattern":"try { return serializer.Deserialize(stream, typeof(Config)); }\ncatch (DuplicateAnchorException ex) { throw new InvalidDataException($\"Duplicate anchor in YAML at {ex.Start}\", ex); }","preventionTips":["Keep anchor names unique within each document","Namespace anchors when merging/template-generating YAML","Lint YAML documents for duplicate anchors in CI","Avoid copy-pasting anchored nodes without renaming"],"tags":["yaml","anchors","parsing"],"backgroundTag":"yaml-parse-error","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}