{"record":{"id":"857c7153ce8d03a2","repo":"hashicorp/terraform","slug":"invalid-index-value-q","errorCode":null,"errorMessage":"invalid index value %q","messagePattern":"invalid index value %q","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"internal/legacy/terraform/diff.go","lineNumber":584,"sourceCode":"\tchildName := path[len(path)-1]\n\tcontainerSchema := &configschema.Block{\n\t\tBlockTypes: map[string]*configschema.NestedBlock{\n\t\t\tchildName: {\n\t\t\t\tNesting: configschema.NestingSet,\n\t\t\t\tBlock:   *synthSchema,\n\t\t\t},\n\t\t},\n\t}\n\n\treturn d.applyBlockDiff(parentPath, attrs, containerSchema)\n}\n\n// countFlatmapContainerValues returns the number of values in the flatmapped container\n// (set, map, list) indexed by key. The key argument is expected to include the\n// trailing \".#\", or \".%\".\nfunc countFlatmapContainerValues(key string, attrs map[string]string) string {\n\tif len(key) < 3 || !(strings.HasSuffix(key, \".#\") || strings.HasSuffix(key, \".%\")) {\n\t\tpanic(fmt.Sprintf(\"invalid index value %q\", key))\n\t}\n\n\tprefix := key[:len(key)-1]\n\titems := map[string]int{}\n\n\tfor k := range attrs {\n\t\tif k == key {\n\t\t\tcontinue\n\t\t}\n\t\tif !strings.HasPrefix(k, prefix) {\n\t\t\tcontinue\n\t\t}\n\n\t\tsuffix := k[len(prefix):]\n\t\tdot := strings.Index(suffix, \".\")\n\t\tif dot > 0 {\n\t\t\tsuffix = suffix[:dot]\n\t\t}","sourceCodeStart":566,"sourceCodeEnd":602,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/legacy/terraform/diff.go#L566-L602","documentation":"Panics in countFlatmapContainerValues (diff.go:584). The function counts flatmap container values and requires the key to end in '.#' (list/set count) or '.%' (map count); any other key (a plain attribute or an element key like 'ports.0') panics. It is an internal precondition violation in the diff-application code.","triggerScenarios":"An internal caller passes a non-count flatmap key to countFlatmapContainerValues, reached during diff expansion of sets/lists/maps. Usually a bug in diff construction or malformed flatmap keys.","commonSituations":"Corrupted diff/state, custom flatmap manipulation, or schema migration producing malformed keys that look like element keys instead of count keys.","solutions":["Inspect the key printed in the panic message; it must be a count key (.# or .%).","Restore a known-good state and re-run plan/apply.","If seen after a provider/TF upgrade, report the diff bug with the offending flatmap key."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"func isContainerCountKey(k string) bool {\n    return strings.HasSuffix(k, \".#\") || strings.HasSuffix(k, \".%\")\n}","typeGuard":null,"tryCatchPattern":"// guard internal callers with recover at the diff boundary\ndefer func() {\n    if r := recover(); r != nil {\n        diag := fmt.Errorf(\"internal diff panic: %v\", r)\n        _ = diag // surface as a diagnostic\n    }\n}()","preventionTips":["Only pass count keys (.#/.%) to countFlatmapContainerValues.","Do not hand-construct flatmap diffs.","Restore known-good state on corruption."],"tags":["terraform","diff","flatmap","panic","internal-invariant"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}