Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trailing space in string block transforms to quoted form #1277

Open
Ri0n opened this issue Jul 21, 2022 · 7 comments
Open

Trailing space in string block transforms to quoted form #1277

Ri0n opened this issue Jul 21, 2022 · 7 comments
Labels

Comments

@Ri0n
Copy link

Ri0n commented Jul 21, 2022

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.

@mikefarah
Copy link
Owner

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

@Ri0n
Copy link
Author

Ri0n commented Jul 22, 2022

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.

@mikefarah
Copy link
Owner

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 mikefarah changed the title yq ignores literal style Trailing space in string block transforms to quoted form Jul 23, 2022
@mikefarah
Copy link
Owner

FYI this depends on a fix for go-yaml/yaml#880

@ericdstein
Copy link

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.

@mikefarah
Copy link
Owner

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

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

@bryant-ferguson
Copy link

Duplicate of #566

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants