{"record":{"id":"c47cc32d7f2a5239","repo":"hashicorp/terraform","slug":"encountered-a-malformed-backend-state-file-that-co","errorCode":null,"errorMessage":"encountered a malformed backend state file that contains state for both a 'backend' and a 'state_store' block","messagePattern":"encountered a malformed backend state file that contains state for both a 'backend' and a 'state_store' block","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/workdir/backend_state.go","lineNumber":111,"sourceCode":"\t\treturn nil, fmt.Errorf(\"unsupported backend state version %d; you may need to use Terraform CLI v%s to work in this directory\", versionSniff.Version, versionSniff.TFVersion)\n\t}\n\n\t// If we get here then we can be sure that this file at least _thinks_\n\t// it's format version 3.\n\tvar stateFile BackendStateFile\n\terr = json.Unmarshal(src, &stateFile)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"invalid syntax: %w\", err)\n\t}\n\tif stateFile.Backend == nil && stateFile.Remote != nil {\n\t\t// It's very unlikely to get here, but one way it could happen is\n\t\t// if this working directory was most recently used with Terraform v0.8\n\t\t// or earlier, which didn't yet include the concept of backends.\n\t\t// This error message assumes that's the case.\n\t\treturn nil, fmt.Errorf(\"this working directory uses legacy remote state and so must first be upgraded using Terraform v0.9\")\n\t}\n\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:","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/workdir/backend_state.go#L93-L129","documentation":"A single backend state file must describe EITHER a 'backend' block OR a 'state_store' block, never both (the two fields are mutually exclusive storage mechanisms). Seeing both at backend_state.go:110 means the file is internally inconsistent or corrupt.","triggerScenarios":"ParseBackendStateFile finds Backend != nil && StateStore != nil after a successful decode.","commonSituations":"Manual editing or git-merging of .terraform/terraform.tfstate spliced two configs together; a botched migration between the classic backend mechanism and the newer state_store mechanism; files copied between directories.","solutions":["Open .terraform/terraform.tfstate, determine which storage mechanism is actually in use, and delete the other top-level block.","Safest: remove .terraform/ and re-run 'terraform init' with the single correct configuration.","Check source-control history of the file to understand how both blocks came to be written."],"exampleFix":"// before — .terraform/terraform.tfstate contains both:\n{ \"version\":3, \"backend\":{...}, \"state_store\":{...} }\nError: encountered a malformed backend state file that contains state for both a 'backend' and a 'state_store' block\n\n// after — keep only the one your config uses:\n{ \"version\":3, \"state_store\":{...} }\n// or simply:\n$ rm -rf .terraform && terraform init","handlingStrategy":"validation","validationCode":"// Reject a file that declares both storage mechanisms before calling ParseBackendStateFile.\nfunc hasBothBackendAndStateStore(src []byte) bool {\n    var probe struct {\n        Backend    *json.RawMessage `json:\"backend\"`\n        StateStore *json.RawMessage `json:\"state_store\"`\n    }\n    _ = json.Unmarshal(src, &probe)\n    return probe.Backend != nil && probe.StateStore != nil\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Never hand-merge .terraform/terraform.tfstate.","Choose one storage mechanism (backend OR state_store) per directory and document it.","After any manual recovery, re-run 'terraform init' to normalize the file."],"tags":["terraform","state","corruption","workdir"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}