yorukot/superfile · error

failed to create directory %s: %w

Error message

failed to create directory %s: %w

What it means

CreateDirectories runs os.MkdirAll with ConfigDirPerm for each provided path; this error wraps a failure to create one of them. MkdirAll creates the full hierarchy but still fails on permission, path, or filesystem problems. The function stops at the first directory that fails.

Source

Thrown at src/pkg/utils/file_utils.go:248

			}
		}
		return err
	})
	if walkErr != nil {
		slog.Error("errors during WalkDir", "error", walkErr)
	}
	return size
}

// Helper functions
// 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) {

View on GitHub (pinned to b72f550bc6)

Solutions

  1. Read the wrapped error to see which directory and why (EACCES vs EEXIST vs ENOTDIR).
  2. Verify no regular file exists at the directory path (`ls -la <dir>`); remove/rename it if so.
  3. Check parent permissions and ownership; mkdir -p manually as the runtime user to confirm.
  4. Fix the environment (HOME/XDG_* vars) or pass explicit writable paths to CreateDirectories.
  5. Prefer calling CreateDirectories before any file creation (InitConfigFile already does; keep that ordering in custom setup code).

Example fix

// before
err := utils.CreateDirectories(cfgDir) // cfgDir happens to be a file
// after
if info, err := os.Stat(cfgDir); err == nil && !info.IsDir() {
    return fmt.Errorf("%s exists and is not a directory", cfgDir)
}
if err := utils.CreateDirectories(cfgDir); err != nil { return err }
Defensive patterns

Strategy: validation

Validate before calling

func ensureCreatableDir(dir string) error {
    if dir == "" { return nil }
    if info, err := os.Stat(dir); err == nil {
        if !info.IsDir() { return fmt.Errorf("%s exists as a file", dir) }
        return nil
    }
    parent := filepath.Dir(dir)
    if info, err := os.Stat(parent); err == nil && info.IsDir() {
        f, e := os.CreateTemp(parent, ".t*"); if e != nil { return e }; f.Close(); os.Remove(f.Name())
    }
    return nil
}

Try / catch

if err := utils.CreateDirectories(dirs...); err != nil {
    return fmt.Errorf("config dir setup failed: %w", err)
}

Prevention

When it happens

Trigger: InitConfigFile calling CreateDirectories with config/cache/data paths that cannot be created — parent dir not writable, a path component exists as a regular file, empty-ish path handled too late, or the filesystem is read-only.

Common situations: $HOME unset or pointing somewhere unwritable; XDG_CONFIG_HOME set to a read-only path; a file named like the config directory left behind by a bad install; sandboxed/container environments with read-only roots.

Related errors


AI-assisted analysis of yorukot/superfile@b72f550bc6 (2026-09-01). Data as JSON: /api/errors/e1325f2462a3aaf1. Report an issue: GitHub.