yq icon indicating copy to clipboard operation
yq copied to clipboard

Trailing space in string block transforms to quoted form

Open Ri0n opened this issue 3 years ago • 7 comments

Describe the bug I use next expression to update YAML item

yq_linux_amd64 -i '
with(.path.to.item.content ; . = strenv(CONTENTS) | . style="literal")' \
myinputfile.yaml

Where CONTENTS keeps some long bash script. It worked well for me until some recent changes the the bash script.

So if before something went wrong IIRC the content in YAML looked like

   |-
      line1
      line2

Then now it looks like

line1\nline2

basically making it unreadable in the YAML

Version of yq: 4.26.1 Operating system: linux Installed via: binary release

Input Yaml Unfortunately the YAML source and the scrips I use are commercial, so I can't post them here and I haven't tried to find the relevant change myself in hope it's something known.

So basically I want the behavior I had initially.

Ri0n avatar Jul 21 '22 10:07 Ri0n

I appreciate the script is commercial - they often are. There has been no changes that I'm aware of that should produce that effect. Just tried;

CONTENTS="cat
dog" ./yq -n '.x = strenv(CONTENTS)'

and it resulted:

x: |-
  cat
  dog

Unless you give me a representative (non-commercial :) ) example that I can reproduce the problem with - there's little I can do to help. I've got this guide here that may help as well: https://mikefarah.gitbook.io/yq/operators/string-operators#string-blocks-bash-and-newlines

mikefarah avatar Jul 21 '22 22:07 mikefarah

Hi Mike,

I figured it out! One of lines in my bash script had a trailing space. For some reason this make yq ignore the style. So I removed this single trailing space and everything works properly now.

With your example is like putting a space right after cat or dog.

To be honest even if it's a correct behavior it's completely unexpected. And there were no warnings or anything giving a clue the spaces are forbidden with this style.

Ri0n avatar Jul 22 '22 10:07 Ri0n

Ooh I haven't seen that before - that is odd.

A trailing space in a block changes to a quoted string - this even happens when opening a file in that format. yq isn't doing anything to the strings itself, this is an issue with the underlying yaml parse go-yaml :/

mikefarah avatar Jul 23 '22 02:07 mikefarah

FYI this depends on a fix for https://github.com/go-yaml/yaml/issues/880

mikefarah avatar Sep 28 '22 23:09 mikefarah

This issue also occurs with CRLF line endings.

teststring=$(cat <<-END
This is line one.
This is line two.
This is line three.
END
)

dos="$(printf "%s" "$teststring" | unix2dos)"
TEST="$dos" yq -n '.test = strenv(TEST)'

unix="$(printf "%s" "$teststring" | dos2unix)"
TEST="$unix" yq -n '.test = strenv(TEST)'

outputs

test: "This is line one.\r\nThis is line two.\r\nThis is line three."
test: |-
  This is line one.
  This is line two.
  This is line three.

ericdstein avatar Nov 11 '22 23:11 ericdstein

FYI - you can use yq to remove trailing spaces in a string block like so:

yq '.myStringBlock |= sub(" *\n", "\n")' file.yaml

mikefarah avatar Mar 03 '23 00:03 mikefarah

Duplicate of https://github.com/mikefarah/yq/issues/566

bryant-ferguson avatar Sep 08 '23 20:09 bryant-ferguson