From 13ff341447ed1624e26edd13ec97ffb534f1bb1c Mon Sep 17 00:00:00 2001 From: Pam Selle <204372+pselle@users.noreply.github.com> Date: Tue, 22 Oct 2019 15:16:43 -0400 Subject: [PATCH 1/2] Add a special error for each.value --- terraform/context_apply_test.go | 2 +- terraform/evaluate.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index 203bd015d6..e4f9aafee6 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -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()) } } diff --git a/terraform/evaluate.go b/terraform/evaluate.go index 6681f8ddcb..a842f7bdc5 100644 --- a/terraform/evaluate.go +++ b/terraform/evaluate.go @@ -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 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: diags = diags.Append(&hcl.Diagnostic{ Severity: hcl.DiagError, From 86d69bbe1868269677359c58679acb11686c2c74 Mon Sep 17 00:00:00 2001 From: Pam Selle <204372+pselle@users.noreply.github.com> Date: Wed, 23 Oct 2019 11:18:10 -0400 Subject: [PATCH 2/2] Tweak the message mildly --- terraform/evaluate.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/terraform/evaluate.go b/terraform/evaluate.go index a842f7bdc5..9e71d3efa8 100644 --- a/terraform/evaluate.go +++ b/terraform/evaluate.go @@ -189,8 +189,8 @@ func (d *evaluationStateData) GetForEachAttr(addr addrs.ForEachAttr, rng tfdiags 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.`), + 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