hashicorp/terraform · warning

Failed to read %s

Error message

Failed to read %s

What it means

Returned by FmtCommand.processFile (fmt.go:179) when io.ReadAll(r) fails while reading the content of a file or stdin stream slated for formatting. processFile is called for both stdin ('<stdin>') and on-disk files; the path label distinguishes them. This is a low-level I/O failure after the source was already opened.

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 c9def3e214)

Solutions

  1. For stdin, ensure the producer process completes before terraform fmt reads — write to a temp file first, then `terraform fmt - < tmp.tf`.
  2. For on-disk files, retry the read; if it persists, run `fsck`/check disk health and restore from version control.
  3. If on a network filesystem, copy the file to local storage and fmt the copy.
  4. Close nothing prematurely: pipe with `cat file | terraform fmt -` only if `cat` is reliable; prefer file paths.

Example fix

# before
$ flaky-gen | terraform fmt -
Failed to read <stdin>   # producer died, pipe closed

# after
$ flaky-gen > tmp.tf || { echo 'producer failed'; exit 1; }
$ terraform fmt - < tmp.tf
Defensive patterns

Strategy: try-catch

Validate before calling

// Read stdin into a buffer first so I/O errors surface before fmt runs
buf, err := io.ReadAll(os.Stdin)
if err != nil {
    return fmt.Errorf("failed to read stdin: %w", err)
}
if _, err := fcmd.fmt([]string{}, bytes.NewReader(buf), out); err != nil { return err }

Try / catch

// For file inputs, retry once on a transient read error; for stdin, fail fast with a clear message.
if _, err := io.ReadAll(r); err != nil {
    if path != "<stdin>" && isTransient(err) {
        time.Sleep(100 * time.Millisecond)
        // reopen and retry once
    } else {
        return fmt.Errorf("%s unreadable: %w", path, err)
    }
}

Prevention

When it happens

Trigger: io.ReadAll returns an error: a read error on the underlying reader (disk I/O error, network filesystem read failure for an opened file, stdin pipe broken because the producer process exited early, or a file reader that hits a bad block).

Common situations: Piping into fmt from a process that crashes (`broken pipe`): `terraform fmt - < <(flaky-cmd)`; reading a .tf file on a flaky NFS share; stdin closed prematurely by a CI step; disk hardware error on a workstation; a file truncated by another process mid-read.

Related errors


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