hashicorp/terraform · error

exactly one argument is required

Error message

exactly one argument is required

What it means

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.

Source

Thrown at internal/builtin/providers/terraform/functions.go:32

	"github.com/zclconf/go-cty/cty"
	"github.com/zclconf/go-cty/cty/function"
)

var functions = map[string]func([]cty.Value) (cty.Value, error){
	"encode_tfvars": encodeTfvarsFunc,
	"decode_tfvars": decodeTfvarsFunc,
	"encode_expr":   encodeExprFunc,
}

func encodeTfvarsFunc(args []cty.Value) (cty.Value, error) {
	// These error checks should not be hit in practice because the language
	// runtime should check them before calling, so this is just for robustness
	// and completeness.
	if len(args) > 1 {
		return cty.NilVal, function.NewArgErrorf(1, "too many arguments; only one expected")
	}
	if len(args) == 0 {
		return cty.NilVal, fmt.Errorf("exactly one argument is required")
	}

	v := args[0]
	ty := v.Type()

	if v.IsNull() {
		// Our functions schema does not say we allow null values, so we should
		// not get to this error message if the caller respects the schema.
		return cty.NilVal, function.NewArgErrorf(1, "cannot encode a null value in tfvars syntax")
	}
	if !v.IsWhollyKnown() {
		return cty.UnknownVal(cty.String).RefineNotNull(), nil
	}

	var keys []string
	switch {
	case ty.IsObjectType():
		atys := ty.AttributeTypes()

View on GitHub (pinned to c9def3e214)

Solutions

  1. Call encode_tfvars with exactly one argument: the object/map value to encode, e.g. `encode_tfvars(local.myvars)`.
  2. If invoking programmatically, pass a single-element []cty.Value matching the declared schema.
  3. Update the function schema (Parameter count) if the intended arity genuinely changed.

Example fix

// before
locals { out = encode_tfvars() }

// after
locals { out = encode_tfvars({ a = 1, b = 2 }) }
Defensive patterns

Strategy: validation

Validate before calling

// Enforce arity before invoking encode_tfvars programmatically.
if len(args) != 1 {
    return cty.NilVal, fmt.Errorf("encode_tfvars requires exactly one argument, got %d", len(args))
}

Type guard

// Ensure the args slice has the expected shape before calling.
func hasOneArg(args []cty.Value) bool { return len(args) == 1 }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07). Data as JSON: /api/errors/213672c840b8a221. Report an issue: GitHub.