{"record":{"id":"a17a49ebb6b79481","repo":"hashicorp/terraform","slug":"json-marshalindent-error-dynamic","errorCode":null,"errorMessage":"json.MarshalIndent error (dynamic)","messagePattern":"json\\.MarshalIndent error \\(dynamic\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"internal/command/views/show.go","lineNumber":231,"sourceCode":"\t\toutput.Diagnostics = []*viewsjson.Diagnostic{}\n\t}\n\n\tvar rs jsonstate.Resource\n\tif !jsonState.Empty() {\n\t\t// we know there's only one resource instance, but we need to find it.\n\t\tif len(jsonState.RootModule.Resources) > 0 {\n\t\t\trs = jsonState.RootModule.Resources[0]\n\t\t} else {\n\t\t\trs = findResourceInChildModules(jsonState.RootModule)\n\t\t}\n\t}\n\n\toutput.Resource = rs\n\n\tj, err := json.MarshalIndent(&output, \"\", \"  \")\n\tif err != nil {\n\t\t// Should never happen because we fully-control the input here\n\t\tpanic(err)\n\t}\n\tv.view.streams.Println(string(j))\n\n\tif diags.HasErrors() {\n\t\treturn 1\n\t}\n\treturn 0\n}\n\nfunc findResourceInChildModules(mod jsonstate.Module) jsonstate.Resource {\n\tfor _, cm := range mod.ChildModules {\n\t\tif len(cm.Resources) == 1 {\n\t\t\treturn cm.Resources[0]\n\t\t}\n\t}\n\tfor _, child := range mod.ChildModules {\n\t\treturn findResourceInChildModules(child)\n\t}","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/views/show.go#L213-L249","documentation":"Terraform panics in ShowJSON.DisplayResourceInstanceState when json.MarshalIndent fails on the 'terraform state show -json <addr>' output struct (FormatVersion + a jsonstate.Resource + diagnostics). The code comment asserts this 'should never happen because we fully-control the input', so a panic here means the resource state being serialized contains a value encoding/json cannot represent.","triggerScenarios":"Calling view.DisplayResourceInstanceState in ShowJSON (internal/command/views/show.go:228) where the assembled Output struct holds a jsonstate.Resource whose MarshalJSON or nested cty/JSON value returns an error. json.MarshalIndent returns a non-nil error for unsupported values: chan/func/math.NaN/math.Inf/cyclic refs, or a custom MarshalJSON that errors.","commonSituations":"A state file built from a provider whose schema produced a numeric value that became NaN/Inf, hand-edited or corrupted state, a third-party provider emitting a value Terraform's jsonstate marshaller cannot round-trip, or an internal regression in jsonstate.Resource marshaling after a schema change.","solutions":["Inspect the exact resource address and provider involved; reproduce with 'terraform state show -json <addr>' and capture the stack trace, then check the resource attributes for NaN/Inf or non-serializable values.","If the state is corrupted, use 'terraform state pull' / edit the JSON / 'terraform state push' to remove the offending attribute, or restore a known-good state backup from the workspace .terraform/ or remote backend history.","Report the bug to Terraform with the goroutine trace and the provider/schema that produced the unmarshallable value; the defensive panic indicates a serialization contract was violated upstream.","Pin to a known-good provider/binary version that does not emit the offending value while the upstream fix lands."],"exampleFix":"// before\nj, err := json.MarshalIndent(&output, \"\", \"  \")\nif err != nil {\n    // Should never happen because we fully-control the input here\n    panic(err)\n}\n\n// after (fail gracefully instead of crashing the CLI)\nj, err := json.MarshalIndent(&output, \"\", \"  \")\nif err != nil {\n    v.view.streams.Eprintf(\"Failed to marshal resource state to json: %s\", err)\n    return 1\n}","handlingStrategy":"validation","validationCode":"// Before calling DisplayResourceInstanceState, sanity-check the resource you\n// are about to render does not contain non-serializable numeric values.\nfunc isMarshallableResource(rs jsonstate.Resource) error {\n    // jsonstate.Resource marshals via encoding/json; the only realistic\n    // failure modes are NaN/Inf in numeric attributes or a custom\n    // MarshalJSON error. Walk the attribute JSON if you have it.\n    raw, err := json.Marshal(rs)\n    if err != nil {\n        return fmt.Errorf(\"resource would fail json marshal: %w\", err)\n    }\n    _ = raw\n    return nil\n}","typeGuard":null,"tryCatchPattern":"// Wrap CLI entry points so an unexpected MarshalIndent panic degrades to a\n// non-zero exit + message instead of an uncaught goroutine crash.\nfunc runShowJSON(ctx context.Context, fn func() int) (code int) {\n    defer func() {\n        if r := recover(); r != nil {\n            fmt.Fprintf(os.Stderr, \"terraform: aborted show -json: %v\\n\", r)\n            code = 1\n        }\n    }()\n    return fn()\n}","preventionTips":["Treat any hit of this panic as a bug: capture the stack trace and the resource address, and check the state for NaN/Inf or provider-schema oddities.","Keep state backups so a corrupted/unmarshallable resource can be restored without the CLI crash blocking recovery.","Pin provider versions that produce known-good, JSON-serializable attribute values."],"tags":["panic","json","marshaling","state-show","defensive","show-command"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}