diff --git a/internal/hcl/hcl.go b/internal/hcl/hcl.go index 84fd1e40b..ff37587e9 100644 --- a/internal/hcl/hcl.go +++ b/internal/hcl/hcl.go @@ -61,26 +61,22 @@ func (f *file) parse() (*parsedFile, error) { return f.pf, nil } - var parseDiags hcl.Diagnostics tokens, diags := hclsyntax.LexConfig(f.content, f.filename, hcl.InitialPos) if diags.HasErrors() { - parseDiags = append(parseDiags, diags...) + // The hclsyntax parser assumes all tokens are valid + // so we return early here + // TODO: Avoid ignoring TokenQuotedNewline to provide completion in unclosed string + return nil, diags } - body, diags := hclsyntax.ParseBodyFromTokens(tokens, hclsyntax.TokenEOF) - if diags.HasErrors() { - parseDiags = append(parseDiags, diags...) - } + body, _ := hclsyntax.ParseBodyFromTokens(tokens, hclsyntax.TokenEOF) f.pf = &parsedFile{ Tokens: tokens, Body: body, } - if parseDiags.HasErrors() { - return f.pf, parseDiags - } return f.pf, nil } @@ -94,7 +90,10 @@ func (f *file) PosInBlock(pos hcl.Pos) bool { } func (f *file) BlockAtPosition(pos hcl.Pos) (TokenizedBlock, error) { - pf, _ := f.parse() + pf, err := f.parse() + if err != nil { + return nil, err + } body, ok := pf.Body.(*hclsyntax.Body) if !ok { diff --git a/internal/hcl/hcl_test.go b/internal/hcl/hcl_test.go index f3f548d1a..ed96cff25 100644 --- a/internal/hcl/hcl_test.go +++ b/internal/hcl/hcl_test.go @@ -56,6 +56,28 @@ func TestFile_BlockAtPosition(t *testing.T) { }, }, }, + { + "invalid tokens", + `provider "aws" { + arg = " +}`, + hcl.Pos{ + Line: 2, + Column: 10, + Byte: 26, + }, + hcl.Diagnostics{ + &hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Invalid multi-line string", + Detail: `Quoted strings may not be split over multiple lines. ` + + `To produce a multi-line string, either use the \n escape to ` + + `represent a newline character or use the "heredoc" ` + + `multi-line template syntax.`, + }, + }, + []hclsyntax.Token{}, + }, { "valid config and position", `provider "aws" { diff --git a/internal/terraform/lang/parser_test.go b/internal/terraform/lang/parser_test.go index 342798071..5a61bf8f3 100644 --- a/internal/terraform/lang/parser_test.go +++ b/internal/terraform/lang/parser_test.go @@ -124,6 +124,7 @@ func TestParser_ParseBlockFromTokens(t *testing.T) { } func newTestBlock(t *testing.T, src string) ihcl.TokenizedBlock { + t.Helper() b, err := ihcl.NewTestBlock([]byte(src)) if err != nil { t.Fatal(err)