yq icon indicating copy to clipboard operation
yq copied to clipboard

`sort_keys` treats content above first key differently

Open snuggie12 opened this issue 2 years ago • 1 comments
trafficstars

Describe the bug I was trying to sort a heavily commented yaml file and noticed content above the first key is not associated with the first key. It appears as if the content is simply not touched at all as I was going to make a feature request regarding not removing blank lines and noticed if you insert a blank line above the first key it is unaltered.

Version of yq: 4.33.3 Operating system: mac Installed via: brew

Input Yaml foo.yaml:

# Overall comment unrelated to first key

# Comment related to first key
zfirstKey:
  asubKey: foo

# Comment related to second key
secondKey:
  subKey: bar

# Comment related to third key
thirdKey:
  subKey: baz

Command The command you ran:

yq 'sort_keys(..)' foo.yaml

Actual behavior

# Overall comment unrelated to first key

# Comment related to first key
# Comment related to second key
secondKey:
  subKey: bar
# Comment related to third key
thirdKey:
  subKey: baz
zfirstKey:
  asubKey: foo

Expected behavior I'm unsure what to do about the top comment, but I expect the comment for the first key to follow it

# Overall comment unrelated to first key

# Comment related to second key
secondKey:
  subKey: bar
# Comment related to third key
thirdKey:
  subKey: baz
# Comment related to first key
zfirstKey:
  asubKey: foo

Additional context

snuggie12 avatar Oct 18 '23 15:10 snuggie12

Yep - yq intentionally leaves as much of the document comments untouched (this was done from a bunch of issues people raised where it would mess up their nicely formatted document comment) - and a side effect of this is that it doesn't recognise " Comment related to first key" is separate from "Overall comment unrelated to first key".

mikefarah avatar Oct 26 '23 01:10 mikefarah