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

Nested anchors of mappings do not retain order #2189

Open fresh2dev opened 2 weeks ago

fresh2dev commented 2 weeks ago

Describe the bug

Nested anchors of mappings do not retain order.

Version of yq: 4.44.3 Operating system: mac Installed via: binary release

Input Yaml data1.yml:

.global-config: &global-config
  first: foo
  second: bar
  third: baz

.local-config: &local-config
  <<: *global-config
  first: FOO

final-config:
  <<: *local-config

Command The command you ran:

yq 'explode(.)' data1.yml

Actual behavior

---
.global-config:
  first: foo
  second: bar
  third: baz
.local-config:
  second: bar
  third: baz
  first: FOO
final-config:
  second: bar
  third: baz
  first: FOO

Expected behavior

---
.global-config:
  first: foo
  second: bar
  third: baz
.local-config:
  first: FOO
  second: bar
  third: baz
final-config:
  first: FOO
  second: bar
  third: baz

Additional context I tested with Helm using a values.yaml, Python's pyyaml, the Python yq, and each of them do retain order of nested anchors. E.g.,

spec:
  template:
    metadata:
      labels:
        app.kubernetes.io/managed-by: Helm
        first: FOO
        second: bar
        third: baz