Add a special error for each.value

This commit is contained in:
Pam Selle 2019-10-22 15:16:43 -04:00
parent d7bce857cf
commit 13ff341447
2 changed files with 11 additions and 1 deletions

View File

@ -2079,7 +2079,7 @@ func TestContext2Apply_provisionerDestroyForEach(t *testing.T) {
if diags == nil { if diags == nil {
t.Fatal("should error") 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()) 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 returnVal = d.InstanceKeyData.EachKey
case "value": case "value":
returnVal = d.InstanceKeyData.EachValue returnVal = d.InstanceKeyData.EachValue
if returnVal == cty.NilVal {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: `each.value is unknown and 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 after the config no longer contains the value in its "for_each" expression. Remove this reference to each.value in your config to work around this error.`),
Subject: rng.ToHCL().Ptr(),
})
return cty.UnknownVal(cty.DynamicPseudoType), diags
}
default: default:
diags = diags.Append(&hcl.Diagnostic{ diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError, Severity: hcl.DiagError,