{"record":{"id":"11757b70380395dc","repo":"hashicorp/terraform","slug":"invalid-collection-length-s","errorCode":null,"errorMessage":"invalid collection length: %s","messagePattern":"invalid collection length: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/lang/funcs/collection.go","lineNumber":463,"sourceCode":"\t\tty := val.Type()\n\n\t\t// Our parameter spec above doesn't set AllowUnknown or AllowNull,\n\t\t// so we can assume our top-level collection is both known and non-null\n\t\t// in here.\n\n\t\tswitch {\n\t\tcase ty.IsListType() || ty.IsSetType():\n\t\t\tlenVal := val.Length()\n\t\t\tif !lenVal.IsKnown() {\n\t\t\t\treturn cty.UnknownVal(retType), nil\n\t\t\t}\n\t\t\tvar l int\n\t\t\terr := gocty.FromCtyValue(lenVal, &l)\n\t\t\tif err != nil {\n\t\t\t\t// It would be very strange to get here, because that would\n\t\t\t\t// suggest that the length is either not a number or isn't\n\t\t\t\t// an integer, which would suggest a bug in cty.\n\t\t\t\treturn cty.NilVal, fmt.Errorf(\"invalid collection length: %s\", err)\n\t\t\t}\n\t\t\tswitch l {\n\t\t\tcase 0:\n\t\t\t\treturn cty.NullVal(retType), nil\n\t\t\tcase 1:\n\t\t\t\tvar ret cty.Value\n\t\t\t\t// We'll use an iterator here because that works for both lists\n\t\t\t\t// and sets, whereas indexing directly would only work for lists.\n\t\t\t\t// Since we've just checked the length, we should only actually\n\t\t\t\t// run this loop body once.\n\t\t\t\tfor it := val.ElementIterator(); it.Next(); {\n\t\t\t\t\t_, ret = it.Element()\n\t\t\t\t}\n\t\t\t\treturn ret, nil\n\t\t\t}\n\t\tcase ty.IsTupleType():\n\t\t\tetys := ty.TupleElementTypes()\n\t\t\tswitch len(etys) {","sourceCodeStart":445,"sourceCodeEnd":481,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/lang/funcs/collection.go#L445-L481","documentation":"Thrown by the `one()` collection function (OneFunc) when converting the computed length of a list or set into a Go int fails. After `val.Length()` returns a known cty value, gocty.FromCtyValue tries to decode it into an `int`; if that fails the value is not a valid integer. The source comment explicitly says this would be 'very strange' and 'would suggest a bug in cty', meaning this is an internal-invariant error rather than a normal validation failure. It surfaces to the Terraform user as an error from the `one(...)` call in HCL.","triggerScenarios":"Calling `one(var.list)` or `one(local.set)` in a .tf file where the collection's runtime length value is produced by cty but is somehow not representable as a normal integer (e.g. a non-finite or non-numeric length coming out of a buggy provider output or expression). The length is marked IsKnown==true yet fails the integer decode.","commonSituations":"A provider returns a malformed collection whose length is not a finite integer; a cty-level corruption during expression evaluation; extremely rare in stock Terraform and almost always points to an upstream bug in cty or a custom provider. Not something a typical config mistake produces.","solutions":["Report this as a Terraform bug with the minimal .tf config that reproduces it, including the provider version producing the collection value.","Inspect the value passed to `one()` (e.g. via `terraform console` running `length(var.x)`) to confirm whether it is a sane integer.","Replace `one(x)` with an explicit length check (`x != null && length(x) == 1 ? x[0] : null`) to bypass the failing code path while the bug is investigated.","Update or replace the provider/cty version that produces the anomalous collection length."],"exampleFix":"// before\nlocals {\n  item = one(data.buggy.thing.items)\n}\n\n// after: guard the value and surface a clearer message\nlocals {\n  items = data.buggy.thing.items\n  item  = length(local.items) == 1 ? local.items[0] : null\n}","handlingStrategy":"validation","validationCode":"// HCL: validate the collection is well-formed before calling one()\nlocals {\n  items = var.list  # the list/set passed to one()\n  ok    = can(length(local.items))\n  item  = local.ok && length(local.items) <= 1 ? one(local.items) : null\n}","typeGuard":"// HCL type guard: ensure arg is a list/set/tuple of <=1 element\noutput \"checked\" {\n  value = can(one(var.x)) ? one(var.x) : null\n}","tryCatchPattern":null,"preventionTips":["Prefer explicit `length(x) == 1 ? x[0] : null` over `one(x)` when the source data is untrusted.","Report reproductions upstream since this indicates a cty-level bug, not a config error.","Use `terraform console` to inspect `length()` of the suspect collection before relying on one()."],"tags":["terraform","hcl","cty","one-func","internal-bug"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}