Skip to content

Extra newline possible when appending to an array ending in commented out entry #528

Open
@olets

Description

@olets

Describe the bug

When appending to array an extra newline is appended before the new element if the last element in the array is commented out at the indentation level where the - would be.

This does no harm, reporting in case it reflects a parsing bug that is problematic in some other scenario.

version of yq: 3.3.2 (installed w/ Homebrew)
operating system: macOS 10.15.6

Input Yaml
Concise yaml document(s) (as simple as possible to show the bug)

data.yml:

b:
  c: 2
  d:
    - new thing
    # - foo thing

Command
The command you ran:

yq w data.yaml "b.d[+]" "bar thing"

Actual behavior

b:
  c: 2
  d:
    - new thing
    # - foo thing

    - bar thing

Expected behavior

b:
  c: 2
  d:
    - new thing
    # - foo thing
    - bar thing

or

b:
  c: 2
  d:
    - new thing
    - bar thing
    # - foo thing

Additional context

Compare to when the # is in a different position. In all cases the command is the same

b:
  c: 2
  d:
    - new thing
#    - foo thing
% yq w git-replay.yaml "b.d[+]" "bar thing"
b:
  c: 2
  d:
    - new thing
    - bar thing
#    - foo thing

and

b:
  c: 2
  d:
    - new thing
 #   - foo thing
b:
  c: 2
  d:
    - new thing
    - bar thing
#   - foo thing

and

b:
  c: 2
  d:
    - new thing
  #  - foo thing
% yq w git-replay.yaml "b.d[+]" "bar thing"
b:
  c: 2
  d:
    - new thing
    - bar thing
  #  - foo thing

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions