{"record":{"id":"60fa93002203c607","repo":"yorukot/superfile","slug":"failed-to-create-file-s-w","errorCode":null,"errorMessage":"failed to create file %s: %w","messagePattern":"failed to create file (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/pkg/utils/file_utils.go","lineNumber":259,"sourceCode":"// Create all dirs that does not already exists\nfunc CreateDirectories(dirs ...string) error {\n\tfor _, dir := range dirs {\n\t\tif dir == \"\" {\n\t\t\tcontinue\n\t\t}\n\t\tif err := os.MkdirAll(dir, ConfigDirPerm); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to create directory %s: %w\", dir, err)\n\t\t}\n\t}\n\treturn nil\n}\n\n// Create all files if they do not exists yet\nfunc CreateFiles(files ...string) error {\n\tfor _, file := range files {\n\t\tif _, err := os.Stat(file); os.IsNotExist(err) {\n\t\t\tif err = os.WriteFile(file, nil, ConfigFilePerm); err != nil {\n\t\t\t\treturn fmt.Errorf(\"failed to create file %s: %w\", file, err)\n\t\t\t}\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc ReadFileContent(filepath string, maxLineLength int, previewLine int) (string, error) {\n\tvar resultBuilder strings.Builder\n\tfile, err := os.Open(filepath)\n\tif err != nil {\n\t\treturn resultBuilder.String(), err\n\t}\n\tdefer file.Close()\n\n\treader := transform.NewReader(file, unicode.BOMOverride(unicode.UTF8.NewDecoder()))\n\tscanner := bufio.NewScanner(reader)\n\tlineCount := 0\n\tfor scanner.Scan() {","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/yorukot/superfile/blob/b72f550bc6e75913f48eb49fdd258e1a2df2fe88/src/pkg/utils/file_utils.go#L241-L277","documentation":"CreateFiles checks each path with os.Stat and creates missing files as empty files via os.WriteFile with ConfigFilePerm; this error wraps a failure of that write. It only fires for files that don't already exist, so the cause is almost always the surrounding filesystem, not the file content (nil).","triggerScenarios":"InitConfigFile (or tests like TestClipboardRender_Empty) calling CreateFiles when the containing directory doesn't exist, the process lacks write permission, the path is actually a directory, or the disk is full.","commonSituations":"Config files declared under directories that were never created (CreateDirectories skipped or failed); read-only config mounts; permission drift after running once as root; path typo pointing at a directory.","solutions":["Read the wrapped error for the exact path and errno (EACCES/ENOENT/EISDIR).","Ensure CreateDirectories runs for all parent dirs before CreateFiles.","Verify the path is not an existing directory and the parent is writable by the current user.","Check ConfigFilePerm against the runtime user's umask/ownership.","Pre-create parent directories in deployment scripts and keep config dirs user-writable."],"exampleFix":"// before\nerr := utils.CreateFiles(cfgPath) // parent dir missing\n// after\nif err := utils.CreateDirectories(filepath.Dir(cfgPath)); err != nil { return err }\nif err := utils.CreateFiles(cfgPath); err != nil { return err }","handlingStrategy":"validation","validationCode":"func ensureCreatableFile(path string) error {\n    dir := filepath.Dir(path)\n    if info, err := os.Stat(path); err == nil && info.IsDir() {\n        return fmt.Errorf(\"%s is a directory\", path)\n    }\n    f, err := os.CreateTemp(dir, \".t*\")\n    if err != nil { return err }\n    f.Close(); os.Remove(f.Name())\n    return nil\n}","typeGuard":null,"tryCatchPattern":"if err := utils.CreateFiles(files...); err != nil {\n    return fmt.Errorf(\"config file setup failed: %w\", err)\n}","preventionTips":["Create parent directories before file creation (CreateDirectories then CreateFiles).","Verify configured paths are files, not directories.","Keep config dirs writable for the unprivileged runtime user.","Log the exact failing path from the wrapped error when triaging."],"tags":["file-write","filesystem","permissions","config"],"backgroundTag":"file-write-failed","analyzedSha":"b72f550bc6e75913f48eb49fdd258e1a2df2fe88","analyzedAt":"2026-09-01T04:38:08.254Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}