Yq: Preserve formatting with in place writing

Created on 15 Jun 2020  路  5Comments  路  Source: mikefarah/yq

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
bug

All 5 comments

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

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.

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)

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

Would be helpful to preserve the original formatting!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jodh-intel picture jodh-intel  路  3Comments

mdubinko picture mdubinko  路  8Comments

brunowego picture brunowego  路  5Comments

mkyc picture mkyc  路  6Comments

curiositycasualty picture curiositycasualty  路  5Comments