Merge pull request #23157 from pselle/each-err

Add a special error when each.value is nil in for_each resources
This commit is contained in:
Pam Selle 2019-10-23 11:19:39 -04:00 committed by GitHub
commit 8f04ab3eaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 1 deletions

View File

@ -2079,7 +2079,7 @@ func TestContext2Apply_provisionerDestroyForEach(t *testing.T) {
if diags == nil {
t.Fatal("should error")
}
if !strings.Contains(diags.Err().Error(), `Reference to "each" in context without for_each`) {
if !strings.Contains(diags.Err().Error(), "each.value is unknown and cannot be used in this context") {
t.Fatal("unexpected error:", diags.Err())
}
}

View File

@ -185,6 +185,16 @@ func (d *evaluationStateData) GetForEachAttr(addr addrs.ForEachAttr, rng tfdiags
returnVal = d.InstanceKeyData.EachKey
case "value":
returnVal = d.InstanceKeyData.EachValue
if returnVal == cty.NilVal {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: `each.value cannot be used in this context`,
Detail: fmt.Sprintf(`A reference to "each.value" has been used in a context in which it unavailable, such as when the configuration no longer contains the value in its "for_each" expression. Remove this reference to each.value in your configuration to work around this error.`),
Subject: rng.ToHCL().Ptr(),
})
return cty.UnknownVal(cty.DynamicPseudoType), diags
}
default:
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,