nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] A preferences file that fails to parse is wiped on the next setting change, including trustedDirectories

Open addyCooks opened this issue 2 weeks ago • 1 comments

Description

If nanocoder-preferences.json cannot be parsed (for example a hand edit left a trailing comma), loadPreferences() returns {} and the next save writes only the one changed setting. All other settings and the trusted directory list are lost.

Environment

  • OS: Windows 11 Home 10.0.26300
  • Node version: v22.22.3
  • Nanocoder version: 1.32.0 (built from main at f92cce9c)
  • Provider: Not provider-specific
  • Model: Not model-specific

Steps to Reproduce

  1. Write this to $NANOCODER_CONFIG_DIR/nanocoder-preferences.json (note the trailing comma):

     {"selectedTheme":"dracula","trustedDirectories":[{"path":"/x","fingerprint":"abc","fingerprintVersion":1}],"lastProvider":"ollama",}
    
  2. Run a script that calls loadPreferences() (it returns {}) and then updateSelectedTheme('nord').

  3. Read the file.

Expected Behavior

The unreadable file is not overwritten. For example it is copied to nanocoder-preferences.json.bak first, and the user sees a warning.

Actual Behavior

trustedDirectories and lastProvider are gone.

Logs/Screenshots

file after: {
  "selectedTheme": "nord"
}

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] I can reproduce this issue consistently
  • [x] This issue occurs with the latest version of nanocoder (main)

Suggested fix:

  • source/config/preferences.ts:188 (JSON.parse) and :214 (catch): remember that the parse failed.
  • source/config/preferences.ts:279 (savePreferences) and :326 (setPreference): when the existing file was unreadable, back it up (or refuse to write) before saving.
  • Add a test for the trailing comma case.

Related: #1931 / PR #1946 (a leading BOM triggers the same wipe; that PR only fixes the BOM case).

addyCooks avatar Nov 14 '26 06:11 addyCooks

Picking this up. Plan: back the unreadable preferences file up to <file>.bak once per corrupted version (keyed on mtime) before any save can overwrite it, log a warning with the backup path, and cover it with tests for both the single-backup and repaired-then-broken-again cases. PR shortly.

chekerh avatar Nov 14 '26 09:11 chekerh