{"record":{"id":"13fc22fb738ad8ea","repo":"hashicorp/terraform","slug":"item-not-found","errorCode":null,"errorMessage":"item not found","messagePattern":"item not found","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/lang/funcs/collection.go","lineNumber":217,"sourceCode":"\n\t\tif args[0].LengthInt() == 0 { // Easy path\n\t\t\treturn cty.NilVal, errors.New(\"cannot search an empty list\")\n\t\t}\n\n\t\tfor it := args[0].ElementIterator(); it.Next(); {\n\t\t\ti, v := it.Element()\n\t\t\teq, err := stdlib.Equal(v, args[1])\n\t\t\tif err != nil {\n\t\t\t\treturn cty.NilVal, err\n\t\t\t}\n\t\t\tif !eq.IsKnown() {\n\t\t\t\treturn cty.UnknownVal(cty.Number), nil\n\t\t\t}\n\t\t\tif eq.True() {\n\t\t\t\treturn i, nil\n\t\t\t}\n\t\t}\n\t\treturn cty.NilVal, errors.New(\"item not found\")\n\n\t},\n})\n\n// LookupFunc constructs a function that performs dynamic lookups of map types.\nvar LookupFunc = function.New(&function.Spec{\n\tParams: []function.Parameter{\n\t\t{\n\t\t\tName:         \"inputMap\",\n\t\t\tType:         cty.DynamicPseudoType,\n\t\t\tAllowMarked:  true,\n\t\t\tAllowUnknown: true,\n\t\t},\n\t\t{\n\t\t\tName:         \"key\",\n\t\t\tType:         cty.String,\n\t\t\tAllowMarked:  true,\n\t\t\tAllowUnknown: true,","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/lang/funcs/collection.go#L199-L235","documentation":"Thrown by `index()` (IndexFunc) after iterating the entire list/tuple without finding an element equal (via stdlib.Equal) to the searched value. The list is non-empty and known, but no element matched. This is the 'not present' terminal state.","triggerScenarios":"Calling `index([\"a\",\"b\"], \"c\")`. The searched value was never added to the list.","commonSituations":"Searching for a key/name that does not exist in the current state. Case mismatch, trailing whitespace, or type difference between the searched value and list elements causing all comparisons to fail.","solutions":["Verify the value exists in the list (typo, case, whitespace).","Use `contains(var.list, val)` to pre-check membership before index().","Wrap with try() or guard the call if absence is expected."],"exampleFix":"// before\nlocals { i = index(var.environments, var.target_env) }\n\n// after\nlocals { i = contains(var.environments, var.target_env) ? index(var.environments, var.target_env) : -1 }","handlingStrategy":"validation","validationCode":"# HCL: check membership before index\nlocals { i = contains(var.list, val) ? index(var.list, val) : -1 }","typeGuard":"# HCL: try-based fallback\nlocals { i = try(index(var.list, val), -1) }","tryCatchPattern":null,"preventionTips":["Use contains() before index() to avoid the not-found error.","Normalize string comparisons (trim, lower) to avoid silent mismatches.","Consider a map lookup when you only need the value, not its position."],"tags":["terraform","hcl","collection-functions","index","not-found"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}