diff --git a/terraform/context.go b/terraform/context.go index 4156aa1f0..8afba75cd 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -781,20 +781,32 @@ func (c *Context) walk(graph *Graph, operation walkOperation) (*ContextGraphWalk } func (c *Context) graphWalker(operation walkOperation) *ContextGraphWalker { - if operation == walkValidate { - return &ContextGraphWalker{ - Context: c, - State: states.NewState().SyncWrapper(), - Changes: c.changes.SyncWrapper(), - InstanceExpander: instances.NewExpander(), - Operation: operation, - StopContext: c.runContext, - RootVariableValues: c.variables, - } + var state *states.SyncState + var refreshState *states.SyncState + + switch operation { + case walkValidate: + // validate should not use any state + s := states.NewState() + state = s.SyncWrapper() + + // validate currently uses the plan graph, so we have to populate the + // refreshState. + refreshState = s.SyncWrapper() + + case walkPlan: + state = c.state.SyncWrapper() + // plan requires a second state to store the refreshed resources + refreshState = c.state.DeepCopy().SyncWrapper() + + default: + state = c.state.SyncWrapper() } + return &ContextGraphWalker{ Context: c, - State: c.state.SyncWrapper(), + State: state, + RefreshState: refreshState, Changes: c.changes.SyncWrapper(), InstanceExpander: instances.NewExpander(), Operation: operation, diff --git a/terraform/graph_walk_context.go b/terraform/graph_walk_context.go index c842c0af4..d3ca73b3f 100644 --- a/terraform/graph_walk_context.go +++ b/terraform/graph_walk_context.go @@ -26,6 +26,7 @@ type ContextGraphWalker struct { // Configurable values Context *Context State *states.SyncState // Used for safe concurrent access to state + RefreshState *states.SyncState // Used for safe concurrent access to state Changes *plans.ChangesSync // Used for safe concurrent writes to changes InstanceExpander *instances.Expander // Tracks our gradual expansion of module and resource instances Operation walkOperation @@ -96,6 +97,7 @@ func (w *ContextGraphWalker) EvalContext() EvalContext { ProvisionerLock: &w.provisionerLock, ChangesValue: w.Changes, StateValue: w.State, + RefreshStateValue: w.RefreshState, Evaluator: evaluator, VariableValues: w.variableValues, VariableValuesLock: &w.variableValuesLock,