{"record":{"id":"4ec94156b8102bbc","repo":"hashicorp/terraform","slug":"key-q-could-not-be-found","errorCode":null,"errorMessage":"Key %q could not be found","messagePattern":"Key %q could not be found","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/backend/remote-state/consul/client.go","lineNumber":101,"sourceCode":"\tchunked, hash, chunks, pair, err := c.chunkedMode()\n\tif err != nil {\n\t\treturn nil, diags.Append(err)\n\t}\n\tif pair == nil {\n\t\treturn nil, diags\n\t}\n\n\tc.modifyIndex = pair.ModifyIndex\n\n\tvar payload []byte\n\tif chunked {\n\t\tfor _, c := range chunks {\n\t\t\tpair, _, err := kv.Get(c, nil)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, diags.Append(err)\n\t\t\t}\n\t\t\tif pair == nil {\n\t\t\t\treturn nil, diags.Append(fmt.Errorf(\"Key %q could not be found\", c))\n\t\t\t}\n\t\t\tpayload = append(payload, pair.Value[:]...)\n\t\t}\n\t} else {\n\t\tpayload = pair.Value\n\t}\n\n\t// If the payload starts with 0x1f, it's gzip, not json\n\tif len(payload) >= 1 && payload[0] == '\\x1f' {\n\t\tpayload, err = uncompressState(payload)\n\t\tif err != nil {\n\t\t\treturn nil, diags.Append(err)\n\t\t}\n\t}\n\n\tmd5 := md5.Sum(payload)\n\n\tif hash != \"\" && fmt.Sprintf(\"%x\", md5) != hash {","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/backend/remote-state/consul/client.go#L83-L119","documentation":"In RemoteClient.Get, when the state is stored in chunked mode, the manifest at the state path lists per-chunk KV paths. The client fetches each chunk; if any chunk returns a nil pair (key deleted or never written), this fires. It means the chunk index disagrees with Consul's KV store — the state is partially missing.","triggerScenarios":"RemoteClient.Get -> chunkedMode() returns chunked=true and a list of chunk paths; for one of them kv.Get(c) returns (nil, nil).","commonSituations":"Manual deletion of chunk keys under tfstate.<hash>/; a previous Put crashed between writing chunks and the manifest; another writer concurrently rewrote and trimmed chunks; Consul lost data after a bad snapshot restore.","solutions":["Inspect the manifest at the state key: `consul kv get <state-path>` and note the chunks array and current-hash.","For each chunk path in the list, check `consul kv get <chunk>` to identify which are missing.","Restore the missing chunks or the whole state prefix from a Consul snapshot taken when the state was consistent.","If unrecoverable, restore a known-good state file and re-run terraform apply to reconcile."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Before relying on Get(), verify the chunk manifest agrees with KV.\nfunc verifyChunksPresent(client *consulapi.Client, statePath string) error {\n    pair, _, err := client.KV().Get(statePath, nil)\n    if err != nil {\n        return err\n    }\n    if pair == nil {\n        return nil // not chunked or absent\n    }\n    var manifest struct {\n        CurrentHash string   `json:\"current-hash\"`\n        Chunks      []string `json:\"chunks\"`\n    }\n    if err := json.Unmarshal(pair.Value, &manifest); err != nil || manifest.CurrentHash == \"\" {\n        return nil // single-entry mode\n    }\n    for _, c := range manifest.Chunks {\n        p, _, err := client.KV().Get(c, nil)\n        if err != nil {\n            return fmt.Errorf(\"checking chunk %s: %w\", c, err)\n        }\n        if p == nil {\n            return fmt.Errorf(\"missing chunk %s; state is corrupt\", c)\n        }\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Take periodic Consul snapshots so a corrupt chunk set is recoverable.","Never manually delete keys under the tfstate.<hash>/ chunk prefix.","Avoid concurrent writers on the same state path.","Monitor Consul KV for unexpected deletes on the state prefix."],"tags":["consul","backend","state-corruption","chunked-state"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}