Skip to content

yq loses comments around merge‑tags when used in an ireduce pipeline #2516

@d-enk

Description

@d-enk

📄 Description

When a YAML document that contains merge‑tags << is processed with the irreduce operator, comments that precede the merge‑key are removed from the output.

In contrast, a simple yq '.' file.yaml (or yq '. * {}' file.yaml) preserves all comments in the file. Thus the issue appears only when a merge‑key is part of an irreduce operation.


🛠️ Reproduction

# file.yaml
values: &values
  value: 1
v1: { <<: *values } # COMMENT
v2:
  # COMMENT
  <<: *values
  # COMMENT
v3:
  a: 1
  # COMMENT
  <<: *values
  v: # COMMENT

Run:

# 1️⃣ Simple eval – comments are kept
yq '.' file.yaml # yq '. * {}' file.yaml

Output:

values: &values
  value: 1
v1: {!!merge <<: *values} # COMMENT
v2:
  # COMMENT
  !!merge <<: *values
  # COMMENT
v3:
  a: 1
  # COMMENT
  !!merge <<: *values
  v: # COMMENT

Now run the ireduce pipeline:

# 2️⃣ ireduce – comments around << are lost
yq '. as $item ireduce ({}; . * $item)' file.yaml

Output:

values: &values
  value: 1
v1:
  <<: *values
v2: # COMMENT
  <<: *values
v3:
  a: 1
  <<: *values
  v: # COMMENT

✅ Expected Behaviour

The merge‑operator should behave like a normal node for the purposes of comment preservation. The output of the ireduce pipeline should be identical to the input, except that the mapping merge has been applied.


❌ Actual Behaviour

  • Comments that precede a merge‑key are dropped.
  • The comment that appears on the same line as the key (v1) is moved to the key line.
  • Comments that follow a key (e.g., v:) are preserved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions