{"record":{"id":"15303400eff2fbca","repo":"hashicorp/terraform","slug":"attempted-to-encode-a-malformed-backend-state-file","errorCode":null,"errorMessage":"attempted to encode a malformed backend state file; it contains state for both a 'backend' and a 'state_store' block. This is a bug in Terraform and should be reported.","messagePattern":"attempted to encode a malformed backend state file; it contains state for both a 'backend' and a 'state_store' block\\. This is a bug in Terraform and should be reported\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/workdir/backend_state.go","lineNumber":128,"sourceCode":"\tif stateFile.Backend != nil && stateFile.StateStore != nil {\n\t\treturn nil, fmt.Errorf(\"encountered a malformed backend state file that contains state for both a 'backend' and a 'state_store' block\")\n\t}\n\tif stateFile.StateStore != nil && stateFile.StateStore.ProviderSupplyMode == \"\" {\n\t\t// Check for this, as lacking this data can cause problems later when an empty provider version\n\t\t// is encountered. This error will make debugging much easier.\n\t\treturn nil, fmt.Errorf(\"encountered a malformed backend state file with a 'state_store' block that is missing the required 'provider_supply_mode' property\")\n\t}\n\n\treturn &stateFile, nil\n}\n\nfunc EncodeBackendStateFile(f *BackendStateFile) ([]byte, error) {\n\tf.Version = 3 // we only support version 3\n\tf.TFVersion = version.SemVer.String()\n\n\tswitch {\n\tcase f.Backend != nil && f.StateStore != nil:\n\t\treturn nil, fmt.Errorf(\"attempted to encode a malformed backend state file; it contains state for both a 'backend' and a 'state_store' block. This is a bug in Terraform and should be reported.\")\n\tcase f.Backend == nil && f.StateStore == nil:\n\t\t// This is valid - if the user has a backend state file and an implied local backend in use\n\t\t// the backend state file exists but has no Backend data.\n\tcase f.Backend != nil:\n\t\t// Not implementing anything here - risk of breaking changes\n\tcase f.StateStore != nil:\n\t\terr := f.StateStore.Validate()\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\tdefault:\n\t\tpanic(\"error when determining whether backend state file was valid. This is a bug in Terraform and should be reported.\")\n\t}\n\n\treturn json.MarshalIndent(f, \"\", \"  \")\n}\n\nfunc (f *BackendStateFile) DeepCopy() *BackendStateFile {","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/workdir/backend_state.go#L110-L146","documentation":"EncodeBackendStateFile at backend_state.go:127 refuses to serialize a BackendStateFile that has BOTH Backend and StateStore set. Unlike error 763 (a corrupt file already on disk), this means Terraform's own in-memory code produced an impossible object — the message explicitly calls it a Terraform bug to report.","triggerScenarios":"An internal call path sets both f.Backend and f.StateStore non-nil before invoking EncodeBackendStateFile (backend_state.go:127 switch case).","commonSituations":"A code regression during the backend<->state_store transition; should never arise from user configuration alone.","solutions":["Report a bug to the Terraform project including the CLI version, the stack trace, and how the directory was initialized.","As an immediate workaround, remove .terraform/ and re-run 'terraform init'.","Avoid any custom code/tooling that constructs a BackendStateFile with both Backend and StateStore populated."],"exampleFix":"// before — internal call:\nf.Backend = &BackendConfigState{...}\nf.StateStore = &StateStoreConfigState{...}\njson.Marshal(EncodeBackendStateFile(f)) // -> error 765 (bug)\n\n// after — set exactly one:\nf.StateStore = &StateStoreConfigState{...}\n// f.Backend stays nil","handlingStrategy":"validation","validationCode":"// Invariant: a BackendStateFile carries at most one of Backend / StateStore.\nfunc assertSingleStorageMode(f *BackendStateFile) error {\n    if f.Backend != nil && f.StateStore != nil {\n        return errors.New(\"BackendStateFile has both Backend and StateStore set\")\n    }\n    return nil\n}\n// call assertSingleStorageMode(f) before EncodeBackendStateFile(f).","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Enforce the one-storage-mechanism invariant wherever BackendStateFile is mutated.","Add a fuzzer/property test: EncodeBackendStateFile never receives both fields set.","Report any occurrence — by design this path is unreachable in correct code."],"tags":["terraform","bug","state-store","backend","workdir"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}