{"record":{"id":"c9e8f3d0ab72c396","repo":"yorukot/superfile","slug":"error-writing-file-w","errorCode":null,"errorMessage":"error writing file : %w","messagePattern":"error writing file : %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/pkg/utils/file_utils.go","lineNumber":33,"sourceCode":"\t\"github.com/pelletier/go-toml/v2\"\n\n\t\"github.com/charmbracelet/x/ansi\"\n\t\"golang.org/x/text/encoding/unicode\"\n\t\"golang.org/x/text/transform\"\n)\n\n// Utility functions related to file operations\n// Note : This is not used anymore as we use os.WriteAt to\n// fix toml files now, but we will still keep it for later use.\nfunc WriteTomlData(filePath string, data interface{}) error {\n\ttomlData, err := toml.Marshal(data)\n\tif err != nil {\n\t\t// return a wrapped error\n\t\treturn fmt.Errorf(\"error encoding data : %w\", err)\n\t}\n\terr = os.WriteFile(filePath, tomlData, ConfigFilePerm)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error writing file : %w\", err)\n\t}\n\treturn nil\n}\n\n// Helper function to load and validate TOML files with field checking\n// errorPrefix is appended before every error message\nfunc LoadTomlFile(filePath string, defaultData string, target interface{},\n\tfixFlag bool, ignoreMissingFields bool) error {\n\t// Initialize with default config\n\t_ = toml.Unmarshal([]byte(defaultData), target)\n\n\tdata, err := os.ReadFile(filePath)\n\tif err != nil {\n\t\treturn &TomlLoadError{\n\t\t\tuserMessage:  \"config file doesn't exist\",\n\t\t\twrappedError: err,\n\t\t}\n\t}","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/yorukot/superfile/blob/b72f550bc6e75913f48eb49fdd258e1a2df2fe88/src/pkg/utils/file_utils.go#L15-L51","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nerr := utils.WriteTomlData(cfgPath, cfg)\n// after\nif err := utils.CreateDirectories(filepath.Dir(cfgPath)); err != nil { return err }\nif err := utils.WriteTomlData(cfgPath, cfg); err != nil {\n    return fmt.Errorf(\"saving config to %s: %w\", cfgPath, err)\n}","handlingStrategy":"validation","validationCode":"func canWrite(path string) error {\n    dir := filepath.Dir(path)\n    info, err := os.Stat(dir)\n    if err != nil { return err }\n    if !info.IsDir() { return fmt.Errorf(\"%s is not a directory\", dir) }\n    f, err := os.CreateTemp(dir, \".wtest*\")\n    if err != nil { return err }\n    f.Close(); os.Remove(f.Name())\n    return nil\n}","typeGuard":null,"tryCatchPattern":"if err := utils.WriteTomlData(path, cfg); err != nil {\n    if strings.Contains(err.Error(), \"error writing file\") {\n        return fmt.Errorf(\"cannot save config to %s (check dir/permissions/disk): %w\", path, err)\n    }\n    return err\n}","preventionTips":["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."],"tags":["toml","file-write","filesystem","permissions"],"backgroundTag":"file-write-failed","analyzedSha":"b72f550bc6e75913f48eb49fdd258e1a2df2fe88","analyzedAt":"2026-09-01T04:38:08.254Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}