yamlfix icon indicating copy to clipboard operation
yamlfix copied to clipboard

Yamlfix generates invalid yaml if comment in a block when top level is a list

Open nonell-viavi opened this issue 10 months ago • 2 comments

First of all, thank you for this fantastic tool!

Now to the issue :)

Description

When the yaml file is organized in a certain way, yamlfix generates invalid YAML as the output.

Steps to reproduce

  1. This is the smallest file that reproduces the issue:
    ---
    - a: |-
      # comment
      content
    
  2. Run yamlfix <file.yaml>

Current behavior

This is the resulting file, which is not valid YAML.

---
- a: |-
      # comment
    content

Desired behavior

I would expect the comment to be left as it is, not shifted right. I think yamlfix is treating the comment as a comment instead of text under the |- block.

The interesting bit is that this only manifests when the top level object is a list! (When top level is a dict, this doesn't happen).

---
a: |-
  # comment
  content

^ This is left as it is.

Environment

$ python -c "import yamlfix.version; print(yamlfix.version.version_info())"
------------------------------------------------------------------
     yamlfix: 1.16.0
     Python: 3.11.8
     Platform: Linux-4.18.0-544.el8.x86_64-x86_64-with-glibc2.28
------------------------------------------------------------------

nonell-viavi avatar Apr 16 '24 15:04 nonell-viavi

another example:

foo:
  bar:
    # asdf
    - asdfasdf

gets turned into this, which is invalid:

---
foo:
  bar:
    # asdf
[asdfasdf]

DetachHead avatar May 29 '24 00:05 DetachHead

Another weird comment handling issue:

config

❯ cat config.toml
# Use double quotes for strings
quote_representation = '"'
# Quote basic values like strings etc.
quote_basic_values = true
# Use multiline `-` style for lists
sequence_style = "block_style"
❯ cat no-comment.yaml
test:
  - one
❯ cat comment.yaml
test:
  - one
# Comment
❯ cat no-comment.yaml| yamlfix -c config.toml -
[+] YamlFix: Fixing files
[+] Fixed <stdin>
---
test:
  - "one"
❯ cat comment.yaml| yamlfix -c config.toml -
[+] YamlFix: Fixing files
[+] Fixed <stdin>
---
test:
  - one
# Comment

When a comment is present, the rule to "quote basic values" is not applied, once the comment is removed - it's all good again

elidhu avatar Jun 11 '24 05:06 elidhu