From 8c216cc8ec05bb19908df303f342ccaca82017f8 Mon Sep 17 00:00:00 2001 From: yuyongyan29-dev <286965556+yuyongyan29-dev@users.noreply.github.com> Date: Sat, 28 Nov 2026 15:31:24 +0800 Subject: [PATCH] Don't add a line to keep scalars before comments When a document ends with a keep ('+') block scalar and has a foot comment, the emitter wrote an extra blank line to separate the comment. That line was read back as part of the scalar, so every Node round trip appended another newline to the value: key: |+ text # trail decoded "text\n\n" but re-encoded as "text\n\n\n". Skip the forced document foot separation when the last output was a keep block scalar; its trailing breaks already separate the comment. --- internal/libyaml/emitter.go | 16 ++++++++++++++-- testdata/node.yaml | 8 ++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/internal/libyaml/emitter.go b/internal/libyaml/emitter.go index 6179474b..79d7b519 100644 --- a/internal/libyaml/emitter.go +++ b/internal/libyaml/emitter.go @@ -133,6 +133,10 @@ type Emitter struct { space_above bool // Is there's an empty line above? foot_indent int // The indent used to write the foot comment above, or -1 if none. + // [Go] Was the last output the content of a keep ('+') block scalar? + // Its trailing line breaks are part of the value. + keep_breaks bool + // Anchor analysis. anchor_data struct { anchor []byte // The anchor value. @@ -584,8 +588,13 @@ func (emitter *Emitter) emitDocumentEnd(event *Event) error { Message: "expected DOCUMENT-END", } } - // [Go] Force document foot separation. - emitter.foot_indent = 0 + // [Go] Force document foot separation, unless a keep block scalar + // already ends the document: an extra line break would be read back + // as part of its value. + if !emitter.keep_breaks { + emitter.foot_indent = 0 + } + emitter.keep_breaks = false if err := emitter.processFootComment(); err != nil { return err } @@ -1296,6 +1305,7 @@ func (emitter *Emitter) processFootComment() error { return err } emitter.FootComment = emitter.FootComment[:0] + emitter.keep_breaks = false emitter.foot_indent = emitter.indent if emitter.foot_indent < 0 { emitter.foot_indent = 0 @@ -1742,6 +1752,7 @@ func (emitter *Emitter) writeIndicator(indicator []byte, need_whitespace, is_whi emitter.whitespace = is_whitespace emitter.indention = (emitter.indention && is_indention) emitter.OpenEnded = false + emitter.keep_breaks = false return nil } @@ -2150,6 +2161,7 @@ func (emitter *Emitter) writeBlockScalarHints(value []byte) error { return err } } + emitter.keep_breaks = chomp_hint[0] == '+' return nil } diff --git a/testdata/node.yaml b/testdata/node.yaml index 29590a6d..e82277f3 100644 --- a/testdata/node.yaml +++ b/testdata/node.yaml @@ -2112,6 +2112,14 @@ * first item * second item +- node-roundtrip: + name: literal keep before trailing comment + yaml: "key: |+\n text\n\n# trail\n" + +- node-roundtrip: + name: folded keep before trailing comment + yaml: "key: >+\n text\n\n\n# trail\n" + - node-test: name: folded long more-indented line yaml: |