google / yamlfmt

An extensible command line tool or library to format yaml files.
Apache License 2.0
1.18k stars 43 forks source link

`yamlfmt` Fails to Parse Block Scalars with Initial Newline #213

Open marcoroth opened 1 week ago

marcoroth commented 1 week ago

yamlfmt encounters an error when parsing YAML files that contain block scalars (|-) with an initial newline after the block scalar indicator. This results in a parsing failure with the following error message:

❯ yamlfmt file.yml
encountered the following formatting errors:
file.yml: yaml: line 3: did not find expected key

The issue occurs when the YAML content is formatted as follows:

# file.yml

- id: item-1
  description: |-

    A lot of 

    multi-line

    copy here.

However, if the initial newline after the |- is removed, the cotent is getting parsed correctly:

# file.yml

- id: item-1
  description: |-
    A lot of 

   multi-line

   copy here.

Expected Behavior

yamlfmt should correctly parse and format YAML content that includes an initial newline after a block scalar indicator (|-). As far as I can tell this seems to be supported in YAML.

Steps to Reproduce

  1. Create a YAML file (file.yml) with the following content:

    - id: item-1
     description: |-
    
       A lot of 
    
      multi-line
    
      text here.
  2. Run yamlfmt on the file:

    yamlfmt file.yml
  3. Observe the parsing error:

    encountered the following formatting errors:
    file.yml: yaml: line 3: did not find expected key
braydonk commented 6 days ago

Hi @marcoroth thanks for opening an issue.

These types of newline nuances are fraught with complications, and I've lost count of the number of times over the years I've tried to fix them and come up short. This is probably going to fall into the same bucket of problems that it's unlikely I'm going to fix any time soon. Thank you for the additional data, it's good to know more edge cases where the library screws this up. I can't promise this will be fixed any time soon though, sorry.

marcoroth commented 6 days ago

Hey @braydonk, thanks for the reply and the openness - this goes a long way. I can work around it for now, thank you!