diff --git a/internal/tfdiags/contextual.go b/internal/tfdiags/contextual.go index 0b6f27b69f..4fdb2e76b2 100644 --- a/internal/tfdiags/contextual.go +++ b/internal/tfdiags/contextual.go @@ -338,12 +338,16 @@ func rangeOfDeepestAttributeValueFromPath(body hcl.Body, traverse cty.Path) hcl. return body.MissingItemRange() } + return RangeForExpressionAtPath(attr.Expr, rest) +} + +func RangeForExpressionAtPath(expression hcl.Expression, path cty.Path) hcl.Range { // Now we need to loop through the rest of the path and progressively introspect // the HCL expression. - currentExpr := attr.Expr + currentExpr := expression STEP_ITERATION: - for _, step := range rest { + for _, step := range path { // We treat cty.IndexStep[type=String] and cty.GetAttrStep the same, so we just // need to deal with list indexes first if idxStep, ok := step.(cty.IndexStep); ok && idxStep.Key.Type() == cty.Number { @@ -354,7 +358,7 @@ STEP_ITERATION: return currentExpr.Range() } if err != nil || idx >= len(items) { - return attr.NameRange + return currentExpr.Range() } currentExpr = items[idx] continue STEP_ITERATION @@ -386,7 +390,7 @@ STEP_ITERATION: } } // If we could not find the item return early - return attr.NameRange + return currentExpr.Range() } return currentExpr.Range() diff --git a/internal/tfdiags/contextual_test.go b/internal/tfdiags/contextual_test.go index 82313c27aa..e02dde2881 100644 --- a/internal/tfdiags/contextual_test.go +++ b/internal/tfdiags/contextual_test.go @@ -239,8 +239,8 @@ simple_attr = "val" ), &SourceRange{ Filename: "test.tf", - Start: SourcePos{Line: 19, Column: 3, Byte: 155}, - End: SourcePos{Line: 19, Column: 19, Byte: 171}, + Start: SourcePos{Line: 19, Column: 22, Byte: 174}, + End: SourcePos{Line: 19, Column: 43, Byte: 195}, }, }, { @@ -378,8 +378,8 @@ simple_attr = "val" ), &SourceRange{ Filename: "test.tf", - Start: SourcePos{Line: 21, Column: 3, Byte: 233}, - End: SourcePos{Line: 21, Column: 13, Byte: 243}, + Start: SourcePos{Line: 21, Column: 16, Byte: 246}, + End: SourcePos{Line: 25, Column: 4, Byte: 393}, }, }, @@ -444,8 +444,8 @@ simple_attr = "val" ), &SourceRange{ Filename: "test.tf", - Start: SourcePos{Line: 27, Column: 1, Byte: 396}, - End: SourcePos{Line: 27, Column: 13, Byte: 408}, + Start: SourcePos{Line: 27, Column: 16, Byte: 411}, + End: SourcePos{Line: 27, Column: 23, Byte: 418}, }, }, { @@ -461,8 +461,8 @@ simple_attr = "val" ), &SourceRange{ Filename: "test.tf", - Start: SourcePos{Line: 28, Column: 1, Byte: 419}, - End: SourcePos{Line: 28, Column: 13, Byte: 431}, + Start: SourcePos{Line: 28, Column: 16, Byte: 434}, + End: SourcePos{Line: 28, Column: 34, Byte: 452}, }, }, { @@ -509,8 +509,8 @@ simple_attr = "val" ), &SourceRange{ Filename: "test.tf", - Start: SourcePos{Line: 29, Column: 1, Byte: 453}, - End: SourcePos{Line: 29, Column: 9, Byte: 461}, + Start: SourcePos{Line: 29, Column: 12, Byte: 464}, + End: SourcePos{Line: 32, Column: 2, Byte: 501}, }, }, {