hashicorp/terraform · error
Only .tf, .tfvars, and .tftest.hcl files can be processed…
Error message
Only .tf, .tfvars, and .tftest.hcl files can be processed with terraform fmt
What it means
Thrown by `terraform fmt` when a path passed on the command line refers to a regular file whose name does not end in one of the supported extensions (.tf, .tfvars, .tftest.hcl). The command iterates fmtSupportedExts; if none match, fmtd stays false and the file is rejected. This guards the formatter from operating on non-HCL files where formatting would be meaningless or destructive.
Solutions
- Rename or restrict the argument to a file with a .tf, .tfvars, or .tftest.hcl extension.
- If formatting a directory, pass the directory path instead of individual non-HCL files so unsupported files are skipped automatically.
- Convert JSON-syntax config (.tf.json) to native HCL (.tf) before formatting, since fmt does not support JSON syntax.
Example fix
// before $ terraform fmt main.tf.json // after $ terraform fmt main.tf
Defensive patterns
Strategy: validation
Validate before calling
// Before calling fmt on a path, verify the extension is supported.
func isFmtSupported(path string) bool {
for _, ext := range []string{".tf", ".tfvars", ".tftest.hcl"} {
if strings.HasSuffix(path, ext) {
return true
}
}
return false
}
// usage:
// if !isFmtSupported(p) { return fmt.Errorf("skip: %s unsupported", p) } Prevention
- Glob inputs with supported extensions before invoking fmt: *.tf, *.tfvars, *.tftest.hcl.
- Do not pass .tf.json files to fmt; convert them to native HCL first.
- Prefer directory arguments over individual files so unsupported files are skipped silently.
When it happens
Trigger: Running `terraform fmt main.tf.json`, `terraform fmt README.md`, `terraform fmt script.sh`, or passing any file with an unsupported extension directly (not a directory). Directory traversal (processDir) skips non-matching files silently, so this only fires for explicit file arguments.
Common situations: Passing a JSON-syntax Terraform file (.tf.json) which is NOT supported by fmt; passing a .sh hook script; mistyping a filename; attempting to format a generated file like .terraform.lock.hcl.
Related errors
- at most 1 action can be invoked per operation
- A managed resource address is required. Importing into a…
- architecture portion must not contain whitespace
- auth must be one of ' ' or ' ' or ' ' or ' ' or ' ' or
- blank output
AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11).
Data as JSON: /api/errors/a668726d324c91ec.
Report an issue: GitHub.
Appendix: source
Thrown at internal/command/fmt.go:163
// so we'll need to simplify here.
diags = diags.Append(fmt.Errorf("Failed to read file %s", path))
continue
}
fileDiags := c.processFile(c.normalizePath(path), f, stdout, false)
diags = diags.Append(fileDiags)
f.Close()
// Take note that we processed the file.
fmtd = true
// Don't check the remaining extensions.
break
}
}
if !fmtd {
diags = diags.Append(fmt.Errorf("Only .tf, .tfvars, and .tftest.hcl files can be processed with terraform fmt"))
continue
}
}
}
return diags
}
func (c *FmtCommand) processFile(path string, r io.Reader, w io.Writer, isStdout bool) tfdiags.Diagnostics {
var diags tfdiags.Diagnostics
log.Printf("[TRACE] terraform fmt: Formatting %s", path)
src, err := io.ReadAll(r)
if err != nil {
diags = diags.Append(fmt.Errorf("Failed to read %s", path))
return diags
}View on GitHub (pinned to d32a084675)