hashicorp/terraform · warning
Failed to write result
Error message
Failed to write result
What it means
Emitted by terraform fmt's processFile when writing the reformatted source to its output writer fails. The branch runs only when none of -list, -write, or -diff are active, i.e. the canonical default for reading from STDIN or echoing formatted bytes to stdout. Because fmt.go:221 calls w.Write(result) on the command's output writer, any I/O failure on that writer (broken pipe, closed fd) surfaces here. The error is diagnostic-level and aborts the current file's processing without touching disk.
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 c9def3e214)
Solutions
- Avoid piping terraform fmt into commands that close the pipe early; if you only need to check formatting use `terraform fmt -check` instead of consuming stdout.
- Redirect fmt output to a file on a volume with adequate space rather than to another command's stdin: `terraform fmt < main.tf > main.tf.out`.
- If wrapping fmt programmatically, keep the stdout reader alive until fmt exits or use a buffer (bytes.Buffer) instead of a live pipe.
- Verify the output target is writable and not full before invoking fmt.
Example fix
// before $ terraform fmt - < main.tf | head -1 // after $ terraform fmt -check main.tf $ terraform fmt -write=true main.tf
Defensive patterns
Strategy: validation
Validate before calling
// Before invoking fmt from a wrapper, ensure the output sink will stay open. // Go example: var buf bytes.Buffer exitCode := fmtCmd.Run(args, stdin, &buf) // buffer never closes early // Shell: prefer -check/-write over consuming stdout via a pipe that may close
Prevention
- Prefer `terraform fmt -check` or `-write=true` over piping stdout into another command.
- When wrapping fmt, write to an in-memory buffer rather than a live pipe so the reader cannot close early.
- Verify the destination file/device is writable and has free space before running fmt.
When it happens
Trigger: Running `terraform fmt -` and piping stdout into a downstream command that exits early (e.g. `terraform fmt - | head -1`, `terraform fmt - | xargs --no-run-if-empty`), or redirecting stdout to a closed/full device. Also triggers if stdout is closed by a wrapping process or a CI runner that kills the pipe mid-stream.
Common situations: Shell pipelines that terminate fmt's stdout before fmt finishes writing, such as `terraform fmt < main.tf | head`. Disk-full when redirecting to a file. Containers/CI where stdout is connected to a collector that drops the connection. Running fmt in a script whose stdout was redirected to a since-unmounted volume.
Related errors
- Cannot read directory %s
- Failed to read state file from %v: %v
- Failed to read remote state: %s
- unable to read 'content' from response: %w
- failed to read existing lock file content: %w
AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07).
Data as JSON: /api/errors/017c15e7b33bc215.
Report an issue: GitHub.