{"record":{"id":"02785d499d6ada09","repo":"hashicorp/terraform","slug":"length-of-keys-and-values-should-be-equal","errorCode":null,"errorMessage":"length of keys and values should be equal","messagePattern":"length of keys and values should be equal","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/lang/funcs/collection.go","lineNumber":365,"sourceCode":"\t\t},\n\t},\n\tType: func(args []cty.Value) (cty.Type, error) {\n\t\tty, _ := convert.UnifyUnsafe([]cty.Type{args[1].Type(), args[2].Type()})\n\t\tif ty == cty.NilType {\n\t\t\treturn cty.NilType, errors.New(\"keys and searchset must be of the same type\")\n\t\t}\n\n\t\t// the return type is based on args[0] (values)\n\t\treturn args[0].Type(), nil\n\t},\n\tRefineResult: refineNotNull,\n\tImpl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {\n\t\tif !args[0].IsKnown() {\n\t\t\treturn cty.UnknownVal(cty.List(retType.ElementType())), nil\n\t\t}\n\n\t\tif args[0].LengthInt() != args[1].LengthInt() {\n\t\t\treturn cty.ListValEmpty(retType.ElementType()), errors.New(\"length of keys and values should be equal\")\n\t\t}\n\n\t\toutput := make([]cty.Value, 0)\n\t\tvalues := args[0]\n\n\t\t// Keys and searchset must be the same type.\n\t\t// We can skip error checking here because we've already verified that\n\t\t// they can be unified in the Type function\n\t\tty, _ := convert.UnifyUnsafe([]cty.Type{args[1].Type(), args[2].Type()})\n\t\tkeys, _ := convert.Convert(args[1], ty)\n\t\tsearchset, _ := convert.Convert(args[2], ty)\n\n\t\t// if searchset is empty, return an empty list.\n\t\tif searchset.LengthInt() == 0 {\n\t\t\treturn cty.ListValEmpty(retType.ElementType()), nil\n\t\t}\n\n\t\tif !values.IsWhollyKnown() || !keys.IsWhollyKnown() {","sourceCodeStart":347,"sourceCodeEnd":383,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/lang/funcs/collection.go#L347-L383","documentation":"Thrown by `matchkeys()` (MatchkeysFunc) in its Impl when `values` (1st arg) and `keys` (2nd arg) have different lengths. matchkeys pairs values[i] with keys[i] positionally, so the two lists must have equal length; a mismatch is a logic error and the function returns an empty list plus this error.","triggerScenarios":"Calling `matchkeys(values, keys, searchset)` where len(values) != len(keys). One list was filtered or extended independently of the other.","commonSituations":"Two parallel lists maintained in separate locals/blocks that drifted out of sync. A comprehension that built values from a superset of the source used for keys.","solutions":["Ensure values and keys are derived from the same source so their lengths stay in sync.","Validate length equality before calling: guard with `length(var.values) == length(var.keys)`.","Restructure as a single list of objects {value=..., key=...} so pairing cannot drift."],"exampleFix":"// before\nlocals { r = matchkeys(var.names, var.ids, [\"prod\"]) }  # names and ids differ in length\n\n// after\nlocals {\n  pairs = [for i, n in var.names : { name = n, id = var.ids[i] }]  # built from one source\n  r     = [for p in local.pairs : p.name if contains([\"prod\"], p.id)]\n}","handlingStrategy":"validation","validationCode":"# HCL: assert equal length before matchkeys\nlocals {\n  ok = length(var.values) == length(var.keys)\n  r  = local.ok ? matchkeys(var.values, var.keys, var.searchset) : []\n}","typeGuard":"# HCL: derive both from one source list of objects\nlocals {\n  pairs = [for p in var.records : { value = p.value, key = p.key }]\n}","tryCatchPattern":null,"preventionTips":["Build values and keys from the same for-expression so lengths stay in sync.","Add a length-equality check before matchkeys.","Prefer a single list of objects over two parallel lists."],"tags":["terraform","hcl","collection-functions","matchkeys","length-mismatch"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}