bcicen/ctop · error
failed to open config for writing: %s
Error message
failed to open config for writing: %s
What it means
config.Write opens the config file with os.OpenFile(O_RDWR|O_CREATE, 0644) before encoding TOML. If the file cannot be opened, the error is wrapped with the path context as 'failed to open config for writing'.
Source
Thrown at config/file.go:103
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 {
return path, fmt.Errorf("failed to write config: %s", err)
}
return path, nil
}
// determine config path from environment
func getConfigPath() (path string, err error) {
homeDir, ok := os.LookupEnv("HOME")
if !ok {
return path, fmt.Errorf("$HOME not set")
}
View on GitHub (pinned to 59f00dd6aa)
Solutions
- Fix file/directory permissions on the config path (chmod 0644 file, writable dir)
- Remove or rename an invalid file sitting at the config path
- Run without sudo or chown the config back to the current user
Example fix
// before -rw------- root root config.toml # app runs as non-root // after chown $USER config.toml && chmod 0644 config.toml
Defensive patterns
Strategy: try-catch
Validate before calling
if fi, err := os.Stat(path); err == nil && fi.IsDir() { /* path invalid: it's a directory */ }
if fi, err := os.Stat(path); err == nil { f, e := os.OpenFile(path, os.O_WRONLY, 0644); if e != nil { /* fix perms */ } else { f.Close() } } Try / catch
_, err := config.Write()
if err != nil && strings.HasPrefix(err.Error(), "failed to open config for writing") {
// correct file ownership/permissions and retry
} Prevention
- Keep config file perms 0644 and owned by the running user
- Never point the config path at a directory
- Avoid running the app under sudo (creates root-owned files)
When it happens
Trigger: os.OpenFile fails: directory doesn't exist or isn't writable, path is a directory, or permission denied on an existing config file.
Common situations: Config file owned by another user (created under sudo); config path is a directory; stale permissions after switching users or running in a container as a different UID.
Related errors
AI-assisted analysis of bcicen/ctop@59f00dd6aa (2026-09-02).
Data as JSON: /api/errors/e9b3e355a3144d00.
Report an issue: GitHub.