{"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/c9def3e214014c1188faabfc4a5bde5095139765/internal/builtin/providers/terraform/functions.go#L14-L50","documentation":"Emitted by encodeTfvarsFunc (functions.go:31-33) when the function is called with zero arguments. The encode_tfvars function expects exactly one argument (the value to encode). The comment at lines 25-27 notes these checks are defensive robustness — the HCL language runtime should validate argument counts against the function schema before reaching this code, so hitting it means the runtime did not enforce the arity.","triggerScenarios":"Invoking `encode_tfvars()` with no arguments in HCL, or a programmatic caller passing an empty cty.Value slice that bypassed the function-schema arity check.","commonSituations":"Direct programmatic use of the function with a malformed args slice; a hypothetical language-runtime bug that skipped arity validation. In normal Terraform config this is unreachable because the runtime rejects missing args with its own error first.","solutions":["Call encode_tfvars with exactly one argument: the object/map value to encode, e.g. `encode_tfvars(local.myvars)`.","If invoking programmatically, pass a single-element []cty.Value matching the declared schema.","Update the function schema (Parameter count) if the intended arity genuinely changed."],"exampleFix":"// before\nlocals { out = encode_tfvars() }\n\n// after\nlocals { out = encode_tfvars({ a = 1, b = 2 }) }","handlingStrategy":"validation","validationCode":"// Enforce arity before invoking encode_tfvars programmatically.\nif len(args) != 1 {\n    return cty.NilVal, fmt.Errorf(\"encode_tfvars requires exactly one argument, got %d\", len(args))\n}","typeGuard":"// Ensure the args slice has the expected shape before calling.\nfunc hasOneArg(args []cty.Value) bool { return len(args) == 1 }","tryCatchPattern":null,"preventionTips":["Always call encode_tfvars with exactly one object/map argument.","Rely on the declared function schema so the runtime enforces arity first.","When invoking programmatically, validate len(args) before dispatch.","Add a type constraint on the input variable so the value is non-null and the right shape."],"tags":["functions","encode-tfvars","argument-validation","hcl"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}