{"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":"warning","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/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/fmt.go#L205-L241","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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.","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`.","If wrapping fmt programmatically, keep the stdout reader alive until fmt exits or use a buffer (bytes.Buffer) instead of a live pipe.","Verify the output target is writable and not full before invoking fmt."],"exampleFix":"// before\n$ terraform fmt - < main.tf | head -1\n// after\n$ terraform fmt -check main.tf\n$ terraform fmt -write=true main.tf","handlingStrategy":"validation","validationCode":"// Before invoking fmt from a wrapper, ensure the output sink will stay open.\n// Go example:\nvar buf bytes.Buffer\nexitCode := fmtCmd.Run(args, stdin, &buf) // buffer never closes early\n// Shell: prefer -check/-write over consuming stdout via a pipe that may close","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer `terraform fmt -check` or `-write=true` over piping stdout into another command.","When wrapping fmt, write to an in-memory buffer rather than a live pipe so the reader cannot close early.","Verify the destination file/device is writable and has free space before running fmt."],"tags":["terraform-fmt","io","stdout","broken-pipe"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}