{"record":{"id":"ad1ef6e32901b274","repo":"hashicorp/terraform","slug":"index-s-out-of-range-in-v","errorCode":null,"errorMessage":"index %s out of range in %#v","messagePattern":"index (.+?) out of range in %#v","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/configs/hcl2shim/paths.go","lineNumber":177,"sourceCode":"\tvar path cty.Path\n\tvar err error\n\n\tk, rest := pathSplit(key)\n\n\t// we don't need to convert the index keys to paths\n\tif k == \"#\" {\n\t\treturn path, nil\n\t}\n\n\tidx, err := strconv.Atoi(k)\n\tif err != nil {\n\t\treturn path, err\n\t}\n\n\tpath = cty.Path{cty.IndexStep{Key: cty.NumberIntVal(int64(idx))}}\n\n\tif idx >= len(etys) {\n\t\treturn path, fmt.Errorf(\"index %s out of range in %#v\", key, etys)\n\t}\n\n\tif rest == \"\" {\n\t\treturn path, nil\n\t}\n\n\tty := etys[idx]\n\n\tp, err := pathFromFlatmapKeyValue(rest, ty.ElementType())\n\tif err != nil {\n\t\treturn path, err\n\t}\n\n\treturn append(path, p...), nil\n}\n\nfunc pathFromFlatmapKeyMap(key string, ty cty.Type) (cty.Path, error) {\n\tvar path cty.Path","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/configs/hcl2shim/paths.go#L159-L195","documentation":"Returned by pathFromFlatmapKeyTuple when the integer index parsed from the key is greater than or equal to the declared tuple arity (len(etys)). The decoder has already built the IndexStep but cannot resolve the element type for an out-of-bounds position, so it aborts with the offending key and element-type list for diagnostics.","triggerScenarios":"Translating a flatmap key with an index segment (e.g. 'attr.5.sub') where the index exceeds the tuple's declared arity. Reached via requiresReplacePath or the pathFromFlatmapKey* family when descending into tuple-typed positions.","commonSituations":"Schema change that shrank a tuple's arity while old diffs/state still reference higher indices; RequiresReplace referencing an index that no longer exists; custom diff producers emitting indices past the end.","solutions":["Cross-check the index in the error against the tuple's current element count.","If the tuple legitimately shrank, write a state upgrader and remove stale RequiresReplace entries.","Restore the schema/plan to a consistent version pair so indices align with arity.","If variable-length indexing is needed, model the field as a list rather than a positional tuple."],"exampleFix":"// before: tuple arity 3, RequiresReplace references index 5\n// after: align arity to 6, or drop the stale index-5 entry","handlingStrategy":"validation","validationCode":"func tupleIndexInRange(key string, arity int) bool {\n    k, _ := pathSplit(key)\n    idx, err := strconv.Atoi(k)\n    if err != nil { return false }\n    return idx >= 0 && idx < arity\n}","typeGuard":"func tupleIndexInRange(key string, arity int) bool {\n    k, _ := pathSplit(key)\n    idx, err := strconv.Atoi(k)\n    if err != nil { return false }\n    return idx >= 0 && idx < arity\n}","tryCatchPattern":null,"preventionTips":["Drop stale RequiresReplace indices when shrinking a tuple's arity.","Prefer cty.List when the collection needs variable length.","Add a state upgrader alongside any schema change that alters tuple arity."],"tags":["flatmap","hcl2shim","paths","tuple","out-of-range","go"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}