Skip to content

yq producing invalid yaml removing all items from a list with comment #2600

@hottea773

Description

@hottea773

Describe the bug
yq is producing invalid yaml removing all items from a list with an inline comment on the list key

Version of yq: yq (https://github.com/mikefarah/yq/) version v4.50.1
Operating system: linux -> Ubuntu 22.04.5 LTS
Installed via: apt

Input Yaml
Concise yaml document(s) (as simple as possible to show the bug, please keep it to 10 lines or less)
test.yaml:

testList: # A comment
  - name: test1
    value: 123
  - name: test2
    value: 456

Command
The command you ran:

yq 'del(.testList[])' test.yaml

Actual behaviour

testList: # A comment
[]

which is invalid:

$ yq 'del(.testList[])' test.yaml | yq .
Error: bad file '-': yaml: line 2: could not find expected ':'

Expected behaviour

Valid yaml for the empty list.
I'd have thought the following would be best:

testList: [] # A comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions