Skip to content

Block's OpenBraceRange.Start differs if a proper block precedes it or not #708

@rcjsuen

Description

@rcjsuen

I have two different HCL blocks.

variable bad
{}
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.

{1 1 0}
{2 1 17}
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)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions