From da2d91c0e4d8a8628624cf5b1d6890963b1cec04 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Thu, 31 May 2018 16:28:28 -0700 Subject: [PATCH] core: Re-fix edge case with missing module state during evaluation In #14526 we fixed a sticky edge-case where a resource with count = 0 set won't create its containing module state on apply, and thus when another expression refers to it we need to deal with that absense. The original bug fixed by #14526 was actually a nil dereference panic in this case. Our new HCL2-oriented expression evaluation codepath was, on the other hand, correctly checking for the nil, but was not taking the correct action in response to it, leading to the result being an unexpected unknown value. Here we replicate the fix to #14526 by behaving as if there are just no instances present in this case. We achieve this in a slightly different way here by just creating an empty ModuleState, but the effect is the same as #14526. This fixes TestContext2Apply_multiVarMissingState. --- terraform/evaluate.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/terraform/evaluate.go b/terraform/evaluate.go index 3fe5f5c4d0..342e695b7d 100644 --- a/terraform/evaluate.go +++ b/terraform/evaluate.go @@ -452,8 +452,16 @@ func (d *evaluationStateData) GetResourceInstance(addr addrs.ResourceInstance, r ms := d.Evaluator.State.ModuleByPath(d.ModulePath) if ms == nil { - // Not evaluated yet? - return cty.DynamicVal, diags + // If we have no module state in the apply walk, that suggests we've hit + // a rather awkward edge-case: the resource this variable refers to + // has count = 0 and is the only resource processed so far on this walk, + // and so we've ended up not creating any resource states yet. We don't + // create a module state until the first resource is written into it, + // so the module state doesn't exist when we get here. + // + // In this case we act as we would if we had been passed a module + // with an empty resource state map. + ms = &ModuleState{} } // Note that the state structs currently have confusing legacy names: