hashicorp/terraform · error
Failed to write
Error message
Failed to write %s
What it means
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.
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.
Example fix
$ chmod u+w main.tf && terraform fmt -write main.tf
Defensive patterns
Strategy: validation
Validate before calling
// Verify writability before running fmt -write.
func isWritable(path string) bool {
fi, err := os.Stat(path)
if err != nil { return false }
return fi.Mode().Perm()&0200 != 0
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Cannot read directory
- Error saving -plugin-dir to workspace directory
- cannot create temporary file to update credentials
- cannot read
- cannot search
AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11).
Data as JSON: /api/errors/54d3e1db3379825d.
Report an issue: GitHub.
Appendix: source
Thrown at internal/command/fmt.go:206
// it. If not, the formatter is likely to make drastic changes that would
// be hard for the user to undo.
_, syntaxDiags := hclsyntax.ParseConfig(src, path, hcl.Pos{Line: 1, Column: 1})
if syntaxDiags.HasErrors() {
diags = diags.Append(syntaxDiags)
return diags
}
result := c.formatSourceCode(src, path)
if !bytes.Equal(src, result) {
// Something was changed
if c.list {
fmt.Fprintln(w, path)
}
if c.write {
err := os.WriteFile(path, result, 0644)
if err != nil {
diags = diags.Append(fmt.Errorf("Failed to write %s", path))
return diags
}
}
if c.diff {
diff, err := bytesDiff(src, result, path)
if err != nil {
diags = diags.Append(fmt.Errorf("Failed to generate diff for %s: %s", path, err))
return diags
}
w.Write(diff)
}
}
if !c.list && !c.write && !c.diff {
_, err = w.Write(result)
if err != nil {
diags = diags.Append(fmt.Errorf("Failed to write result"))
}View on GitHub (pinned to d32a084675)