{"record":{"id":"54d3e1db3379825d","repo":"hashicorp/terraform","slug":"failed-to-write-s","errorCode":null,"errorMessage":"Failed to write %s","messagePattern":"Failed to write (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/command/fmt.go","lineNumber":206,"sourceCode":"\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\n\tif !bytes.Equal(src, result) {\n\t\t// Something was changed\n\t\tif c.list {\n\t\t\tfmt.Fprintln(w, path)\n\t\t}\n\t\tif c.write {\n\t\t\terr := os.WriteFile(path, result, 0644)\n\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}","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/fmt.go#L188-L224","documentation":"Returned by FmtCommand.processFile (fmt.go:206) when os.WriteFile fails while writing the reformatted content back to the source file (only when -write is true and the reformatted bytes differ from the original). The file has already been read and reformatted, but the writeback step fails, leaving the original file unchanged.","triggerScenarios":"os.WriteFile(path, result, 0644) returns an error: disk full, permission denied (file marked read-only or owned by another user), path deleted between read and write, read-only filesystem, or the file is on a volume with a write quota exceeded.","commonSituations":"CI runner out of disk; file checked into a read-only source tree (e.g. mounted from a packaged release); user lacks write permission on the .tf file; another process chmod'd the file read-only mid-run; container with read-only mount of the config directory.","solutions":["Free disk space on the volume holding the file (`df -h`).","Make the file and its directory writable: `chmod u+w <file>` (and the parent dir for the write temp).","If the tree is intentionally read-only, run fmt with -write=false and redirect output, or copy the tree to a writable location first.","Ensure the Terraform process owns or can write the target file: `ls -l <file>`."],"exampleFix":"# before\n$ terraform fmt main.tf\nFailed to write main.tf   # file is read-only\n\n# after\n$ chmod u+w main.tf\n$ terraform fmt main.tf","handlingStrategy":"validation","validationCode":"// Confirm the target file is writable before running fmt with -write\nif c.write {\n    if err := os.WriteFile(path, []byte(\"\"), 0644); err != nil {\n        return fmt.Errorf(\"%s is not writable: %w\", path, err)\n    }\n    // restore handled by fmt's own write\n}","typeGuard":null,"tryCatchPattern":"// On write failure, leave the file untouched and emit a warning; do not corrupt the original.\nif err := os.WriteFile(path, result, 0644); err != nil {\n    log.Printf(\"warning: could not write reformatted %s (%v); original preserved unchanged\", path, err)\n    return err\n}","preventionTips":["Ensure config files and their directories are writable by the Terraform process.","Keep config trees on writable storage; copy out of read-only release mounts before formatting.","Free disk space before running fmt with -write in CI.","Use -write=false and redirect output for read-only trees."],"tags":["cli","fmt","filesystem","write","permissions","disk-full"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}