I have two different HCL blocks.
variable good {}
variable bad
{}
Although both of the bad blocks look similar, their Block.OpenBraceRange.Start value is quite different. The first one seems to believe it exists at the start of the file (essentially hcl.InitialPos) but the second one seems to have something more sane as a response.
package main
import (
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
)
func Parse(filename string, bytes []byte) *hclsyntax.Body {
file, _ := hclsyntax.ParseConfig(bytes, filename, hcl.InitialPos)
return file.Body.(*hclsyntax.Body)
}
func main() {
body := Parse("", []byte("variable bad\n{}"))
fmt.Println(body.Blocks[0].OpenBraceRange.Start)
body = Parse("", []byte("variable good {}\nvariable bad\n{}"))
fmt.Println(body.Blocks[1].OpenBraceRange.Start)
}