yq icon indicating copy to clipboard operation
yq copied to clipboard

Preserve formatting with in place writing

Open tamalsaha opened this issue 4 years ago • 8 comments

Describe the bug yq w -1 command removes blank lines and changes spacing before comments.

Input Yaml Concise yaml document(s) (as simple as possible to show the bug) data1.yml:

# Log level for operator
logLevel: 3

# Annotations applied to operator deployment
annotations: {}

# Annotations passed to operator pod(s).
podAnnotations: {}

# Node labels for pod assignment
nodeSelector:  # +doc-gen:break
  beta.kubernetes.io/os: linux
  beta.kubernetes.io/arch: amd64

Command The command you ran:

yq w -i test.yaml logLevel 4

Actual behavior

# Log level for operator
logLevel: 4
# Annotations applied to operator deployment
annotations: {}
# Annotations passed to operator pod(s).
podAnnotations: {}
# Node labels for pod assignment
nodeSelector: # +doc-gen:break
  beta.kubernetes.io/os: linux
  beta.kubernetes.io/arch: amd64

Expected behavior

# Log level for operator
logLevel: 4

# Annotations applied to operator deployment
annotations: {}

# Annotations passed to operator pod(s).
podAnnotations: {}

# Node labels for pod assignment
nodeSelector:  # +doc-gen:break
  beta.kubernetes.io/os: linux
  beta.kubernetes.io/arch: amd64

Additional context Add any other context about the problem here.

$ yq --version
yq version 3.3.2

tamalsaha avatar Jun 15 '20 01:06 tamalsaha

Yep - this is due to the underlying go-yaml parser, I see someone has already raised an issue there:

https://github.com/go-yaml/yaml/issues/627

mikefarah avatar Jun 15 '20 02:06 mikefarah

Thanks for the confirmation. Something else is also broken between v3.3.0 (working) and v3.3.2. I am running the following command:

yq r api/crds/installer.voyagermesh.com_voyageroperators.v1.yaml spec.versions[0].schema.openAPIV3Schema.properties.spec > /tmp/voyager-values.openapiv3_schema.yaml

You can see the files here: https://gist.github.com/tamalsaha/eeee8d9d2db6623bf1c77717ba3b2c2d

In v3.3.0, the formatting of strings remain unchanged (newlines are preserved). In v3.3.2, the newlines are gone.

tamalsaha avatar Jun 15 '20 02:06 tamalsaha

Ah yeah I updated the go-yaml library in 3.3.1 which had a number of changes and fixes. That is an open issue against the lib (https://github.com/go-yaml/yaml/issues/627)

mikefarah avatar Jun 15 '20 03:06 mikefarah

Looks like go-yaml decided to not fix the issue, rather just document it. :disappointed:

tamalsaha avatar Jun 15 '20 19:06 tamalsaha

Would be helpful to preserve the original formatting!

aakrem avatar Jul 22 '20 06:07 aakrem

It would also be nice to be able to preserve document markers (---). Seems like a great tool but unfortunately not being able to preserve virtually all the formatting is a dealbreaker for us.

twbecker avatar Apr 28 '21 17:04 twbecker

Just wanted to second this, love the tool but also love our blank lines 😁

markzporter avatar Oct 31 '23 13:10 markzporter

It also changes our yml files from 4 spaces indentation to 2 :(

Justinzobel avatar Apr 06 '24 01:04 Justinzobel