{"record":{"id":"b2ae01d15c4295f0","repo":"hashicorp/terraform","slug":"wrong-number-of-values-for-q-in-state-got-d-bu","errorCode":null,"errorMessage":"wrong number of values for %q in state: got %d, but need %d","messagePattern":"wrong number of values for %q in state: got (.+?), but need (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/configs/hcl2shim/flatmap.go","lineNumber":240,"sourceCode":"\tlistName := strings.TrimRight(prefix, \".\")\n\tif m[listName] == UnknownVariableValue {\n\t\treturn cty.UnknownVal(cty.Tuple(etys)), nil\n\t}\n\n\tcountStr, exists := m[prefix+\"#\"]\n\tif !exists {\n\t\treturn cty.NullVal(cty.Tuple(etys)), nil\n\t}\n\tif countStr == UnknownVariableValue {\n\t\treturn cty.UnknownVal(cty.Tuple(etys)), nil\n\t}\n\n\tcount, err := strconv.Atoi(countStr)\n\tif err != nil {\n\t\treturn cty.DynamicVal, fmt.Errorf(\"invalid count value for %q in state: %s\", prefix, err)\n\t}\n\tif count != len(etys) {\n\t\treturn cty.DynamicVal, fmt.Errorf(\"wrong number of values for %q in state: got %d, but need %d\", prefix, count, len(etys))\n\t}\n\n\tvals = make([]cty.Value, len(etys))\n\tfor i, ety := range etys {\n\t\tkey := prefix + strconv.Itoa(i)\n\t\tval, err := hcl2ValueFromFlatmapValue(m, key, ety)\n\t\tif err != nil {\n\t\t\treturn cty.DynamicVal, err\n\t\t}\n\t\tvals[i] = val\n\t}\n\treturn cty.TupleVal(vals), nil\n}\n\nfunc hcl2ValueFromFlatmapMap(m map[string]string, prefix string, ty cty.Type) (cty.Value, error) {\n\tvals := make(map[string]cty.Value)\n\tety := ty.ElementType()\n","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/configs/hcl2shim/flatmap.go#L222-L258","documentation":"Returned by hcl2ValueFromFlatmapTuple (flatmap.go:240) when the integer length stored in the flatmap count key does not equal len(etys) — the number of element types declared in the tuple schema. Tuples have a fixed, positionally-typed shape, so any mismatch between the stored element count and the schema's declared tuple arity is a structural inconsistency.","triggerScenarios":"Decoding a tuple-typed attribute where the flatmap '<prefix>#' count is an integer but differs from the number of TupleElementTypes in the schema's cty.Tuple type. For example schema declares tuple([string, number]) (arity 2) but state count says 3.","commonSituations":"A provider schema tuple's element types were changed (added/removed a positional element) without migrating existing state. State from a different provider version. Manual edits that changed the count key without adding/removing corresponding element keys.","solutions":["Compare the schema's tuple arity against the state's count value (both shown in the message); bring them into agreement.","If you intentionally changed the tuple schema, write a state migration to add/remove element entries and update the count together.","Pin the provider version whose schema matches the state, apply once to normalize, then upgrade.","If unsure which is correct, re-import the resource to regenerate a consistent flatmap."],"exampleFix":"// before — schema: cty.Tuple([]cty.Type{cty.String, cty.Number})\n// state:  \"config.#\": \"3\"  (arity mismatch)\n\n// after — align state element count with tuple arity\n\"config.#\": \"2\"","handlingStrategy":"validation","validationCode":"// Ensure the flatmap tuple count matches the schema's declared arity\nfunc tupleCountMatches(m map[string]string, prefix string, etys []cty.Type) error {\n    s, ok := m[prefix+\"#\"]\n    if !ok {\n        return nil\n    }\n    n, err := strconv.Atoi(s)\n    if err != nil {\n        return fmt.Errorf(\"count for %s is not an integer\", prefix)\n    }\n    if n != len(etys) {\n        return fmt.Errorf(\"count %d != tuple arity %d for %s\", n, len(etys), prefix)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When changing a tuple schema's element count, always migrate existing state in lockstep.","Pin provider versions through a schema change so the writer and reader agree.","Re-import resources after a structural schema change rather than relying on stale state."],"tags":["flatmap","hcl2shim","state","tuple","schema-drift"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}