{"record":{"id":"213672c840b8a221","repo":"hashicorp/terraform","slug":"exactly-one-argument-is-required","errorCode":null,"errorMessage":"exactly one argument is required","messagePattern":"exactly one argument is required","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/builtin/providers/terraform/functions.go","lineNumber":32,"sourceCode":"\t\"github.com/zclconf/go-cty/cty\"\n\t\"github.com/zclconf/go-cty/cty/function\"\n)\n\nvar functions = map[string]func([]cty.Value) (cty.Value, error){\n\t\"encode_tfvars\": encodeTfvarsFunc,\n\t\"decode_tfvars\": decodeTfvarsFunc,\n\t\"encode_expr\":   encodeExprFunc,\n}\n\nfunc encodeTfvarsFunc(args []cty.Value) (cty.Value, error) {\n\t// These error checks should not be hit in practice because the language\n\t// runtime should check them before calling, so this is just for robustness\n\t// and completeness.\n\tif len(args) > 1 {\n\t\treturn cty.NilVal, function.NewArgErrorf(1, \"too many arguments; only one expected\")\n\t}\n\tif len(args) == 0 {\n\t\treturn cty.NilVal, fmt.Errorf(\"exactly one argument is required\")\n\t}\n\n\tv := args[0]\n\tty := v.Type()\n\n\tif v.IsNull() {\n\t\t// Our functions schema does not say we allow null values, so we should\n\t\t// not get to this error message if the caller respects the schema.\n\t\treturn cty.NilVal, function.NewArgErrorf(1, \"cannot encode a null value in tfvars syntax\")\n\t}\n\tif !v.IsWhollyKnown() {\n\t\treturn cty.UnknownVal(cty.String).RefineNotNull(), nil\n\t}\n\n\tvar keys []string\n\tswitch {\n\tcase ty.IsObjectType():\n\t\tatys := ty.AttributeTypes()","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/builtin/providers/terraform/functions.go#L14-L50","documentation":"encode_tfvars builtin guard: the function schema declares exactly one parameter, so the language runtime should reject zero-arg calls before this code runs. The check is purely defensive robustness; reaching it implies the caller bypassed schema validation.","triggerScenarios":"encodeTfvarsFunc invoked with len(args) == 0: a direct programmatic call to the function without args (not via HCL), or a bug in the function dispatcher that skipped schema-based arity checks.","commonSituations":"Internal testing invoking the function directly; a regression in the function dispatcher; experimental code path bypassing the schema layer.","solutions":["Call encode_tfvars through normal HCL so the schema enforces arity (encode_tfvars(var.obj)).","If calling programmatically, supply exactly one argument matching the declared parameter.","Ensure the function's declared parameter count matches when wrapping/monkey-patching."],"exampleFix":"// before (programmatic misuse)\nencodeTfvarsFunc([]cty.Value{})\n// after\nencodeTfvarsFunc([]cty.Value{cty.ObjectVal(map[string]cty.Value{\"x\": cty.StringVal(\"y\")})})","handlingStrategy":"validation","validationCode":"// Programmatic callers: enforce arity before dispatch\nif len(args) != 1 { return cty.NilVal, fmt.Errorf(\"encode_tfvars takes exactly 1 arg\") }","typeGuard":"func encodeTfvarsArityOK(args []cty.Value) bool { return len(args) == 1 }","tryCatchPattern":null,"preventionTips":["Invoke encode_tfvars through HCL so the schema enforces arity.","Keep the in-body guard and the schema ParameterTypes in sync when editing.","Cover direct-call paths in unit tests."],"tags":["terraform","builtin","tfvars","encode","defensive","schema"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}