diff --git a/command/jsonplan/plan.go b/command/jsonplan/plan.go index 43cb9064f..8adc8dd62 100644 --- a/command/jsonplan/plan.go +++ b/command/jsonplan/plan.go @@ -91,7 +91,11 @@ func Marshal( p *plans.Plan, sf *statefile.File, schemas *terraform.Schemas, + stateSchemas *terraform.Schemas, ) ([]byte, error) { + if stateSchemas == nil { + stateSchemas = schemas + } output := newPlan() output.TerraformVersion = version.String() @@ -120,7 +124,7 @@ func Marshal( } // output.PriorState - output.PriorState, err = jsonstate.Marshal(sf, schemas) + output.PriorState, err = jsonstate.Marshal(sf, stateSchemas) if err != nil { return nil, fmt.Errorf("error marshaling prior state: %s", err) } diff --git a/command/show.go b/command/show.go index 1831fb58e..7180ab394 100644 --- a/command/show.go +++ b/command/show.go @@ -145,7 +145,30 @@ func (c *ShowCommand) Run(args []string) int { if plan != nil { if jsonOutput == true { config := ctx.Config() - jsonPlan, err := jsonplan.Marshal(config, plan, stateFile, schemas) + + var err error + var jsonPlan []byte + + // If there is no prior state, we have all the schemas needed. + if stateFile == nil { + jsonPlan, err = jsonplan.Marshal(config, plan, stateFile, schemas, nil) + } else { + // If there is state, we need the state-specific schemas, which + // may differ from the schemas loaded from the plan. + // This occurs if there is a data_source in the state that was + // removed from the configuration, because terraform core does + // not need to load the schema to remove a data source. + opReq.PlanFile = nil + ctx, _, ctxDiags := local.Context(opReq) + diags = diags.Append(ctxDiags) + if ctxDiags.HasErrors() { + c.showDiagnostics(diags) + return 1 + } + stateSchemas := ctx.Schemas() + jsonPlan, err = jsonplan.Marshal(config, plan, stateFile, schemas, stateSchemas) + } + if err != nil { c.Ui.Error(fmt.Sprintf("Failed to marshal plan to json: %s", err)) return 1 @@ -236,10 +259,6 @@ func getStateFromEnv(b backend.Backend, env string) (*statefile.File, error) { return nil, fmt.Errorf("Failed to load state manager: %s", err) } - if err := stateStore.RefreshState(); err != nil { - return nil, fmt.Errorf("Failed to load state: %s", err) - } - sf := statemgr.Export(stateStore) return sf, nil