goccy / go-yaml

YAML support for the Go language
MIT License
1.12k stars 129 forks source link

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

Open charithe opened 6 months ago

charithe commented 6 months ago

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

Additional context

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