goccy / go-yaml

YAML support for the Go language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Folded strings lose the last character if there is no trailing whitespace

charithe opened this issue · comments

Describe the bug

If the last character of a folded string is the end of the file and there's nothing else following it, the parsed token's Value field doesn't contain that last character.

To Reproduce

This is a strange case. Apparently, some editors strip the line feed character from the last line of the file even if that line is not empty. I am going to illustrate it with $ to denote the invisible line feed characters (ASCII 0a).

Normally, the file contents would look like the following and foo is correctly parsed as bar.

foo: >$
  bar$

However, the following YAML file produces ba as the value of foo. Note that there's no line feed following bar. Your editor would normally add the line feed automatically so you need to remove it by running truncate -s -1 file.yaml to reproduce this.

foo: >$
  bar

Expected behavior

Folded strings should not be truncated if there's no trailing whitespace.

Version Variables

  • Go version: 1.22.1
  • go-yaml's Version: 1.11.3 and master

Additional context

I have a patch ready and can raise a PR to fix this.