google / yamlfmt

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

bug: nested arrays are flattened #167

Open timruffles opened 4 months ago

timruffles commented 4 months ago

nested arrays are flattened:

$ cat test.yml
- - one
  - two
$ yamlfmt -formatter indent=4 test.yml
$ git diff
diff --git a/test.yml b/test.yml
index 9ff9c3253f..8c71a9deb8 100644
--- a/test.yml
+++ b/test.yml
@@ -1,2 +1,3 @@
-- - one
-  - two
+-
+- one
+- two
$ cat test.yml
-
- one
- two

tested with v0.11.0

braydonk commented 4 months ago

Thanks for opening an issue! Unfortunately this falls under problems with the underlying yaml parser, which I can't easily address in the near future for reasons explained in this post.

timruffles commented 4 months ago

I think in this case yaml/v3 parses it fine:

package main

import (
    "fmt"

    "gopkg.in/yaml.v3"
)

const input = `
- - one
  - two`

func main() {
    var parsed any
    err := yaml.Unmarshal([]byte(input), &parsed)
    fmt.Println(parsed, err) // [[one two]] <nil>
}

digging into a parse into a yaml.Node, we get the AST you'd expect: Document > Sequence > Sequence

Screenshot 2024-04-10 at 14 49 34

braydonk commented 4 months ago

To clarify, the issues I have tagged with this label are all around the way the yaml library is used. It isn't necessarily designed for the way yamlfmt uses it, which is putting yaml in and right back out, so there are a number of cases where the emitter decides to output things in weird ways. I've fixed a few of those types of issues in my fork but they are tricky and take a lot of time.

den-is commented 3 days ago

wth, this "bug" broke my configuration, thankfully I'm tracking this specific "ansible" configuration via git and was able to notice an issue with flattened array-of-arrays quite quickly. ridiculous it is not yet fixed. apparently, yamlfmt is not used by that many people.

braydonk commented 2 days ago

Having trouble reproducing this at the moment. Ran with the following yaml from the original comment:

- - one
  - two

Using yamlfmt from latest master and from v0.11.0 on both Linux and Windows, the nested arrays were retained on formatting.

braydonk@braydonk:~/Git/yamlfmt$ cat tmp/a.yaml 
- - one
  - two
braydonk@braydonk:~/Git/yamlfmt$ cat tmp/b.yaml
- - one
  - two
braydonk@braydonk:~/Git/yamlfmt$ diff tmp/a.yaml tmp/b.yaml 
braydonk@braydonk:~/Git/yamlfmt$ go run ./cmd/yamlfmt -formatter indent=4 tmp/b.yaml
braydonk@braydonk:~/Git/yamlfmt$ diff tmp/a.yaml tmp/b.yaml
braydonk@braydonk:~/Git/yamlfmt$ 

(same result on Windows)

There must be another detail about the yaml, the system, or the yamlfmt configuration that causes the bug to trigger. Need more information to properly reproduce. @timruffles or @den-is are there any other details you can share about the yaml or other reproduction cases?