{"record":{"id":"b2f382c1720897a2","repo":"hashicorp/terraform","slug":"invalid-tfvars-syntax-s","errorCode":null,"errorMessage":"invalid tfvars syntax: %s","messagePattern":"invalid tfvars syntax: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/builtin/providers/terraform/functions.go","lineNumber":121,"sourceCode":"\t\t// type, since it will be an object type decided based on which\n\t\t// arguments and values we find in the string.\n\t\treturn cty.DynamicVal, nil\n\t}\n\n\t// If we get here then we know that:\n\t// - there's exactly one element in args\n\t// - it's a string\n\t// - it is known and non-null\n\t// So therefore the following is guaranteed to succeed.\n\tsrc := []byte(args[0].AsString())\n\n\t// As usual when we wrap HCL stuff up in functions, we end up needing to\n\t// stuff HCL diagnostics into plain string error messages. This produces\n\t// a non-ideal result but is still better than hiding the HCL-provided\n\t// diagnosis altogether.\n\tf, hclDiags := hclsyntax.ParseConfig(src, \"<decode_tfvars argument>\", hcl.InitialPos)\n\tif hclDiags.HasErrors() {\n\t\treturn cty.NilVal, fmt.Errorf(\"invalid tfvars syntax: %s\", hclDiags.Error())\n\t}\n\tattrs, hclDiags := f.Body.JustAttributes()\n\tif hclDiags.HasErrors() {\n\t\treturn cty.NilVal, fmt.Errorf(\"invalid tfvars content: %s\", hclDiags.Error())\n\t}\n\tretAttrs := make(map[string]cty.Value, len(attrs))\n\tfor name, attr := range attrs {\n\t\t// Evaluating the expression with no EvalContext achieves the same\n\t\t// interpretation as Terraform CLI makes of .tfvars files, rejecting\n\t\t// any function calls or references to symbols.\n\t\tv, hclDiags := attr.Expr.Value(nil)\n\t\tif hclDiags.HasErrors() {\n\t\t\treturn cty.NilVal, fmt.Errorf(\"invalid expression for variable %q: %s\", name, hclDiags.Error())\n\t\t}\n\t\tretAttrs[name] = v\n\t}\n\n\treturn cty.ObjectVal(retAttrs), nil","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/builtin/providers/terraform/functions.go#L103-L139","documentation":"Emitted by decodeTfvarsFunc (functions.go:119-122) when hclsyntax.ParseConfig cannot parse the input string as HCL tfvars syntax. The raw string is parsed at hcl.InitialPos with the label '<decode_tfvars argument>'; any HCL syntax error (unbalanced braces, invalid tokens, bad expression) is flattened into this message because HCL diagnostics cannot be propagated verbatim through the cty function error channel.","triggerScenarios":"Passing a malformed tfvars string to decode_tfvars: unbalanced braces/brackets, invalid attribute syntax, a stray token, a heredoc or expression the tfvars grammar rejects. Also triggered by content that is valid JSON/another format but not HCL tfvars.","commonSituations":"Reading a hand-edited .tfvars file with a typo; constructing a tfvars string dynamically with broken concatenation; passing a JSON string (tfvars is HCL, not JSON); Windows line-ending or BOM issues; copy-paste introducing smart quotes.","solutions":["Read the wrapped HCL diagnostic in %s to locate the exact line/column of the syntax error and fix it.","Validate the .tfvars file standalone with `terraform validate -var-file=...` or by linting it as HCL before feeding it to decode_tfvars.","Ensure the content is HCL tfvars syntax, not JSON — use jsondecode for JSON instead.","Strip BOM and normalize line endings if the string originates from a non-Unix source."],"exampleFix":"// before\nlocals { out = decode_tfvars(\"a = [1, 2\") }  // unbalanced bracket -> invalid syntax\n\n// after\nlocals { out = decode_tfvars(\"a = [1, 2]\") }","handlingStrategy":"validation","validationCode":"// Validate the string parses as HCL tfvars before relying on decode_tfvars.\nf, diags := hclsyntax.ParseConfig([]byte(s), \"preflight\", hcl.InitialPos)\nif diags.HasErrors() {\n    return fmt.Errorf(\"fix tfvars syntax before decode_tfvars: %s\", diags.Error())\n}","typeGuard":null,"tryCatchPattern":"// Surface the HCL diagnostic verbatim so the operator can fix the exact location.\nf, hclDiags := hclsyntax.ParseConfig(src, \"<decode_tfvars argument>\", hcl.InitialPos)\nif hclDiags.HasErrors() {\n    return cty.NilVal, fmt.Errorf(\"invalid tfvars syntax: %s\", hclDiags.Error())\n}","preventionTips":["Lint .tfvars files with `terraform validate -var-file=...` before decode_tfvars.","Ensure the content is HCL tfvars, not JSON (use jsondecode for JSON).","Strip BOM and normalize line endings from non-Unix sources.","Read the HCL diagnostic to fix the exact line/column of the syntax error."],"tags":["functions","decode-tfvars","hcl","parsing","syntax"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}