bcicen/ctop · error

failed to create config dir [%s]: %s

Error message

failed to create config dir [%s]: %s

What it means

During config.Write, the target directory is created with os.MkdirAll if it does not exist. If that mkdir fails (permissions, read-only filesystem, path is a file), Write wraps the error with the directory path and underlying cause.

Source

Thrown at config/file.go:90

		}
		SetColumns(colNames)
	}

	return nil
}

func Write() (path string, err error) {
	path, err = getConfigPath()
	if err != nil {
		return path, err
	}

	cfgdir := filepath.Dir(path)
	// create config dir if not exist
	if _, err := os.Stat(cfgdir); err != nil {
		err = os.MkdirAll(cfgdir, 0755)
		if err != nil {
			return path, fmt.Errorf("failed to create config dir [%s]: %s", cfgdir, err)
		}
	}

	// remove prior to writing new file
	if err := os.Remove(path); err != nil {
		if !os.IsNotExist(err) {
			return path, err
		}
	}

	file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644)
	if err != nil {
		return path, fmt.Errorf("failed to open config for writing: %s", err)
	}

	writer := toml.NewEncoder(file)
	err = writer.Encode(exportConfig())
	if err != nil {

View on GitHub (pinned to 59f00dd6aa)

Solutions

  1. Check/fix permissions on the config directory's parent (chmod/u chown)
  2. Verify $HOME and XDG_CONFIG_HOME point to writable directories
  3. Ensure no regular file occupies the config directory path
  4. Check disk space and mount flags (read-only fs)

Example fix

// before
HOME=/ro-mount  # read-only
// after
export XDG_CONFIG_HOME=/tmp/app-config  # writable location
Defensive patterns

Strategy: try-catch

Validate before calling

cfgdir := filepath.Dir(path)
if st, err := os.Stat(cfgdir); err != nil || !st.IsDir() {
    if fi, err2 := os.Stat(filepath.Dir(cfgdir)); err2 != nil || !fi.IsDir() || !writable(fi) { /* fix env */ }
}

Try / catch

_, err := config.Write()
if err != nil && strings.HasPrefix(err.Error(), "failed to create config dir") {
    // fall back to a writable temp config dir
}

Prevention

When it happens

Trigger: os.MkdirAll(cfgdir, 0755) returns an error — e.g. parent directory not writable, disk full, or a file exists at the config directory path.

Common situations: $HOME or XDG_CONFIG_HOME pointing to a non-writable or nonexistent location; running in a sandboxed/containerized environment with read-only home; permission changes after setup.

Understand the failure class

Background: mkdir permission denied (EACCES): failed to create directory errors explained — this error's family across 32 libraries.

Related errors


AI-assisted analysis of bcicen/ctop@59f00dd6aa (2026-09-02). Data as JSON: /api/errors/6c2ee0f9788373b9. Report an issue: GitHub.