{"record":{"id":"10d443efce81eec0","repo":"hashicorp/terraform","slug":"failed-to-marshal-outputs-to-json-w","errorCode":null,"errorMessage":"failed to marshal outputs to json: %w","messagePattern":"failed to marshal outputs to json: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cloud/state.go","lineNumber":228,"sourceCode":"\tif schemas != nil {\n\t\tjsonState, err = jsonstate.Marshal(f, schemas)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\tstateFile, err := statefile.Read(bytes.NewReader(buf.Bytes()))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to read state: %w\", err)\n\t}\n\n\tov, err := jsonstate.MarshalOutputs(stateFile.State.RootOutputValues)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to translate outputs: %w\", err)\n\t}\n\tjsonStateOutputs, err := json.Marshal(ov)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to marshal outputs to json: %w\", err)\n\t}\n\n\terr = s.uploadState(s.lineage, s.serial, s.forcePush, buf.Bytes(), jsonState, jsonStateOutputs)\n\tif err != nil {\n\t\ts.stateUploadErr = true\n\t\treturn fmt.Errorf(\"error uploading state: %w\", err)\n\t}\n\t// After we've successfully persisted, what we just wrote is our new\n\t// reference state until someone calls RefreshState again.\n\t// We've potentially overwritten (via force) the state, lineage\n\t// and / or serial (and serial was incremented) so we copy over all\n\t// three fields so everything matches the new state and a subsequent\n\t// operation would correctly detect no changes to the lineage, serial or state.\n\ts.readState = s.state.DeepCopy()\n\ts.readLineage = s.lineage\n\ts.readSerial = s.serial\n\n\treturn nil","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/cloud/state.go#L210-L246","documentation":"Thrown during PersistState when Go's standard encoding/json.Marshal fails to serialize the output value structure produced by jsonstate.MarshalOutputs into the final JSONStateOutputs byte slice. Unlike the preceding translate step (which handles cty-to-JSON type mapping), this is pure JSON encoding of an already-translated Go structure. Failure indicates the translated structure contains a non-serializable Go value.","triggerScenarios":"jsonstate.MarshalOutputs returns a structure containing a Go type json.Marshal cannot handle (func, chan, complex128, or a type with a broken MarshalJSON); the structure contains an unserializable cty.Value marker that leaked through translation; a bug in MarshalOutputs producing map keys that are not strings.","commonSituations":"Rare in stable releases; most commonly a terraform bug discovered in a new output type. Can appear when a provider's custom output serialization interacts badly with a terraform version change.","solutions":["Upgrade terraform to the latest stable release — json marshaling bugs in outputs are fixed promptly","Simplify the root outputs to isolate which value produces the unserializable structure","Report the issue with the json.Marshal error text (it names the Go type that failed) and the terraform/provider versions"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Validate that translated outputs are JSON-serializable before persist:\nov, err := jsonstate.MarshalOutputs(state.RootOutputValues)\nif err != nil {\n    return err\n}\nif _, err := json.Marshal(ov); err != nil {\n    return fmt.Errorf(\"outputs are not JSON-serializable, cannot persist: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"err := stateMgr.PersistState(schemas)\nif err != nil && strings.Contains(err.Error(), \"failed to marshal outputs to json\") {\n    log.Print(\"output JSON marshaling failed; this is likely a terraform bug\")\n}\nreturn err","preventionTips":["Report persistent JSON marshaling failures as terraform bugs since the translated structure should always be serializable","Keep terraform updated to benefit from output-marshaling fixes","Minimize exotic output types in root module outputs"],"tags":["serialization","json","outputs","state-persist","terraform"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}