yorukot/superfile · error
failed to create file %s: %w
Error message
failed to create file %s: %w
What it means
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).
Source
Thrown at src/pkg/utils/file_utils.go:259
// Create all dirs that does not already exists
func CreateDirectories(dirs ...string) error {
for _, dir := range dirs {
if dir == "" {
continue
}
if err := os.MkdirAll(dir, ConfigDirPerm); err != nil {
return fmt.Errorf("failed to create directory %s: %w", dir, err)
}
}
return nil
}
// Create all files if they do not exists yet
func CreateFiles(files ...string) error {
for _, file := range files {
if _, err := os.Stat(file); os.IsNotExist(err) {
if err = os.WriteFile(file, nil, ConfigFilePerm); err != nil {
return fmt.Errorf("failed to create file %s: %w", file, err)
}
}
}
return nil
}
func ReadFileContent(filepath string, maxLineLength int, previewLine int) (string, error) {
var resultBuilder strings.Builder
file, err := os.Open(filepath)
if err != nil {
return resultBuilder.String(), err
}
defer file.Close()
reader := transform.NewReader(file, unicode.BOMOverride(unicode.UTF8.NewDecoder()))
scanner := bufio.NewScanner(reader)
lineCount := 0
for scanner.Scan() {View on GitHub (pinned to b72f550bc6)
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.
Example fix
// before
err := utils.CreateFiles(cfgPath) // parent dir missing
// after
if err := utils.CreateDirectories(filepath.Dir(cfgPath)); err != nil { return err }
if err := utils.CreateFiles(cfgPath); err != nil { return err } Defensive patterns
Strategy: validation
Validate before calling
func ensureCreatableFile(path string) error {
dir := filepath.Dir(path)
if info, err := os.Stat(path); err == nil && info.IsDir() {
return fmt.Errorf("%s is a directory", path)
}
f, err := os.CreateTemp(dir, ".t*")
if err != nil { return err }
f.Close(); os.Remove(f.Name())
return nil
} Try / catch
if err := utils.CreateFiles(files...); err != nil {
return fmt.Errorf("config file setup failed: %w", err)
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
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
- error writing file : %w
- failed to create directory %s: %w
- failed to initialize json file %s: %w
- failed to remove source after copy: %w
- failed to create destination directory: %w
AI-assisted analysis of yorukot/superfile@b72f550bc6 (2026-09-01).
Data as JSON: /api/errors/60fa93002203c607.
Report an issue: GitHub.