{"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":"exception","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/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/workdir/backend_state.go#L93-L129","documentation":"ParseBackendStateFile found a file containing BOTH a `backend` block and a `state_store` block. These are mutually exclusive configuration shapes — backends are the classic model, state_store is the newer model — so a file with both is malformed and Terraform refuses to guess which to use.","triggerScenarios":"A hand-merged or partially-rewritten .terraform/terraform.tfstate, or a buggy migration tool that appended a state_store section without removing the prior backend section.","commonSituations":"Manual file surgery, interrupted `terraform init` mid-rewrite, or a third-party state-management tool that wrote both shapes.","solutions":["Inspect .terraform/terraform.tfstate and delete whichever block (`backend` or `state_store`) you did not intend to keep.","Re-run `terraform init` to rewrite the file cleanly from your configuration.","Restore from a backup taken before the corruption."],"exampleFix":"# inspect\njq '{has_backend: (.backend!=null), has_state_store: (.state_store!=null)}' .terraform/terraform.tfstate\n\n# regenerate cleanly\nrm .terraform/terraform.tfstate\nterraform init","handlingStrategy":"validation","validationCode":"// pre-flight: detect dual-block corruption before parsing\nvar probe struct {\n    Backend   *json.RawMessage `json:\"backend\"`\n    StateStore *json.RawMessage `json:\"state_store\"`\n}\n_ = json.Unmarshal(src, &probe)\nif probe.Backend != nil && probe.StateStore != nil {\n    return nil, errors.New(\"backend state file has both backend and state_store; delete one and re-init\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Don't merge backend state files from different tooling.","Keep .terraform/ ephemeral; recreate via init."],"tags":["backend","state-store","malformed","init"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}