hashicorp/terraform · error
Failed to write result
Error message
Failed to write result
What it means
Thrown by processFile when none of -list, -write, -diff are set (the default stdout mode) and writing the formatted result to the output writer fails. This is a write failure on stdout or the configured output buffer.
Solutions
- Avoid piping fmt output through commands that exit before consuming the full stream (use a file redirect instead).
- Use -write to write results directly to files rather than stdout.
- Verify the output destination is writable and has space.
Example fix
// before $ terraform fmt - | head -1 // after $ terraform fmt -write .
Defensive patterns
Strategy: try-catch
Try / catch
// When consuming fmt stdout in a pipeline, ignore EPIPE/broken-pipe.
out, err := cmd.Output()
if err != nil && !errors.Is(err, syscall.EPIPE) {
return err
} Prevention
- Avoid piping fmt stdout through pagers/head that exit early.
- Prefer -write for in-place formatting over stdout streaming.
- Redirect to a file rather than through a consumer that may close the pipe.
When it happens
Trigger: Running `terraform fmt` (or `terraform fmt -` for stdin) where the stdout pipe is broken or closed — e.g., piping to `head` which exits early, or redirecting to a full/unwritable destination.
Common situations: Piping formatted output through a consumer that closes the pipe (head, less quitting); redirecting to a full disk; CI capturing stdout to a closed file descriptor.
Related errors
AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11).
Data as JSON: /api/errors/017c15e7b33bc215.
Report an issue: GitHub.
Appendix: source
Thrown at internal/command/fmt.go:223
if err != nil {
diags = diags.Append(fmt.Errorf("Failed to write %s", path))
return diags
}
}
if c.diff {
diff, err := bytesDiff(src, result, path)
if err != nil {
diags = diags.Append(fmt.Errorf("Failed to generate diff for %s: %s", path, err))
return diags
}
w.Write(diff)
}
}
if !c.list && !c.write && !c.diff {
_, err = w.Write(result)
if err != nil {
diags = diags.Append(fmt.Errorf("Failed to write result"))
}
}
return diags
}
func (c *FmtCommand) processDir(path string, stdout io.Writer) tfdiags.Diagnostics {
var diags tfdiags.Diagnostics
log.Printf("[TRACE] terraform fmt: looking for files in %s", path)
entries, err := os.ReadDir(path)
if err != nil {
switch {
case os.IsNotExist(err):
diags = diags.Append(fmt.Errorf("There is no configuration directory at %s", path))
default:
// ReadDir does not produce error messages that are end-user-appropriate,View on GitHub (pinned to d32a084675)