{"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":"error","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/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/fmt.go#L161-L197","documentation":"Thrown inside processFile when io.ReadAll(r) returns an error while reading the full contents of the reader (file or stdin). The reader has already been opened, so this is an I/O failure during the read itself, not an open failure. The path is included to identify which input failed.","triggerScenarios":"A file that was opened successfully but becomes unreadable mid-stream (e.g., file truncated/removed between open and read, a pipe/stdin that closes early, a network-mounted filesystem I/O error, or a special device file that errors on read).","commonSituations":"Reading from a piped stdin where the upstream process exits prematurely; NFS/CIFS mount hiccup; file deleted by another process between os.Open and io.ReadAll; reading from /dev or fifo with unexpected behavior.","solutions":["Re-run terraform fmt after confirming the file still exists and is readable: `ls -l <path>`.","If using stdin via a pipe, ensure the upstream command completes successfully and does not close the pipe early.","For network filesystems, verify mount health and stability, then retry."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Pre-check readability before handing the file to fmt processing.\nfunc canReadFully(path string) error {\n    f, err := os.Open(path)\n    if err != nil { return err }\n    defer f.Close()\n    _, err = io.ReadAll(f)\n    return err\n}","typeGuard":null,"tryCatchPattern":"// Wrap fmt invocation and surface I/O errors with context.\ndiags := c.processFile(path, r, w, false)\nfor _, d := range diags {\n    if strings.Contains(d.Description().Summary, \"Failed to read\") {\n        log.Printf(\"fmt read failed for %s: retry or skip\", path)\n    }\n}","preventionTips":["Validate file readability before batch fmt runs.","Avoid piping stdin from commands that may exit early.","For network filesystems, prefer copying files locally before formatting."],"tags":["terraform-fmt","io","filesystem","stdin"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}