Skip to content

The incorrect item is deleted from a slice after using add, flatten, sort, reverse or shuffle operators on a sequence. #2387

Open
@antoinedeschenes

Description

@antoinedeschenes

Describe the bug

The incorrect item is deleted from a slice after using add, flatten, sort, reverse or shuffle operators on a sequence.

Version of yq: 4.45.4, problem started with 4.40.1
Operating system: mac
Installed via:binary release

Input

data1.xml:

<items>
  <foo>
    <id>1</id>
  </foo>
</items>

data2.yml:

items:
  foo:
    id: 1

Command
The command you ran:

yq '.items.foo |= ([] + .) | del(.items.foo[] | select(.id==1))' data1.xml
yq '.items.foo |= ([] + .) | del(.items.foo[] | select(.id==1))' data2.yml

Actual behavior
for data1.xml

<items>
  <foo>
    <id>1</id>
  </foo>
</items>

for data2.yml

items:
  foo:
    - id: 1

Expected behavior

for data1.xml

<items></items>

for data2.yml

items:
  foo: []

Additional context

If I run the command with yq -v, I notice the node's path is printed as Pfoo.foo rather than Pfoo[0].

I've been looking at the code around addSequence and it looks like slice elements from the RHS aren't getting the index key assigned since the CandidateNode alerady had a Key set.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions