yorukot/superfile · error

error writing pinned directories file: %w

Error message

error writing pinned directories file: %w

What it means

Save marshals the pinned-directory list to JSON and writes it to mgr.filePath (the pinned.json config file). This error wraps a failure from os.WriteFile, meaning the marshaled data could not be persisted to disk. It is returned so callers like Toggle and Clean can surface a filesystem-level failure to the UI layer.

Source

Thrown at src/internal/ui/sidebar/pinned.go:57

		slog.Error("Error parsing pinned directories data", "error", err)
		return directories
	}

	// Clean non-existing directories
	cleanedDirs := mgr.Clean(directories)

	return cleanedDirs
}

// Save marshals and writes the pinned directories to file.
func (mgr *PinnedManager) Save(dirs []directory) error {
	data, err := json.Marshal(dirs)
	if err != nil {
		return fmt.Errorf("error marshaling pinned directories: %w", err)
	}

	if err := os.WriteFile(mgr.filePath, data, utils.ConfigFilePerm); err != nil {
		return fmt.Errorf("error writing pinned directories file: %w", err)
	}

	return nil
}

// Toggle adds or removes a directory from the pinned directories list
func (mgr *PinnedManager) Toggle(dir string) error {
	dirs := mgr.Load()
	unPinned := false

	for i, other := range dirs {
		if other.Location == dir {
			dirs = append(dirs[:i], dirs[i+1:]...)
			unPinned = true
			break
		}
	}

View on GitHub (pinned to b72f550bc6)

Solutions

  1. Create the parent directory before saving (os.MkdirAll(filepath.Dir(mgr.filePath), 0o755))
  2. Check permissions on the config file/directory and fix with chmod/chown
  3. Verify free disk space and that the path is not a read-only mount
  4. Log the wrapped inner error (%w) to see the exact OS-level cause

Example fix

// before
if err := os.WriteFile(mgr.filePath, data, utils.ConfigFilePerm); err != nil {
	return fmt.Errorf("error writing pinned directories file: %w", err)
}
// after
if err := os.MkdirAll(filepath.Dir(mgr.filePath), 0o755); err != nil {
	return fmt.Errorf("error creating config directory: %w", err)
}
if err := os.WriteFile(mgr.filePath, data, utils.ConfigFilePerm); err != nil {
	return fmt.Errorf("error writing pinned directories file: %w", err)
}
Defensive patterns

Strategy: validation

Validate before calling

if err := os.MkdirAll(filepath.Dir(mgr.filePath), 0o755); err != nil { return err }
if info, err := os.Stat(mgr.filePath); err == nil && info.Mode().Perm()&0o200 == 0 { return fmt.Errorf("pinned file not writable: %s", mgr.filePath) }

Try / catch

if err := mgr.Save(dirs); err != nil {
	var pe *fs.PathError
	if errors.As(err, &pe) { log.Printf("write failed at %s: %v", pe.Path, pe.Err) }
	return err
}

Prevention

When it happens

Trigger: os.WriteFile fails when writing mgr.filePath: the directory containing the file does not exist, the process lacks write permission, the disk is full, or the path is a directory/readonly mount.

Common situations: First run before the config directory (~/.config/...) has been created; read-only HOME or config dir; disk quota exceeded; running under a sandboxed environment without access to the config path.

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


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