hashicorp / hcl2

Former temporary home for experimental new version of HCL
https://github.com/hashicorp/hcl
Mozilla Public License 2.0
373 stars 66 forks source link

heredocs aren't properly closed during indent fomartting #106

Closed nozaq closed 4 years ago

nozaq commented 5 years ago

hclfmt failed to properly format indentation when a block contains two or more heredocs. related issue: https://github.com/hashicorp/terraform/issues/21434

Consider the following HCL code.

$ cat ../test.tf                        
foo {
bar = <<EOT
Foo bar baz
EOT
baz = <<EOT
Foo bar baz
EOT
}

baz {
default = "string"
}

When pass that file to hclfmt, the output of baz block is not indented as expected.

$ go run ./cmd/hclfmt/main.go ../test.tf
foo {
  bar = <<EOT
Foo bar baz
EOT
  baz = <<EOT
Foo bar baz
EOT
}

baz {
default = "string"
}

The expected output is:

$ go run ./cmd/hclfmt/main.go ../test.tf
foo {
  bar = <<EOT
Foo bar baz
EOT
  baz = <<EOT
Foo bar baz
EOT
}

baz {
  default = "string"
}

It seems inHeredoc flag is not set to false even after processing foo block. This could be because formatIndent() function expects TokenOHeredoc and TokenCHeredoc appear in different lines, while linesForFormat function put all tokens between TokenOHeredoc and TokenCHeredoc in the same formatLine instance.

I'm new to this codebase, so the above analysis could be completely wrong though...

pselle commented 4 years ago

Thanks for finding and fixing this! I've merged your change into master, and tested upgrading terraform with this change, but it doesn't seem to fix the upstream issue as yet. That said, this issue can be closed, and thank you for moving us closer to the solution!