yorukot/superfile · error
error writing file
Error message
error writing file : %w
What it means
WriteTomlData writes the marshaled TOML bytes with os.WriteFile using ConfigFilePerm; this error wraps any failure from that write. It means serialization succeeded but the filesystem write did not. Common causes are path and permission problems rather than data problems.
Solutions
- Check the parent directory exists (run CreateDirectories first) and is writable.
- Verify permissions: `ls -la <file>`; chown/chmod or fix ConfigFilePerm if too restrictive for the runtime user.
- Confirm the path points to a file, not a directory.
- Check disk space (`df -h`) and mount state (`mount | grep <dir>`) for read-only filesystems.
- Handle the error at the call site and surface it to the user instead of silently dropping config saves.
Example fix
// before
err := utils.WriteTomlData(cfgPath, cfg)
// after
if err := utils.CreateDirectories(filepath.Dir(cfgPath)); err != nil { return err }
if err := utils.WriteTomlData(cfgPath, cfg); err != nil {
return fmt.Errorf("saving config to %s: %w", cfgPath, err)
} Defensive patterns
Strategy: validation
Validate before calling
func canWrite(path string) error {
dir := filepath.Dir(path)
info, err := os.Stat(dir)
if err != nil { return err }
if !info.IsDir() { return fmt.Errorf("%s is not a directory", dir) }
f, err := os.CreateTemp(dir, ".wtest*")
if err != nil { return err }
f.Close(); os.Remove(f.Name())
return nil
} Try / catch
if err := utils.WriteTomlData(path, cfg); err != nil {
if strings.Contains(err.Error(), "error writing file") {
return fmt.Errorf("cannot save config to %s (check dir/permissions/disk): %w", path, err)
}
return err
} Prevention
- Always run CreateDirectories on the parent before saving config files.
- Deploy with user-writable config directories.
- Monitor disk space on hosts running the app.
- Never ignore the error from WriteTomlData; surface it to the user.
When it happens
Trigger: Calling WriteTomlData when the target path's directory doesn't exist, the process lacks write permission on the file/directory, the path is a directory, the disk is full, or the file is locked/readonly.
Common situations: Read-only config dirs after package installs; saving config before CreateDirectories ran; running under a different user than the config owner; container filesystems mounted read-only; full disks on CI machines.
Understand the failure class
Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.
Related errors
- failed to create file
- failed to initialize json file
- cannot access source path
- failed to create destination directory
- failed to create destination file
AI-assisted analysis of yorukot/superfile@b72f550bc6 (2026-09-01).
Data as JSON: /api/errors/c9e8f3d0ab72c396.
Report an issue: GitHub.
Appendix: source
Thrown at src/pkg/utils/file_utils.go:33
"github.com/pelletier/go-toml/v2"
"github.com/charmbracelet/x/ansi"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
)
// Utility functions related to file operations
// Note : This is not used anymore as we use os.WriteAt to
// fix toml files now, but we will still keep it for later use.
func WriteTomlData(filePath string, data interface{}) error {
tomlData, err := toml.Marshal(data)
if err != nil {
// return a wrapped error
return fmt.Errorf("error encoding data : %w", err)
}
err = os.WriteFile(filePath, tomlData, ConfigFilePerm)
if err != nil {
return fmt.Errorf("error writing file : %w", err)
}
return nil
}
// Helper function to load and validate TOML files with field checking
// errorPrefix is appended before every error message
func LoadTomlFile(filePath string, defaultData string, target interface{},
fixFlag bool, ignoreMissingFields bool) error {
// Initialize with default config
_ = toml.Unmarshal([]byte(defaultData), target)
data, err := os.ReadFile(filePath)
if err != nil {
return &TomlLoadError{
userMessage: "config file doesn't exist",
wrappedError: err,
}
}View on GitHub (pinned to b72f550bc6)