hashicorp/terraform · error
Failed to read
Error message
Failed to read %s
What it means
Thrown inside processFile when io.ReadAll(r) returns an error while reading the full contents of the reader (file or stdin). The reader has already been opened, so this is an I/O failure during the read itself, not an open failure. The path is included to identify which input failed.
Solutions
- Re-run terraform fmt after confirming the file still exists and is readable: `ls -l <path>`.
- If using stdin via a pipe, ensure the upstream command completes successfully and does not close the pipe early.
- For network filesystems, verify mount health and stability, then retry.
Defensive patterns
Strategy: try-catch
Validate before calling
// Pre-check readability before handing the file to fmt processing.
func canReadFully(path string) error {
f, err := os.Open(path)
if err != nil { return err }
defer f.Close()
_, err = io.ReadAll(f)
return err
} Try / catch
// Wrap fmt invocation and surface I/O errors with context.
diags := c.processFile(path, r, w, false)
for _, d := range diags {
if strings.Contains(d.Description().Summary, "Failed to read") {
log.Printf("fmt read failed for %s: retry or skip", path)
}
} Prevention
- Validate file readability before batch fmt runs.
- Avoid piping stdin from commands that may exit early.
- For network filesystems, prefer copying files locally before formatting.
When it happens
Trigger: A file that was opened successfully but becomes unreadable mid-stream (e.g., file truncated/removed between open and read, a pipe/stdin that closes early, a network-mounted filesystem I/O error, or a special device file that errors on read).
Common situations: Reading from a piped stdin where the upstream process exits prematurely; NFS/CIFS mount hiccup; file deleted by another process between os.Open and io.ReadAll; reading from /dev or fifo with unexpected behavior.
Related errors
- can not read private key from
- can't read
- Cannot read directory
- error deleting workspace
- Error loading statefile
AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11).
Data as JSON: /api/errors/15359d5cef988ace.
Report an issue: GitHub.
Appendix: source
Thrown at internal/command/fmt.go:179
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
}
// Register this path as a synthetic configuration source, so that any
// diagnostic errors can include the source code snippet
c.registerSynthConfigSource(path, src)
// File must be parseable as HCL native syntax before we'll try to format
// it. If not, the formatter is likely to make drastic changes that would
// be hard for the user to undo.
_, syntaxDiags := hclsyntax.ParseConfig(src, path, hcl.Pos{Line: 1, Column: 1})
if syntaxDiags.HasErrors() {
diags = diags.Append(syntaxDiags)
return diags
}
result := c.formatSourceCode(src, path)
View on GitHub (pinned to d32a084675)