google / yamlfmt

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Panic on formatter options through cli args

badouralix opened this issue · comments

When running yamlfmt without any .yamlfmt, the --formatter flag may lead to a panic :

$ rm .yamlfmt

$ yamlfmt --formatter retain_line_breaks=true
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x119228f]
goroutine 1 [running]:
main.makeCommandConfigFromData(0x12156e0?)
	github.com/google/yamlfmt/cmd/yamlfmt/config.go:194 +0x18f
main.run()
	github.com/google/yamlfmt/cmd/yamlfmt/main.go:55 +0x17b
main.main()
	github.com/google/yamlfmt/cmd/yamlfmt/main.go:27 +0x19

This also happens when .yamlfmt contains no override, but with a different panic message :

$ cat .yamlfmt
formatter:
  type: basic

$ yamlfmt --formatter retain_line_breaks=true
panic: assignment to entry in nil map
goroutine 1 [running]:
main.makeCommandConfigFromData(0xc000024300?)
	github.com/google/yamlfmt/cmd/yamlfmt/config.go:194 +0x19f
main.run()
	github.com/google/yamlfmt/cmd/yamlfmt/main.go:55 +0x17b
main.main()
	github.com/google/yamlfmt/cmd/yamlfmt/main.go:27 +0x19

No issue when .yamlfmt contains at least one override :

$ cat .yamlfmt
formatter:
  type: basic
  retain_line_breaks: false

$ yamlfmt --formatter retain_line_breaks=true

Thank you for the fix! 🚀