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

Why is yq putting multiline separator indicator (|-) to complex object replacements? #2025

Open
Skarlso opened this issue May 2, 2024 · 7 comments
Labels

Comments

@Skarlso
Copy link

Skarlso commented May 2, 2024

Describe the bug
OSX

Version: v4.40.5

cat test.yaml
test:
  this: cool
➜ yq -i '.test.this = "bla: bla\ntwice: bla"' test.yaml
➜ cat test.yaml
test:
  this: |-
    bla: bla
    twice: bla

Why did the |- get there and how can I remove it?

@Skarlso Skarlso changed the title Why is yq putting multiline separator indicator to complex object replacements? Why is yq putting multiline separator indicator (|-) to complex object replacements? May 2, 2024
@nicktimko
Copy link

The program is pretty-printing the equivalent string with the newline. See https://yaml-multiline.info/ for how the multiline marks work, but the actual value is identical.

$ echo '{"string": "x\\ny"}' | yq -P
string: |-
  x
  y


$ echo '{"string": "x\\ny"}' | yq -P | yq -ojson
{
  "string": "x\ny"
}

If you have jq output the raw string, you see it actually includes a newline (0x0A)

$ echo '{"string": "x\\ny"}' | jq -r .string | xxd
00000000: 780a 790a                                x.y.

If you want an actual \n in the string and not a newline, it seems like a bug that yq won't accept the backslash escape (which would eat up the backslashes for the newline escape):

$ # yq ignores \\ and instead translates \n into 0x0A, so the true output has a backslash + newline
$ echo '{"string": "x"}' | yq '.string = "x\\ny"' | yq -r .string | xxd
00000000: 785c 0a79 0a                             x\.y.

$ # jq accepts \\n and puts literal '\n' in the output
$ echo '{"string": "x"}' | jq '.string = "x\\ny"' | jq -r .string | xxd
00000000: 785c 6e79 0a                             x\ny.

@nicktimko
Copy link

Maybe related: #1692 #1814

@Skarlso
Copy link
Author

Skarlso commented May 2, 2024

Bummer. Thanks for the info @nicktimko!

@nicktimko
Copy link

@Skarlso did you intend to put the escaped string into the YAML (it contains the characters \ and n, not a newline)?

@Skarlso
Copy link
Author

Skarlso commented May 2, 2024

Yes, that's an object. Something like

bla: value1
bla2: value2

Which is the replacement. That should be translated into a newline I believe?

If I would actually try and do a multi-line string I would get a weird |2 there.

yq -i '.test.this |= "    bla: bla
    twice: bla
"' test.yaml
➜  SAP cat test.yaml
test:
  this: |2
        bla: bla
        twice: bla

@Skarlso
Copy link
Author

Skarlso commented May 2, 2024

Ah sorry, my bad, too many spaces. This is the equivalent.

yq -i '.test.this |= "bla: bla
twice: bla
"' test.yaml
➜  SAP cat test.yaml
test:
  this: |
    bla: bla
    twice: bla

Still got | in there...

@mikefarah
Copy link
Owner

The | |- and other variations are how multi line strings are encoded in yaml. Unfortunately, the underlying yaml encoder does not give me many controls to be able to force particular variations; and sometimes it 'auto' formats things :(

It's a known issue against go-yaml; and there are a bunch of tickets against it - but no one has worked on that repo for many months now :(

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

3 participants