Special code cause `IMPOSSIBLE` error
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 https://github.com/prettier/prettier/pull/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.
Thank you for filing the issue! I think/hope this might be the first time someone's hit an IMPOSSIBLE error.
The bug is somewhere around here in the CST parser. The minimal case appears to be something like:
1: 2
[3]: 4
where a flow collection is used as a mapping key and it has either an empty line or a comment before it; in that case the CST collection ends up with an extra empty/comment-only item in it, rather than merging that into the start of the next item like it should. Empty items like that should only show up at the very end of a CST collection, and so the compiler fails with the error you see when processing it.
I'll need to continue investigating a bit later (hopefully next week sometime), so leaving this note to confirm the problem and to help me return to it later. In the meantime, anyone else interested is quite welcome to dig into this as well...
Another suggestion,
Maybe the message in the IMPOSSIBLE error can add a link to ask the user to report.
When I saw this error I was thinking maybe the spec changed, and try to make changes to Prettier, until I checked the errorCode and checked 08_errors.md.