hashicorp/terraform · warning
Failed to write %s
Error message
Failed to write %s
What it means
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.
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 c9def3e214)
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>`.
Example fix
# before $ terraform fmt main.tf Failed to write main.tf # file is read-only # after $ chmod u+w main.tf $ terraform fmt main.tf
Defensive patterns
Strategy: validation
Validate before calling
// Confirm the target file is writable before running fmt with -write
if c.write {
if err := os.WriteFile(path, []byte(""), 0644); err != nil {
return fmt.Errorf("%s is not writable: %w", path, err)
}
// restore handled by fmt's own write
} Try / catch
// On write failure, leave the file untouched and emit a warning; do not corrupt the original.
if err := os.WriteFile(path, result, 0644); err != nil {
log.Printf("warning: could not write reformatted %s (%v); original preserved unchanged", path, err)
return err
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Failed to read file %s
- No file or directory at %s
- Error saving -plugin-dir to workspace directory: %s
- Error creating temporary directory: %s
- error deleting workspace %s: %w
AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07).
Data as JSON: /api/errors/54d3e1db3379825d.
Report an issue: GitHub.