Skip to content

Commit a3a19e5

Browse files
authored
Temporarily ignore key evaluation errors (#200)
1 parent 390211c commit a3a19e5

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

rules/terraform_map_duplicate_keys.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,10 @@ func (r *TerraformMapDuplicateKeysRule) checkObjectConsExpr(e hcl.Expression, ru
8080
} else {
8181
err := runner.EvaluateExpr(expr, &val, nil)
8282
if err != nil {
83-
diags = append(diags, &hcl.Diagnostic{
84-
Severity: hcl.DiagError,
85-
Summary: "failed to evaluate expression",
86-
Detail: err.Error(),
87-
})
83+
// When a key fails to evaluate, ignore the key and continue processing rather than terminating with an error.
84+
// This is due to a limitation that expressions with different scopes, such as for expressions, cannot be evaluated.
85+
// @see https://github.com/terraform-linters/tflint-ruleset-terraform/issues/199
86+
logger.Debug("Failed to evaluate key. The key will be ignored", "range", expr.Range(), "error", err.Error())
8887
continue
8988
}
9089
}

rules/terraform_map_duplicate_keys_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,18 @@ resource "null_resource" "test" {
193193
},
194194
},
195195
},
196+
{
197+
Name: "keys in for expressions",
198+
Content: `
199+
resource "null_resource" "test" {
200+
list = [for a in ["foo", "bar"] : {
201+
"${a}_baz" = 1
202+
"foo_baz" = 2
203+
}]
204+
}`,
205+
// The current implementation cannot find duplicate keys in for expressions.
206+
Expected: helper.Issues{},
207+
},
196208
}
197209

198210
rule := NewTerraformMapDuplicateKeysRule()

0 commit comments

Comments
 (0)