{"record":{"id":"1368fd7abd8cc4e6","repo":"hashicorp/terraform","slug":"all-arguments-must-have-the-same-type","errorCode":null,"errorMessage":"all arguments must have the same type","messagePattern":"all arguments must have the same type","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/lang/funcs/collection.go","lineNumber":152,"sourceCode":"// stdlib and modified so that it returns the first *non-empty* non-null element\n// from a sequence, instead of merely the first non-null.\nvar CoalesceFunc = function.New(&function.Spec{\n\tParams: []function.Parameter{},\n\tVarParam: &function.Parameter{\n\t\tName:             \"vals\",\n\t\tType:             cty.DynamicPseudoType,\n\t\tAllowUnknown:     true,\n\t\tAllowDynamicType: true,\n\t\tAllowNull:        true,\n\t},\n\tType: func(args []cty.Value) (ret cty.Type, err error) {\n\t\targTypes := make([]cty.Type, len(args))\n\t\tfor i, val := range args {\n\t\t\targTypes[i] = val.Type()\n\t\t}\n\t\tretType, _ := convert.UnifyUnsafe(argTypes)\n\t\tif retType == cty.NilType {\n\t\t\treturn cty.NilType, errors.New(\"all arguments must have the same type\")\n\t\t}\n\t\treturn retType, nil\n\t},\n\tRefineResult: refineNotNull,\n\tImpl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {\n\t\tfor _, argVal := range args {\n\t\t\t// We already know this will succeed because of the checks in our Type func above\n\t\t\targVal, _ = convert.Convert(argVal, retType)\n\t\t\tif !argVal.IsKnown() {\n\t\t\t\treturn cty.UnknownVal(retType), nil\n\t\t\t}\n\t\t\tif argVal.IsNull() {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif retType == cty.String && argVal.RawEquals(cty.StringVal(\"\")) {\n\t\t\t\tcontinue\n\t\t\t}\n","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/lang/funcs/collection.go#L134-L170","documentation":"Thrown by the `coalesce()` function (CoalesceFunc) during its Type callback when convert.UnifyUnsafe over all variadic argument types yields cty.NilType — meaning the arguments cannot be unified to a single common type. coalesce must return one concrete type, so heterogeneous types that cannot convert to each other are rejected before evaluation.","triggerScenarios":"Calling `coalesce()` with arguments of incompatible cty types, e.g. `coalesce(\"a\", 1)` (string vs number) or `coalesce([1], {a=1})`. The Type func collects all arg types; if UnifyUnsafe fails it returns NilType and this error.","commonSituations":"Mixing a string default with a number variable, e.g. `coalesce(var.env, 0)`. Using coalesce on list/object args of differing element shapes. Copying an example that assumed homogeneous types into a config with mixed types.","solutions":["Make every coalesce() argument the same type, or types that auto-convert (e.g. all strings, all numbers).","Add explicit type conversion: `coalesce(tostring(var.x), \"default\")`.","Split logic: use separate coalesce calls per type or a lookup map keyed by a discriminator."],"exampleFix":"// before\nlocals { x = coalesce(var.name, 0) }   # string vs number\n\n// after\nlocals { x = coalesce(var.name, \"default\") }","handlingStrategy":"validation","validationCode":"# HCL: ensure all coalesce args share a type before the call\nlocals {\n  args = [var.a, var.b, var.c]\n  ok   = alltrue([for v in local.args : try(tostring(v), null) != null])\n  out  = local.ok ? coalesce([for v in local.args : tostring(v)]...) : \"default\"\n}","typeGuard":"# HCL: normalize all args to one type first\nlocals { out = coalesce(tostring(var.a), tostring(var.b), \"default\") }","tryCatchPattern":null,"preventionTips":["Standardize the type of every coalesce() argument explicitly with to{string,number,list}().","Avoid mixing variable types in a single coalesce chain.","Add `terraform validate` to CI."],"tags":["terraform","hcl","collection-functions","coalesce","type-unification"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}