Skip to content

Special code cause IMPOSSIBLE error #646

@fisker

Description

@fisker

Describe the bug
A clear and concise description of what the bug is.

An IMPOSSIBLE error was thrown when parsing the following code

? - Detroit Tigers
  - Chicago cubs
: - 2001-07-23

[New York Yankees, Atlanta Braves]: [2001-07-02, 2001-08-12, 2001-08-14]

To Reproduce
Steps to reproduce the behaviour.

Call the parse function

require('yaml').parse(fs.readFileSync('./test.yaml','utf8'))
Uncaught YAMLParseError: Map comment with trailing content at line 5, column 1:


[New York Yankees, Atlanta Braves]: [2001-07-02, 2001-08-12, 2001-08-14]
^

    at Composer.onError (<CWD>\node_modules\yaml\dist\compose\composer.js:70:34)
    at Object.resolveBlockMap (<CWD>\node_modules\yaml\dist\compose\resolve-block-map.js:112:9)
    at resolveCollection (<CWD>\node_modules\yaml\dist\compose\compose-collection.js:13:27)
    at Object.composeCollection (<CWD>\node_modules\yaml\dist\compose\compose-collection.js:59:16)
    at Object.composeNode (<CWD>\node_modules\yaml\dist\compose\compose-node.js:33:38)
    at Object.composeDoc (<CWD>\node_modules\yaml\dist\compose\compose-doc.js:35:23)
    at Composer.next (<CWD>\node_modules\yaml\dist\compose\composer.js:150:40)
    at next (<anonymous>)
    at Composer.compose (\node_modules\yaml\dist\compose\composer.js:132:25) {
  code: 'IMPOSSIBLE',
  pos: [ 52, 53 ],
  linePos: [ { line: 5, col: 1 }, { line: 5, col: 2 } ]

Expected behaviour
A clear and concise description of what you expected to happen.

Not sure

Versions (please complete the following information):

  • Environment: Node.js v24
  • yaml: 2.8.2

Additional context
Add any other context about the problem here.

This code is from Prettier test suite, and we are upgrading the yaml package prettier/prettier#18419 , the original code is

? - Detroit Tigers
  - Chicago cubs
:
  - 2001-07-23

? [ New York Yankees,
    Atlanta Braves ]
: [ 2001-07-02, 2001-08-12,
    2001-08-14 ]

Prettier format to the code above, and it's become unparseable. Link

This code seems to come from an early version of YAML spec, but I'm not sure.

Update: Found, it's "Example 2.11 Mapping between Sequences" from YAML spec 1.2.2

I'm not sure if it's valid code or not.

But the docs say

This really should not happen. If you encounter this error code, please file a bug.

So I opened.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions