{"record":{"id":"84f16d117815877c","repo":"hashicorp/terraform","slug":"cannot-search-an-empty-list","errorCode":null,"errorMessage":"cannot search an empty list","messagePattern":"cannot search an empty list","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/lang/funcs/collection.go","lineNumber":201,"sourceCode":"\t\t},\n\t\t{\n\t\t\tName: \"value\",\n\t\t\tType: cty.DynamicPseudoType,\n\t\t},\n\t},\n\tType:         function.StaticReturnType(cty.Number),\n\tRefineResult: refineNotNull,\n\tImpl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {\n\t\tif !(args[0].Type().IsListType() || args[0].Type().IsTupleType()) {\n\t\t\treturn cty.NilVal, errors.New(\"argument must be a list or tuple\")\n\t\t}\n\n\t\tif !args[0].IsKnown() {\n\t\t\treturn cty.UnknownVal(cty.Number), nil\n\t\t}\n\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},","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/lang/funcs/collection.go#L183-L219","documentation":"Thrown by `index()` (IndexFunc) when the first argument is a known list/tuple of length zero. Searching an empty sequence can never match, so the function fails fast with this explicit message instead of falling through to the 'item not found' path. Unknown lists return unknown and do not trigger this.","triggerScenarios":"Calling `index(var.list, val)` where var.list evaluates to `[]`. A dynamic list that happens to be empty at apply time.","commonSituations":"A list populated from a filtered comprehension that filtered everything out. An empty `for_each`-derived list passed to index(). A conditional returning [] in the active workspace.","solutions":["Guard with a length check: `length(var.list) > 0 ? index(var.list, val) : -1`.","Ensure the source list is actually populated before calling index().","Reconsider the workflow: if the list may legitimately be empty, handle that case explicitly rather than relying on index()."],"exampleFix":"// before\nlocals { i = index(var.items, target) }   # var.items may be []\n\n// after\nlocals { i = length(var.items) > 0 ? index(var.items, target) : -1 }","handlingStrategy":"validation","validationCode":"# HCL: guard the empty case\nlocals { i = length(var.list) > 0 ? index(var.list, val) : -1 }","typeGuard":"# HCL membership pre-check\nlocals { i = contains(var.list, val) ? index(var.list, val) : -1 }","tryCatchPattern":null,"preventionTips":["Wrap index() with a length check whenever the list may be empty.","Derive lists from a single source so emptiness is predictable.","Use contains() to pre-validate membership."],"tags":["terraform","hcl","collection-functions","index","empty-collection"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}