Skip to content

Change in handling of anchors and document merging #2502

@ross211

Description

@ross211

Describe the bug
From v4.46.1 the merged output of documents with anchors has changed.

Version of yq: 4.46.1+
Operating system: mac + linux
Installed via: homebrew + github binaries

Input Yaml
input.yml:

map1: &map
  key1: true
map2:
 <<: *map
map3:
 <<: *map
 addkey: true
map4: *map
---
map1:
  key1: replace
  newkey: true

v4.45.4
In v4.45.4 you can do the explode before or after the document merge and get the same expected result:

$ ./yq-v4.45.4 eval-all '. as $item ireduce ({}; . * $item ) | explode(.)' input.yaml
map1:
  key1: replace
  newkey: true
map2:
  key1: true
map3:
  key1: true
  addkey: true
map4:
  key1: true

$ ./yq-v4.45.4 eval-all 'explode(.) as $item ireduce ({}; . * $item )' input.yaml
map1:
  key1: replace
  newkey: true
map2:
  key1: true
map3:
  key1: true
  addkey: true
map4:
  key1: true

v4.46.1
In v4.46.1 if you explode before the merge then it mangles the maps:

$ ./yq-v4.46.1 eval-all 'explode(.) as $item ireduce ({}; . * $item )' input.yaml
map1:
  key1: replace
  newkey: true
map2:
  "0": key1
  "1": true
map3:
  "0": key1
  "1": true
  "2": addkey
  "3": true
map4:
  key1: true

$ ./yq-v4.46.1 eval-all '. as $item ireduce ({}; . * $item ) | explode(.)' input.yaml
map1:
  key1: replace
  newkey: true
map2:
  key1: true
map3:
  key1: true
  addkey: true
map4:
  key1: true

v4.48.1
v4.48.1 behaves the same as v4.46.1 but if you set --yaml-fix-merge-anchor-to-spec then it presents another issue:

$ ./yq-v4.48.1 --yaml-fix-merge-anchor-to-spec eval-all 'explode(.) as $item ireduce ({}; . * $item )' input.yaml
map1:
  key1: replace
  newkey: true
map2: {}
map3:
  addkey: true
map4:
  key1: true

$ ./yq-v4.48.1 --yaml-fix-merge-anchor-to-spec eval-all '. as $item ireduce ({}; . * $item ) | explode(.)' input.yaml
map1:
  key1: replace
  newkey: true
map2:
  <<:
    key1: true
map3:
  <<:
    key1: true
  addkey: true
map4:
  key1: true

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions