Skip to content

pkg/yaml: keep leading line break in block scalars - #337

Open
0z5a wants to merge 1 commit into
google:mainfrom
0z5a:fix/block-scalar-leading-line-break
Open

0z5a wants to merge 1 commit into
google:mainfrom
0z5a:fix/block-scalar-leading-line-break

Conversation

@0z5a

@0z5a 0z5a commented Nov 16, 2026

Copy link
Copy Markdown

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 and text is formatted to a: |2 followed by text, 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):

$ printf 'a: |\n\n  text\n' | yamlfmt -
a: |2
  text

Before formatting, a is "\ntext\n"; after formatting it is "text\n". With this change the output is a: |2, an empty line, then text, 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_comment keeps its behaviour for the other callers, including the pad_line_comments handling.

Tests:

  • pkg/yaml: TestBlockScalarLeadingLineBreak round-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's TestNewLinePreserved).
  • formatters/basic: a TestFormatter case 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) and make integrationtest pass.

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.

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.
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.

1 participant