yaml icon indicating copy to clipboard operation
yaml copied to clipboard

parseDocument fails to parse arrays correctly

Open eyalroth opened this issue 3 months ago • 3 comments

Describe the bug

parseDocument not parsing some arrays correctly, for instance:

A: 1
B: [ {
  "foo": "bar"
} ]
C: 3

In this example, C will not appear in the parsed document contents

Additionally, multiple errors will appear:

  • YAMLParseError: Flow map in block collection must be sufficiently indented and end with a } at
  • YAMLParseError: Flow sequence in block collection must be sufficiently indented and end with a ]
  • YAMLParseError: Unexpected flow-map-end token in YAML stream: "}"
  • YAMLParseError: Unexpected flow-seq-end token in YAML stream: "]"
  • YAMLParseError: Unexpected scalar token in YAML stream: "C"
  • YAMLParseError: Unexpected map-value-ind token in YAML stream: ":"
  • YAMLParseError: Unexpected scalar token in YAML stream:

To Reproduce

import util from 'node:util';
import { parseDocument } from 'yaml';

const raw = `A: 1\nB: [ {\n  "foo": "bar"\n} ]\nC: 3`;

const doc = parseDocument(raw);

console.log(doc.errors) // many errors

console.log(util.inspect(doc.contents, { depth: null })); // C:3 is missing

Expected behaviour

The YAML from the example, which is a valid YAML document, should be parsed correctly and without errors.

Versions (please complete the following information):

  • Environment: NodeJS 22.13.1
  • yaml: 2.6.0, 2.8.1

eyalroth avatar Aug 28 '25 13:08 eyalroth

The example is not a valid yaml document. As the first error indicates, the } is insufficiently indented.

Values in block mappings must be more indented than their key. There is a specific relaxation for the closing indicator of a single flow collection being allowed on the same indent level as the key, but this is not extended to multiple closing indicators, as you have here.

eemeli avatar Aug 29 '25 03:08 eemeli

@eemeli hmm I do see now that there's inconsistency between different tools regarding this behavior.

Just FYI my use-case involves AWS's CloudFormation templates, which accept this pattern.

eyalroth avatar Aug 31 '25 10:08 eyalroth

I agree that there's incosistency in how different YAML libraries deal with this, but the spec isn't, really, even if it's not explicitly obvious that the +1 in the ns-l-block-map-entry(n+1+m) part of the rule for l+block-mapping(n) leads to this.

eemeli avatar Sep 01 '25 07:09 eemeli