{"record":{"id":"b81cc8f29bb1fd2b","repo":"hashicorp/terraform","slug":"failed-to-marshal-state-to-json-s","errorCode":null,"errorMessage":"Failed to marshal state to json: %s","messagePattern":"Failed to marshal state to json: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/state_show.go","lineNumber":151,"sourceCode":"\n\t// check if the resource has a configured provider, otherwise this will use the default provider\n\trs := state.Resource(addr.ContainingResource())\n\tabsPc := addrs.AbsProviderConfig{\n\t\tProvider: rs.ProviderConfig.Provider,\n\t\tAlias:    rs.ProviderConfig.Alias,\n\t\tModule:   addrs.RootModule,\n\t}\n\tsingleInstance := states.NewState()\n\tsingleInstance.EnsureModule(addr.Module).SetResourceInstanceCurrent(\n\t\taddr.Resource,\n\t\tis.Current,\n\t\tabsPc,\n\t)\n\n\tmockFile := statefile.New(singleInstance, \"\", 0)\n\troot, outputs, err := jsonstate.MarshalForRenderer(mockFile, schemas)\n\tif err != nil {\n\t\tdiags = diags.Append(fmt.Errorf(\"Failed to marshal state to json: %s\", err))\n\t\treturn view.DisplayResourceInstanceState(jsonformat.State{}, diags)\n\t}\n\n\tjstate := jsonformat.State{\n\t\tStateFormatVersion:    jsonstate.FormatVersion,\n\t\tProviderFormatVersion: jsonprovider.FormatVersion,\n\t\tRootModule:            root,\n\t\tRootModuleOutputs:     outputs,\n\t\tProviderSchemas:       jsonprovider.MarshalForRenderer(schemas),\n\t}\n\n\treturn view.DisplayResourceInstanceState(jstate, diags)\n}\n\nfunc (c *StateShowCommand) Help() string {\n\thelpText := `\nUsage: terraform [global options] state show [options] ADDRESS\n","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/state_show.go#L133-L169","documentation":"Thrown by state_show.go when jsonstate.MarshalForRenderer(mockFile, schemas) fails while preparing the single-instance mock state for display. The schemas come from lr.Core.Schemas(...) and the state from the refreshed state manager. Failure means the state structure does not match the provider schemas Terraform has on hand, or the marshal hit an internal invariant violation.","triggerScenarios":"Provider schema mismatch: the resource type in state was produced by a provider version whose schema differs from the one currently cached; a state upgrade for that resource type did not run; provider plugin failed to report schema for the resource type in question.","commonSituations":"Downgraded provider version in required_providers; state written by a provider that has since been removed from required_providers; plugin crash during GetProviderSchema; stale schema cache after a provider version bump without re-init.","solutions":["Run `terraform init -upgrade` to refresh provider binaries and their schemas.","Confirm `required_providers` versions still cover the resource types present in state.","Run `terraform state list` and `terraform state show` for other resources to isolate which resource's schema is bad.","If a state upgrade is needed, run `terraform refresh`/`terraform apply -refresh-only` with matching provider versions first."],"exampleFix":"# before: provider downgraded, schema mismatch\n terraform state show aws_instance.web\n\n# after\n terraform init -upgrade\n terraform state show aws_instance.web","handlingStrategy":"validation","validationCode":"// Confirm every resource type in state has a schema.\nfor _, mod := range state.Modules {\n    for rt := range mod.Resources {\n        if _, ok := schemas.ResourceTypes[rt.String()]; !ok {\n            return fmt.Errorf(\"no schema for %s; run terraform init -upgrade\", rt)\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"root, outputs, err := jsonstate.MarshalForRenderer(mockFile, schemas)\nif err != nil {\n    diags = diags.Append(fmt.Errorf(\"Failed to marshal state to json: %s; provider schema may be stale — run terraform init -upgrade\", err))\n    return view.DisplayResourceInstanceState(jsonformat.State{}, diags)\n}","preventionTips":["Run `terraform init -upgrade` after changing provider versions.","Keep `required_providers` versions aligned with the providers that wrote state.","Do not remove a provider from required_providers while its resources are still in state.","Use `terraform state replace-provider` when migrating provider sources."],"tags":["terraform","state-show","json","schema","provider","render"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}