google / yamlfmt

An extensible command line tool or library to format yaml files.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bug: nested arrays are flattened

timruffles opened this issue · comments

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

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.

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

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.