{"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":"validation","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/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/configs/hcl2shim/flatmap.go#L222-L258","documentation":"Returned by hcl2ValueFromFlatmapTuple when the count sentinel parses as an integer but does not equal len(etys) — the declared tuple element type count. Because tuple types are positional with a fixed arity, a mismatch between the persisted count and the schema's expected element count means the state does not conform to the schema and decoding cannot proceed unambiguously.","triggerScenarios":"A tuple-typed attribute whose schema declares N element types but whose flatmap count sentinel holds M != N. Reached during state read or plan-time diff when the schema and state disagree on tuple width.","commonSituations":"Schema change that added/removed tuple positions without a corresponding state upgrader; state written against an older provider version; merging state from incompatible provider versions.","solutions":["Confirm the schema's tuple element type count matches the count sentinel in state; if the schema legitimately changed arity, write a state upgrader.","Restore state from a backup written under the matching schema version.","Pin the provider version so schema and state evolve together, then upgrade deliberately.","If the tuple is actually variable-length, model it as a list (cty.List) rather than a positional tuple."],"exampleFix":"// conceptual\n// before: tuple type with 2 element types, state count sentinel = 3\n// after: align — either update schema to declare 3 element types, or write\n//        a state v0->v1 upgrader that reshapes the stored value to 2 slots.","handlingStrategy":"validation","validationCode":"func tupleCountMatchesArity(m map[string]string, prefix string, arity int) bool {\n    s, ok := m[prefix+\"#\"]\n    if !ok { return true }\n    n, err := strconv.Atoi(s)\n    if err != nil { return false }\n    return n == arity\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep provider versions aligned with state; widen tuple arity only via a state upgrader.","Prefer cty.List over cty.Tuple when the collection is conceptually variable-length.","Add a schema-state compatibility test that decodes representative state against each schema change."],"tags":["flatmap","hcl2shim","tuple","schema-mismatch","go"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}