{"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":"error","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/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/fmt.go#L188-L224","documentation":"Thrown by processFile when the -write flag is set, formatting changed the file, and os.WriteFile fails to write the reformatted bytes back to disk. This is a write-permission or disk-capacity failure on the target file path.","triggerScenarios":"Running `terraform fmt -write` (default for files) on a file whose path is read-only, on a full disk, on a read-only filesystem, or where the user lacks write permission. Also fires if the directory was removed mid-operation.","commonSituations":"Files checked into a read-only VCS checkout; Docker volume mount mounted ro; CI running as a user without write access to the module directory; full disk / inode exhaustion.","solutions":["Check and fix write permissions on the file and directory: `chmod u+w <path>`.","Free disk/inode space if the volume is full (`df -h`, `df -i`).","Ensure the working directory is not on a read-only mount; remount rw or copy files to a writable location."],"exampleFix":"$ chmod u+w main.tf && terraform fmt -write main.tf","handlingStrategy":"validation","validationCode":"// Verify writability before running fmt -write.\nfunc isWritable(path string) bool {\n    fi, err := os.Stat(path)\n    if err != nil { return false }\n    return fi.Mode().Perm()&0200 != 0\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure files are checked out writable before fmt (avoid read-only VCS workspaces).","Run fmt as the file owner or a user with write access.","Check disk space before large batch -write operations."],"tags":["terraform-fmt","filesystem","permissions","write"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}