diff --git a/terraform/context_plan2_test.go b/terraform/context_plan2_test.go index e823484c3..e6c7da29f 100644 --- a/terraform/context_plan2_test.go +++ b/terraform/context_plan2_test.go @@ -740,6 +740,10 @@ func TestContext2Plan_refreshOnlyMode(t *testing.T) { resource "test_object" "a" { arg = "after" } + + output "out" { + value = test_object.a.arg + } `, }) state := states.BuildState(func(s *states.SyncState) { @@ -838,6 +842,23 @@ func TestContext2Plan_refreshOnlyMode(t *testing.T) { t.Errorf("%s has wrong previous run state after plan\ngot:\n%s\n\nwant substring: %s", addr, got, want) } } + + // The output value should also have updated. If not, it's likely that we + // skipped updating the working state to match the refreshed state when we + // were evaluating the resource. + if outChangeSrc := plan.Changes.OutputValue(addrs.RootModuleInstance.OutputValue("out")); outChangeSrc == nil { + t.Errorf("no change planned for output value 'out'") + } else { + outChange, err := outChangeSrc.Decode() + if err != nil { + t.Fatalf("failed to decode output value 'out': %s", err) + } + got := outChange.After + want := cty.StringVal("current") + if !want.RawEquals(got) { + t.Errorf("wrong value for output value 'out'\ngot: %#v\nwant: %#v", got, want) + } + } } func TestContext2Plan_invalidSensitiveModuleOutput(t *testing.T) { diff --git a/terraform/node_resource_plan_instance.go b/terraform/node_resource_plan_instance.go index 7ba45c706..d9603326f 100644 --- a/terraform/node_resource_plan_instance.go +++ b/terraform/node_resource_plan_instance.go @@ -226,6 +226,16 @@ func (n *NodePlannableResourceInstance) managedResourceExecute(ctx EvalContext) } diags = diags.Append(n.writeChange(ctx, change, "")) + } else { + // Even if we don't plan changes, we do still need to at least update + // the working state to reflect the refresh result. If not, then e.g. + // any output values refering to this will not react to the drift. + // (Even if we didn't actually refresh above, this will still save + // the result of any schema upgrading we did in readResourceInstanceState.) + diags = diags.Append(n.writeResourceInstanceState(ctx, instanceRefreshState, workingState)) + if diags.HasErrors() { + return diags + } } return diags