stride3d/stride · error · YamlException

Duplicate key

Error message

Duplicate key

What it means

YamlMappingNode's constructor throws this while building the mapping when adding a child whose key already exists (children.Add raises ArgumentException, converted to a YamlException positioned at the duplicate key node). YAML technically allows duplicate keys but this library treats them as invalid.

Solutions

  1. Remove or rename the duplicate key in the YAML file — the exception location points at the second occurrence.
  2. Use YAML merge keys (<<: *anchor) instead of duplicating keys when combining sections.
  3. Add a pre-parse lint check (yamllint with duplicate-key rule) to CI.
  4. Fix the generator/template that emits the YAML so keys are unique.

Example fix

// before
name: first
name: second
// after
name: second
Defensive patterns

Strategy: validation

Validate before calling

// Detect duplicate top-level keys before loading (example with YamlDotNet-style scanner)
var keys = new HashSet<string>();
foreach (var key in EnumerateMappingKeys(yamlText))
    if (!keys.Add(key)) throw new InvalidDataException($"Duplicate YAML key '{key}'");

Try / catch

try { return YamlMappingNode.FromEvents(events); } catch (YamlException ex) when (ex.Message == "Duplicate key") { throw new InvalidDataException($"Duplicate mapping key at {ex.Start}; remove or rename the repeated key", ex); }

Prevention

When it happens

Trigger: Loading a YAML mapping that contains the same key twice at the same level, e.g. 'name: a' followed by 'name: b'; duplicate keys generated by templating or by concatenating config files.

Common situations: Merged/concatenated YAML config files where both halves define the same key; hand-edited YAML with an accidental repeated key; generated YAML (template loops) emitting the same key multiple times; strict parsing after migration from a laxer YAML tool.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core.Yaml/Serialization/YamlMappingNode.cs:95

        internal YamlMappingNode(EventReader events, DocumentLoadingState state)
        {
            MappingStart mapping = events.Expect<MappingStart>();
            Load(mapping, state);
            Style = mapping.Style;

            bool hasUnresolvedAliases = false;
            while (!events.Accept<MappingEnd>())
            {
                YamlNode key = ParseNode(events, state);
                YamlNode value = ParseNode(events, state);

                try
                {
                    children.Add(key, value);
                }
                catch (ArgumentException err)
                {
                    throw new YamlException(key.Start, key.End, "Duplicate key", err);
                }

                hasUnresolvedAliases |= key is YamlAliasNode || value is YamlAliasNode;
            }

            if (hasUnresolvedAliases)
            {
                state.AddNodeWithUnresolvedAliases(this);
            }
#if DEBUG
            else
            {
                foreach (var child in children)
                {
                    if (child.Key is YamlAliasNode)
                    {
                        throw new InvalidOperationException("Error in alias resolution.");
                    }

View on GitHub (pinned to 96fad776d2)