Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: nested arrays are flattened #167

Open
timruffles opened this issue Apr 9, 2024 · 5 comments
Open

bug: nested arrays are flattened #167

timruffles opened this issue Apr 9, 2024 · 5 comments
Labels
yaml_v3_problem A bug in the underlying yaml library. These issues are vastly harder to fix.

Comments

@timruffles
Copy link

timruffles commented Apr 9, 2024

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 braydonk added the yaml_v3_problem A bug in the underlying yaml library. These issues are vastly harder to fix. label Apr 10, 2024
@braydonk
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Collaborator

braydonk commented Apr 10, 2024

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
Copy link

den-is commented Sep 3, 2024

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
Copy link
Collaborator

braydonk commented Sep 3, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
yaml_v3_problem A bug in the underlying yaml library. These issues are vastly harder to fix.
Projects
None yet
Development

No branches or pull requests

3 participants