Skip to content

End block scalars at document markers - #495

Open
chinmayajha wants to merge 2 commits into
yaml:mainfrom
chinmayajha:block-scalar-document-marker
Open

chinmayajha wants to merge 2 commits into
yaml:mainfrom
chinmayajha:block-scalar-document-marker

Conversation

@chinmayajha

@chinmayajha chinmayajha commented Nov 7, 2026 •

Copy link
Copy Markdown

A block scalar whose content starts at column 1 (a top-level --- | or --- >) runs straight through a --- or ... line and eats the rest of the stream:

$ printf -- '--- |\nfoo\n...\n--- x\n' | go-yaml -j -o all
"foo\n...\n--- x\n"

Expected "foo\n" and "x". The spec doesn't allow document markers inside block scalar content (c-forbidden, Example 9.5 / YTS W4TN). The plain and quoted scalar scanners already stop at a marker; the block scalar loop never checked for one.

This adds the check to scanBlockScalar and moves the existing inline check from the plain and quoted scanners into a small isDocumentIndicator helper so all three use the same one. Nested block scalars can't have content at column 1, so only top-level ones are affected. Lines like ----, ...x or --- are still content.

Two scanner tests added (literal + ..., folded + ---), plus tests for isDocumentIndicator. The scanner tests fail on main and pass here; go test ./... passes. W4TN stays in known-failing-tests because it starts with %YAML 1.2, which the parser still rejects. That change will come as a separate PR after this one.

Written with AI assistance (Claude); I have reviewed the change.

Comment thread internal/libyaml/scanner.go Resolved

@ccoVeille ccoVeille left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate you used go-yaml and our YAML test suite.

I feel like the fix is good.

@ccoVeille
ccoVeille requested a review from ingydotnet November 7, 2026 07:51
@ccoVeille

This comment has been minimized.

@chinmayajha

This comment has been minimized.

Comment thread internal/libyaml/plugin_parser.go Resolved
@chinmayajha
chinmayajha force-pushed the block-scalar-document-marker branch from 7129c54 to d1be72c Compare November 8, 2026 07:45

@yxshee yxshee left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran this merged onto main 2008d62 (head d1be72c) through a structural comparison with libyaml (PyYAML's C loader) and the yaml npm package on 60,000 generated documents built from block scalars, document markers, directives, flow collections and anchors. go test ./... passes with the YAML Test Suite data.

On the documents where libyaml and the yaml package agree, go-yaml differed from them on 956 before the change and on 931 after it. The 25 that now match include |+\n---\n (two documents instead of a scalar containing ---), |-\n...\n and >\n---\nb: |\n t\n, and none that matched before stopped matching. Of the 14 other changed results, 10 now match the yaml package where libyaml rejects the document (e.g. |+\n...\n- a gives "" then [a]), 3 now fail like libyaml (|+\n---\n*x and the like, where *x refers to no anchor), and one, \n...\n|+\n---\n, matches neither before or after because go-yaml does not emit the empty document before the leading .... So I could not find an input that gets worse.

@chinmayajha

Copy link
Copy Markdown
Author

@yxshee thanks for running this, that is a much wider check than the YAML Test Suite cases I used. The \n...\n|+\n---\n case is the same before and after, since it comes from how a leading ... is handled and not from where a block scalar ends, so I kept it out of this PR. cc @ccoVeille

@ccoVeille

ccoVeille commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

I ran this merged onto main 2008d62 (head d1be72c) through a structural comparison with libyaml (PyYAML's C loader) and the yaml npm package on 60,000 generated documents built from block scalars, document markers, directives, flow collections and anchors. go test ./... passes with the YAML Test Suite data.

@yxshee Interesting, can you share how you generate or fetch these YAML documents to run your tests?

This reminds me about something @Fredi told me about what he did for his own go-yaml package

https://github.com/go-openapi/go-yaml

@chinmayajha

Copy link
Copy Markdown
Author

@ingydotnet this is approved by ccoVeille, and a 60k-document differential check found no regressions. Could you take a look and merge it if it looks right?

@chinmayajha
chinmayajha force-pushed the block-scalar-document-marker branch from d1be72c to 81f9ef3 Compare November 25, 2026 09:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants