mikefarah / yq

yq is a portable command-line YAML, JSON, XML, CSV, TOML and properties processor
https://mikefarah.gitbook.io/yq/
MIT License
12.33k stars 602 forks source link

`evaluate` command moves comments to a different line #2176

Open guymeron opened 1 month ago

guymeron commented 1 month ago

Describe the bug evaluate command moves comments to a different line

Version of yq: 4.44.3 Operating system: mac Installed via: homebrew

Input Yaml data1.yml:

line1: "aaa"
line2:
  - name: foo
    array:
      # comment1
      - key1: val1
        key2: val2
      # comment2

      # comment3
      - key1: val10
        key2: val20

Command

cat data1.yml |  yq eval .line2

Actual behavior

- name: foo
  array:
    # comment1
    - key1: val1
      key2: val2
    # comment3
    - key1: val10
      # comment2

      key2: val20

Expected behavior

- name: foo
  array:
    # comment1
    - key1: val1
      key2: val2
    # comment2

    # comment3
    - key1: val10        
      key2: val20

Additional context It works OK when there is no empty line between the comments (line number 9).

felicijus commented 1 week ago

I have a similar Issue, if there are multiple comment line a blank line is inserted.

global:
    # BUG: Elasticsearch does not support network file systems (NFS). Usage of NFS may cause data corruption.

    # NOTE: xxxxx by default, use 'xxxxx' instead
    defaultStorageClass: "xxx" # TEMPLATE: default "storageClass: """

should still be:

global:
    # BUG: Elasticsearch does not support network file systems (NFS). Usage of NFS may cause data corruption.
    # NOTE: xxxxx by default, use 'xxxxx' instead
    defaultStorageClass: "xxx" # TEMPLATE: default "storageClass: """

Edit: I tested a bit around and this part of the documentation does not work https://mikefarah.gitbook.io/yq/operators/comment-operators#get-head-comment