{"record":{"id":"02b73e467421cc09","repo":"hashicorp/terraform","slug":"error-marshaling-prior-state-s","errorCode":null,"errorMessage":"error marshaling prior state: %s","messagePattern":"error marshaling prior state: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/jsonplan/plan.go","lineNumber":337,"sourceCode":"\t\t\treturn nil, fmt.Errorf(\"error in marshaling deferred action invocations: %s\", err)\n\t\t}\n\t}\n\n\t// output.OutputChanges\n\tif output.OutputChanges, err = MarshalOutputChanges(p.Changes); err != nil {\n\t\treturn nil, fmt.Errorf(\"error in marshaling output changes: %s\", err)\n\t}\n\n\t// output.Checks\n\tif p.Checks != nil && p.Checks.ConfigResults.Len() > 0 {\n\t\toutput.Checks = jsonchecks.MarshalCheckStates(p.Checks)\n\t}\n\n\t// output.PriorState\n\tif sf != nil && !sf.State.Empty() {\n\t\toutput.PriorState, err = jsonstate.Marshal(sf, schemas)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"error marshaling prior state: %s\", err)\n\t\t}\n\t}\n\n\t// output.Config\n\toutput.Config, err = jsonconfig.Marshal(config, schemas)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error marshaling config: %s\", err)\n\t}\n\n\t// output.Changes.ActionInvocations\n\tif p.Changes.ActionInvocations != nil {\n\t\tif output.ActionInvocations, err = MarshalActionInvocations(p.Changes.ActionInvocations, schemas); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"error marshaling action invocations: %s\", err)\n\t\t}\n\t}\n\n\treturn json.Marshal(output)\n}","sourceCodeStart":319,"sourceCodeEnd":355,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/jsonplan/plan.go#L319-L355","documentation":"Top-level wrapper: jsonstate.Marshal(sf, schemas) failed while building the prior_state section. State serialization usually fails because a resource in the prior state has no loaded schema to interpret it with.","triggerScenarios":"sf != nil and the state is non-empty, and marshaling it errors. Typically an inner 'no schema found' style error for a resource still present in state.","commonSituations":"Prior state references a resource whose provider was removed or downgraded; provider not initialized; state file referencing a resource type no longer defined by the installed provider; corrupted state.","solutions":["Run `terraform init` to install all providers the state references.","Align provider versions (check .terraform.lock.hcl).","Inspect state with `terraform state list` and remove orphaned resources no longer in config."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"out, err := jsonplan.Marshal(config, plan, sf, schemas)\nif err != nil && strings.Contains(err.Error(), \"marshaling prior state\") {\n    // a resource in prior state lacks a schema; run terraform init\n}\nreturn err","preventionTips":["Run `terraform init` so every provider referenced by state is installed.","Keep provider versions aligned with whatever wrote the state.","Remove orphaned resources from state when their provider is gone (`terraform state rm`)."],"tags":["state","marshal","provider"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}