Repository navigation
Conversation
When a literal or folded block scalar starts with a line break and the header has no line comment, the emitter used that line break to end the header line, so the first empty line of the value was lost. Port the fix for go-yaml/yaml#755 from go.yaml.in/yaml/v3: write the header line break separately when there is no line comment.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a literal (
|) or folded (>) block scalar value starts with a line break, the emitter writes the indentation indicator for it. But it then uses that line break to end the header line, so the first empty line of the value is lost. With the default config,a: |followed by an empty line andtextis formatted toa: |2followed bytext, which changes the value from"\ntext\n"to"text\n". With two leading empty lines, one is kept and one is dropped. The same happens for>and for|+values that are only a line break (YAML spec Example 8.6:keep: |+followed by one empty line becomes""). The bug does not show when the header has a line comment, because the comment writer ends the line itself.Reproduction with the default config (yamlfmt main at dea0b79):
Before formatting,
ais"\ntext\n"; after formatting it is"text\n". With this change the output isa: |2, an empty line, thentext, and the value is unchanged.This ports the upstream fix for go-yaml/yaml#755, which is in
go.yaml.in/yaml/v3.yaml_emitter_process_line_comment_linebreak(emitter, true)writes the line break that ends the header when there is no line comment, and the literal and folded writers now call it.yaml_emitter_process_line_commentkeeps its behaviour for the other callers, including thepad_line_commentshandling.Tests:
pkg/yaml:TestBlockScalarLeadingLineBreakround-trips"\nb\n","\n\nb\n"and"\n"in literal and folded style, and checks the exact output for a literal (same expectation as upstream'sTestNewLinePreserved).formatters/basic: aTestFormattercase with the default config,a: |+ empty line +b, which must keep the empty line.Both new tests fail on main and pass with this change.
go test ./...,go vet(as in the Makefile) andmake integrationtestpass.I also formatted all valid inputs of the yaml-test-suite with the default config and compared the parsed value before and after. Five cases that changed value on main now keep it, including spec Examples 8.2 and 8.6, and no case got worse. An encoder benchmark with 6000 block scalars showed no measurable difference: median 14.3 ms before and 13.6 ms after over 12 interleaved runs, with the run-to-run spread larger than that gap.
This change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described.