Skip to content

Multiline String with Trailing Spaces Gets Converted to Single-Line String #2324

@pteroctopus

Description

@pteroctopus

Describe the bug
When modifying a YAML file using yq eval with in-place editing, multiline strings that contain trailing spaces are incorrectly converted to single-line strings.

Version of yq: yq (https://github.com/mikefarah/yq/) version v4.45.1
Operating system: macOS
Installed via: Homebrew

Input YAML

orig.yaml:

hello: world
annotations:
  zero: asdf
  one: |
    This is a multi-line string.
    It has multiple lines.
  two: |
    one more line
    Line with spaces at the end    
    Normal line
  three:
    four: five

Command

yq eval '.annotations.one = ""' -i orig.yaml

Actual behavior

The output orig.yaml is modified as follows:

hello: world
annotations:
  zero: asdf
  one: ""
  two: "one more line\nLine with spaces at the end    \nNormal line\n"
  three:
    four: five

The annotations.two field, which originally used the block scalar |, is converted into a single-line string with explicit \n characters.

Expected behavior

Only annotations.one should be modified, while annotations.two should retain its original formatting:

hello: world
annotations:
  zero: asdf
  one: ""
  two: |
    one more line
    Line with spaces at the end    
    Normal line
  three:
    four: five

Additional context

This issue seems to occur when a multiline string contains trailing spaces. The expected behavior is that unmodified fields retain their original block scalar formatting.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions