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.
This commit is contained in:
Martin Atkins 2018-05-31 16:28:28 -07:00
parent 26f76dd222
commit da2d91c0e4
1 changed files with 10 additions and 2 deletions

View File

@ -452,8 +452,16 @@ func (d *evaluationStateData) GetResourceInstance(addr addrs.ResourceInstance, r
ms := d.Evaluator.State.ModuleByPath(d.ModulePath) ms := d.Evaluator.State.ModuleByPath(d.ModulePath)
if ms == nil { if ms == nil {
// Not evaluated yet? // If we have no module state in the apply walk, that suggests we've hit
return cty.DynamicVal, diags // 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: // Note that the state structs currently have confusing legacy names: