{"record":{"id":"017c15e7b33bc215","repo":"hashicorp/terraform","slug":"failed-to-write-result","errorCode":null,"errorMessage":"Failed to write result","messagePattern":"Failed to write result","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/fmt.go","lineNumber":223,"sourceCode":"\t\t\tif err != nil {\n\t\t\t\tdiags = diags.Append(fmt.Errorf(\"Failed to write %s\", path))\n\t\t\t\treturn diags\n\t\t\t}\n\t\t}\n\t\tif c.diff {\n\t\t\tdiff, err := bytesDiff(src, result, path)\n\t\t\tif err != nil {\n\t\t\t\tdiags = diags.Append(fmt.Errorf(\"Failed to generate diff for %s: %s\", path, err))\n\t\t\t\treturn diags\n\t\t\t}\n\t\t\tw.Write(diff)\n\t\t}\n\t}\n\n\tif !c.list && !c.write && !c.diff {\n\t\t_, err = w.Write(result)\n\t\tif err != nil {\n\t\t\tdiags = diags.Append(fmt.Errorf(\"Failed to write result\"))\n\t\t}\n\t}\n\n\treturn diags\n}\n\nfunc (c *FmtCommand) processDir(path string, stdout io.Writer) tfdiags.Diagnostics {\n\tvar diags tfdiags.Diagnostics\n\n\tlog.Printf(\"[TRACE] terraform fmt: looking for files in %s\", path)\n\n\tentries, err := os.ReadDir(path)\n\tif err != nil {\n\t\tswitch {\n\t\tcase os.IsNotExist(err):\n\t\t\tdiags = diags.Append(fmt.Errorf(\"There is no configuration directory at %s\", path))\n\t\tdefault:\n\t\t\t// ReadDir does not produce error messages that are end-user-appropriate,","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/fmt.go#L205-L241","documentation":"Thrown by processFile when none of -list, -write, -diff are set (the default stdout mode) and writing the formatted result to the output writer fails. This is a write failure on stdout or the configured output buffer.","triggerScenarios":"Running `terraform fmt` (or `terraform fmt -` for stdin) where the stdout pipe is broken or closed — e.g., piping to `head` which exits early, or redirecting to a full/unwritable destination.","commonSituations":"Piping formatted output through a consumer that closes the pipe (head, less quitting); redirecting to a full disk; CI capturing stdout to a closed file descriptor.","solutions":["Avoid piping fmt output through commands that exit before consuming the full stream (use a file redirect instead).","Use -write to write results directly to files rather than stdout.","Verify the output destination is writable and has space."],"exampleFix":"// before\n$ terraform fmt - | head -1\n// after\n$ terraform fmt -write .","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// When consuming fmt stdout in a pipeline, ignore EPIPE/broken-pipe.\nout, err := cmd.Output()\nif err != nil && !errors.Is(err, syscall.EPIPE) {\n    return err\n}","preventionTips":["Avoid piping fmt stdout through pagers/head that exit early.","Prefer -write for in-place formatting over stdout streaming.","Redirect to a file rather than through a consumer that may close the pipe."],"tags":["terraform-fmt","stdout","io","broken-pipe"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}