diff --git a/hcl/hclsyntax/expression.go b/hcl/hclsyntax/expression.go index db3cbfd..7f8d2b0 100644 --- a/hcl/hclsyntax/expression.go +++ b/hcl/hclsyntax/expression.go @@ -604,8 +604,9 @@ func (e *IndexExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { diags = append(diags, collDiags...) diags = append(diags, keyDiags...) - val, diags := hcl.Index(coll, key, &e.SrcRange) - setDiagEvalContext(diags, e, ctx) + val, indexDiags := hcl.Index(coll, key, &e.SrcRange) + setDiagEvalContext(indexDiags, e, ctx) + diags = append(diags, indexDiags...) return val, diags } diff --git a/hcl/hclsyntax/expression_test.go b/hcl/hclsyntax/expression_test.go index fe44eb2..a3c5f75 100644 --- a/hcl/hclsyntax/expression_test.go +++ b/hcl/hclsyntax/expression_test.go @@ -981,6 +981,16 @@ upper( cty.StringVal("hello"), 0, }, + { + `["boop"].foo[index]`, // index is a variable to force IndexExpr instead of traversal + &hcl.EvalContext{ + Variables: map[string]cty.Value{ + "index": cty.NumberIntVal(0), + }, + }, + cty.DynamicVal, + 1, // expression ["boop"] does not have attributes + }, { `foo`,