Skip to content

Commit

Permalink
fix: handle nil expr for completion
Browse files Browse the repository at this point in the history
  • Loading branch information
ansgarm committed Feb 23, 2024
1 parent 11c10d4 commit 9935743
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
5 changes: 4 additions & 1 deletion decoder/candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ func (d *PathDecoder) completionAtPos(ctx context.Context, body *hclsyntax.Body,
filename := body.Range().Filename

for _, attr := range body.Attributes {
if d.isPosInsideAttrExpr(attr, pos) {
// TODO: handle nil Expr in all nested calls instead which allows us
// to recover incomplete calls to provider defined functions (which have no expression
// as they are deemed invalid by hcl while they are not completed yet)
if attr.Expr != nil && d.isPosInsideAttrExpr(attr, pos) {
if bodySchema.Extensions != nil && bodySchema.Extensions.SelfRefs {
ctx = schema.WithActiveSelfRefs(ctx)
}
Expand Down
34 changes: 34 additions & 0 deletions decoder/candidates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,40 @@ resource "random_resource" "test" {
}
}

func TestDecoder_CompletionAtPos_nil_expr(t *testing.T) {
ctx := context.Background()

// provider:: is not a traversal expression, so hcl will return a nil expression which needs to be
// handled gracefully
f, _ := hclsyntax.ParseConfig([]byte(`attr = provider::`), "test.tf", hcl.InitialPos)

d := testPathDecoder(t, &PathContext{
Schema: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"attr": {Constraint: schema.AnyExpression{OfType: cty.DynamicPseudoType}},
},
},
Files: map[string]*hcl.File{
"test.tf": f,
},
})

pos := hcl.Pos{Line: 1, Column: 16, Byte: 15}

candidates, err := d.CompletionAtPos(ctx, "test.tf", pos)
if err != nil {
t.Fatal(err)
}

expectedCandidates := lang.CompleteCandidates([]lang.Candidate{})

diff := cmp.Diff(expectedCandidates, candidates, ctydebug.CmpOptions)
if diff != "" {
t.Fatalf("unexpected schema for %s: %s", stringPos(pos), diff)
}

}

func TestDecoder_CompletionAtPos_AnyAttribute(t *testing.T) {
ctx := context.Background()
providersSchema := &schema.BlockSchema{
Expand Down

0 comments on commit 9935743

Please sign in to comment.