{"record":{"id":"142b09162d0a0c17","repo":"hashicorp/terraform","slug":"invalid-count-value-for-q-in-state-s","errorCode":null,"errorMessage":"invalid count value for %q in state: %s","messagePattern":"invalid count value for %q in state: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/configs/hcl2shim/flatmap.go","lineNumber":237,"sourceCode":"\tvar vals []cty.Value\n\n\t// if the container is unknown, there is no count string\n\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) {","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/configs/hcl2shim/flatmap.go#L219-L255","documentation":"Returned by hcl2ValueFromFlatmapTuple when the '<prefix>#' sentinel key (the legacy flatmap count marker) holds a value that strconv.Atoi cannot parse as an integer. The flatmap tuple representation stores element count under a trailing '#' key; if that marker is non-numeric, the decoder cannot know how many elements to read and aborts.","triggerScenarios":"Decoding a tuple-typed value from a flatmap where the count sentinel (e.g. 'root.0.field.#') is missing-as-string, empty, or contains non-digit characters. Reached via the flatmap-to-cty shim during state read or diff application.","commonSituations":"Corrupt or hand-edited state where the count sentinel was overwritten; flatmap producers that emit 'count' or 'len' under a different key than '#'; partial state writes that left the count marker empty during a crash.","solutions":["Inspect the state key named in the error and confirm the trailing '#' marker holds an integer string.","Restore from a known-good state backup if the marker is corrupt.","If migrating from a non-flatmap representation, ensure the producer writes the '#' count sentinel as an integer for tuple/list/set types.","Run 'terraform state pull' / 'terraform state push' to round-trip and normalize the state."],"exampleFix":"// operational — not a code fix\n// before: \"root.tuple.#\": \"three\"  (non-numeric)\n// after:  \"root.tuple.#\": \"3\"","handlingStrategy":"validation","validationCode":"func tupleCountIsInteger(m map[string]string, prefix string) bool {\n    s, ok := m[prefix+\"#\"]\n    if !ok { return true } // missing -> null, not an error\n    _, err := strconv.Atoi(s)\n    return err == nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Avoid editing tuple count sentinels in state by hand.","When producing flatmaps programmatically, always write the '#' marker as a base-10 integer.","Restore from a known-good state backup if a sentinel is corrupt."],"tags":["flatmap","hcl2shim","tuple","state-corruption","go"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}