plandex-ai/plandex · error

error writing JSON file: %v

Error message

error writing JSON file: %v

What it means

After serializing, WriteModelSettingsFile writes the JSON bytes to the settings path with os.WriteFile (mode 0644). Failures are wrapped as 'error writing JSON file: %v'. The root cause is the wrapped OS error, not the JSON content.

Source

Thrown at app/cli/lib/model_settings.go:90

func WriteModelSettingsFile(path string, originalSettings *shared.PlanSettings) error {
	err := os.MkdirAll(filepath.Dir(path), 0755)
	if err != nil {
		return fmt.Errorf("error creating directory: %v", err)
	}

	modelPackSchemaRoles := originalSettings.GetModelPack().ToModelPackSchema().ModelPackSchemaRoles

	clientModelPackRoles := modelPackSchemaRoles.ToClientModelPackSchemaRoles()

	bytes, err := json.MarshalIndent(clientModelPackRoles, "", "  ")
	if err != nil {
		return fmt.Errorf("error marshalling model pack: %v", err)
	}

	err = os.WriteFile(path, bytes, 0644)
	if err != nil {
		return fmt.Errorf("error writing JSON file: %v", err)
	}

	err = SaveModelPackRolesHash(path, &modelPackSchemaRoles)
	if err != nil {
		return fmt.Errorf("error saving model pack roles hash: %v", err)
	}

	return nil
}

func SaveModelPackRolesHash(basePath string, serverModelPack *shared.ModelPackSchemaRoles) error {
	hashPath := basePath + ".hash"

	hash, err := serverModelPack.Hash()
	if err != nil {
		return fmt.Errorf("error hashing model pack: %v", err)
	}

View on GitHub (pinned to e2d772072e)

Solutions

  1. Fix file/directory permissions: chmod u+w on model-settings.json and its directory
  2. Check the wrapped OS error for ENOSPC/EDQUOT and free space or quota
  3. Ensure no other process deleted the ~/.plandex/<planId> directory mid-operation
  4. If a directory exists at the file path, remove it and let the CLI recreate the file

Example fix

// before
open /home/user/.plandex/xyz/model-settings.json: permission denied
// after
sudo chown -R "$USER" ~/.plandex && chmod -R u+rw ~/.plandex
Defensive patterns

Strategy: validation

Validate before calling

if info, err := os.Stat(path); err == nil && info.IsDir() {
    return fmt.Errorf("%s is a directory; cannot write settings file", path)
}
if info, err := os.Stat(path); err == nil && info.Mode().Perm()&0o200 == 0 {
    os.Chmod(path, 0o644) // make writable before WriteModelSettingsFile
}

Try / catch

if err := lib.WriteModelSettingsFile(path, settings); err != nil {
    if strings.Contains(err.Error(), "error writing JSON file") {
        // check disk space/permissions on path, fix, retry
    }
    return err
}

Prevention

When it happens

Trigger: os.WriteFile(path, bytes, 0644) fails: the settings file is read-only, owned by another user, the directory was removed between MkdirAll and the write, the disk is full, or path points to a directory.

Common situations: model-settings.json opened in an editor with restrictive permissions; two CLI processes racing so the plan directory is deleted mid-write; quota-exceeded home directories on shared servers.

Understand the failure class

Background: "Permission denied" / "Failed to write" file errors: why a library can't write its files to disk (EACCES, EPERM, ENOSPC) and how to fix them — this error's family across 43 libraries.

Related errors


AI-assisted analysis of plandex-ai/plandex@e2d772072e (2026-09-05). Data as JSON: /api/errors/35459915f0accd28. Report an issue: GitHub.