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

  1. 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.
  2. 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`.
  3. If wrapping fmt programmatically, keep the stdout reader alive until fmt exits or use a buffer (bytes.Buffer) instead of a live pipe.
  4. 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

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


AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07). Data as JSON: /api/errors/017c15e7b33bc215. Report an issue: GitHub.