hashicorp / hashicorp/hcl

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

Open
#708 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
5.8k
Forks
657
Avg merge
20h 36m
Merged PRs (30d)
6

Description

I have two different HCL blocks.
```HCL
variable bad
{}
```
```HCL
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}
```
```Go
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)
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by running the supplied Go reproduction through hclsyntax.ParseConfig and inspect how Block.OpenBraceRange.Start is assigned. Trace the parser's handling of the two block layouts, then add regression coverage showing that equivalent blocks report the correct brace position.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.