{"record":{"id":"15359d5cef988ace","repo":"hashicorp/terraform","slug":"failed-to-read-s","errorCode":null,"errorMessage":"Failed to read %s","messagePattern":"Failed to read (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/command/fmt.go","lineNumber":179,"sourceCode":"\n\t\t\tif !fmtd {\n\t\t\t\tdiags = diags.Append(fmt.Errorf(\"Only .tf, .tfvars, and .tftest.hcl files can be processed with terraform fmt\"))\n\t\t\t\tcontinue\n\t\t\t}\n\t\t}\n\t}\n\n\treturn diags\n}\n\nfunc (c *FmtCommand) processFile(path string, r io.Reader, w io.Writer, isStdout bool) tfdiags.Diagnostics {\n\tvar diags tfdiags.Diagnostics\n\n\tlog.Printf(\"[TRACE] terraform fmt: Formatting %s\", path)\n\n\tsrc, err := io.ReadAll(r)\n\tif err != nil {\n\t\tdiags = diags.Append(fmt.Errorf(\"Failed to read %s\", path))\n\t\treturn diags\n\t}\n\n\t// Register this path as a synthetic configuration source, so that any\n\t// diagnostic errors can include the source code snippet\n\tc.registerSynthConfigSource(path, src)\n\n\t// File must be parseable as HCL native syntax before we'll try to format\n\t// it. If not, the formatter is likely to make drastic changes that would\n\t// be hard for the user to undo.\n\t_, syntaxDiags := hclsyntax.ParseConfig(src, path, hcl.Pos{Line: 1, Column: 1})\n\tif syntaxDiags.HasErrors() {\n\t\tdiags = diags.Append(syntaxDiags)\n\t\treturn diags\n\t}\n\n\tresult := c.formatSourceCode(src, path)\n","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/fmt.go#L161-L197","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["For stdin, ensure the producer process completes before terraform fmt reads — write to a temp file first, then `terraform fmt - < tmp.tf`.","For on-disk files, retry the read; if it persists, run `fsck`/check disk health and restore from version control.","If on a network filesystem, copy the file to local storage and fmt the copy.","Close nothing prematurely: pipe with `cat file | terraform fmt -` only if `cat` is reliable; prefer file paths."],"exampleFix":"# before\n$ flaky-gen | terraform fmt -\nFailed to read <stdin>   # producer died, pipe closed\n\n# after\n$ flaky-gen > tmp.tf || { echo 'producer failed'; exit 1; }\n$ terraform fmt - < tmp.tf","handlingStrategy":"try-catch","validationCode":"// Read stdin into a buffer first so I/O errors surface before fmt runs\nbuf, err := io.ReadAll(os.Stdin)\nif err != nil {\n    return fmt.Errorf(\"failed to read stdin: %w\", err)\n}\nif _, err := fcmd.fmt([]string{}, bytes.NewReader(buf), out); err != nil { return err }","typeGuard":null,"tryCatchPattern":"// For file inputs, retry once on a transient read error; for stdin, fail fast with a clear message.\nif _, err := io.ReadAll(r); err != nil {\n    if path != \"<stdin>\" && isTransient(err) {\n        time.Sleep(100 * time.Millisecond)\n        // reopen and retry once\n    } else {\n        return fmt.Errorf(\"%s unreadable: %w\", path, err)\n    }\n}","preventionTips":["Materialize piped input into a temp file before formatting so a producer crash is detectable up front.","Keep .tf files on local, healthy storage.","In CI, verify the producer's exit code before piping to fmt.","Avoid network filesystems for files being formatted."],"tags":["cli","fmt","io","stdin","network-filesystem"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}