From e488ff126edc57d29d01a08f14264392670d3e4d Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Mon, 10 Sep 2018 14:44:32 -0700 Subject: [PATCH] core: InstanceDiff.ApplyToValue correctly handle creates We previously tried to take a shortcut for an empty diff, just returning the given value directly. This is incorrect in the weird case where we're creating a new instance but it has no attributes (and thus an empty diff) because in that case we'd return the given null value, turning the result into a no-op or destroy change. To fix this, we just always do the work to construct a new value, even if we might end up doing all this just to reconstruct the same value we started with in some cases. --- terraform/diff.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/terraform/diff.go b/terraform/diff.go index 74a7811c3..f57d23201 100644 --- a/terraform/diff.go +++ b/terraform/diff.go @@ -415,10 +415,10 @@ func (d *InstanceDiff) Unlock() { d.mu.Unlock() } // This method is intended for shimming old subsystems that still use this // legacy diff type to work with the new-style types. func (d *InstanceDiff) ApplyToValue(base cty.Value, schema *configschema.Block) (cty.Value, error) { - // No diff means the state is unchanged. - if d.Empty() { - return base, nil - } + // We always build a new value here, even if the given diff is "empty", + // because we might be planning to create a new instance that happens + // to have no attributes set, and so we want to produce an empty object + // rather than just echoing back the null old value. // Create an InstanceState attributes from our existing state. // We can use this to more easily apply the diff changes.