{"record":{"id":"a668726d324c91ec","repo":"hashicorp/terraform","slug":"only-tf-tfvars-and-tftest-hcl-files-can-be-pr","errorCode":null,"errorMessage":"Only .tf, .tfvars, and .tftest.hcl files can be processed with terraform fmt","messagePattern":"Only \\.tf, \\.tfvars, and \\.tftest\\.hcl files can be processed with terraform fmt","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"info","filePath":"internal/command/fmt.go","lineNumber":163,"sourceCode":"\t\t\t\t\t\t// so we'll need to simplify here.\n\t\t\t\t\t\tdiags = diags.Append(fmt.Errorf(\"Failed to read file %s\", path))\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\n\t\t\t\t\tfileDiags := c.processFile(c.normalizePath(path), f, stdout, false)\n\t\t\t\t\tdiags = diags.Append(fileDiags)\n\t\t\t\t\tf.Close()\n\n\t\t\t\t\t// Take note that we processed the file.\n\t\t\t\t\tfmtd = true\n\n\t\t\t\t\t// Don't check the remaining extensions.\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif !fmtd {\n\t\t\t\tdiags = diags.Append(fmt.Errorf(\"Only .tf, .tfvars, and .tftest.hcl files can be processed with terraform fmt\"))\n\t\t\t\tcontinue\n\t\t\t}\n\t\t}\n\t}\n\n\treturn diags\n}\n\nfunc (c *FmtCommand) processFile(path string, r io.Reader, w io.Writer, isStdout bool) tfdiags.Diagnostics {\n\tvar diags tfdiags.Diagnostics\n\n\tlog.Printf(\"[TRACE] terraform fmt: Formatting %s\", path)\n\n\tsrc, err := io.ReadAll(r)\n\tif err != nil {\n\t\tdiags = diags.Append(fmt.Errorf(\"Failed to read %s\", path))\n\t\treturn diags\n\t}","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/fmt.go#L145-L181","documentation":"Returned by FmtCommand.fmt (fmt.go:163) when a target is a regular file (not a directory) whose extension is not in fmtSupportedExts (.tf, .tfvars, .tftest.hcl, .tfmock.hcl, .tfquery.hcl). fmt refuses to process unrelated files to avoid mangling non-HCL content. The message lists the canonical subset even though the internal list now also includes .tfmock.hcl and .tfquery.hcl.","triggerScenarios":"Pointing fmt at a file like README.md, main.tf.json, variables.auto.tfvars.json, policy.json, or any file with a non-supported extension. fmt loops fmtSupportedExts, finds no match, sets fmtd=false, and appends this diagnostic.","commonSituations":"Running `terraform fmt .` in a directory and pointing it directly at a .tf.json file (JSON config is not formatted by fmt); passing a .md or .txt doc by mistake; globbing `terraform fmt *.json`; users expecting fmt to handle JSON variant files.","solutions":["Only pass .tf, .tfvars, .tftest.hcl, .tfmock.hcl, or .tfquery.hcl files to fmt.","For JSON config files (.tf.json etc.), format the JSON manually with `jq .` or your editor — fmt does not touch JSON.","If you meant to format a directory, pass the directory (not individual non-tf files): `terraform fmt ./modules`.","Filter globs: `terraform fmt *.tf *.tfvars` instead of `terraform fmt *`."],"exampleFix":"# before\nterraform fmt variables.tf.json\n# Only .tf, .tfvars, and .tftest.hcl files can be processed with terraform fmt\n\n# after\nterraform fmt variables.tf        # native HCL\njq . variables.tf.json > tmp && mv tmp variables.tf.json  # format JSON separately","handlingStrategy":"type-guard","validationCode":"// Filter inputs to supported extensions before calling fmt\nvar valid []string\nfor _, p := range paths {\n    if isFmtSupported(p) { valid = append(valid, p) } else {\n        log.Printf(\"skipping unsupported file: %s\", p)\n    }\n}\nif len(valid) == 0 { return errors.New(\"no fmt-supported files supplied\") }","typeGuard":"// isFmtSupported reports whether path has an extension terraform fmt will process.\nfunc isFmtSupported(path string) bool {\n    for _, ext := range []string{\".tf\", \".tfvars\", \".tftest.hcl\", \".tfmock.hcl\", \".tfquery.hcl\"} {\n        if strings.HasSuffix(path, ext) { return true }\n    }\n    return false\n}","tryCatchPattern":null,"preventionTips":["Glob only supported extensions when invoking fmt.","Remember fmt does not process .tf.json; format JSON separately.","Filter inputs in wrapper scripts before calling fmt.","Document the supported extension list for your team."],"tags":["cli","fmt","file-type","user-input"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}