Skip to content

A dictionary key starting with $ (or a hand-added "$schema") saves fine, but the next load archives the file as .corrupt and resets every setting to defaults #376

Closed

Description

@matt-edmondson

What's wrong

AppData.JsonSerializerOptions sets ReferenceHandler = ReferenceHandler.Preserve (AppDataStorage/AppData.cs:45). Under that option, System.Text.Json happily writes a dictionary key such as "$HOME". When it reads the file back, it throws:

JsonException: Properties that start with '$' are not allowed in types that support metadata

A top-level "$schema" property that a user adds by hand for editor validation fails the same way, though other unknown properties are ignored as usual.

LoadOrCreate (AppData.cs:785-794) catches the JsonException, archives the file as settings.json.corrupt.<timestamp> and returns defaults. So one key the library itself wrote silently wipes every unrelated setting.

Repro (net10.0, at 9a1b47b)

public class Settings : AppData<Settings>
{
    public string Theme { get; set; } = "Light";
    public Dictionary<string, string> Variables { get; set; } = [];
}

var s = Settings.LoadOrCreate();
s.Theme = "Dark";
s.Variables["$HOME"] = "/home/me";
s.Save();
var r = Settings.LoadOrCreate();
Console.WriteLine($"Theme={r.Theme} Variables.Count={r.Variables.Count}");

The saved file contains "Variables": { "$id": "2", "$HOME": "/home/me" }. Then:

Theme=Light Variables.Count=0
  settings.json
  settings.json.corrupt.20261111_141154

Expected: Theme=Dark Variables.Count=1, and no .corrupt file.

The same failure without a dictionary: deserializing {"$schema":"./s.json","Theme":"Dark"} with these options throws the same JsonException (path $.$schema). By contrast, {"Theme":"Dark","Unknown":1} loads fine.

Why it matters

Dictionaries keyed by environment variables, template placeholders or shell variables are normal things to keep in settings, and $ keys are common in them. The save succeeds, so the data loss only shows on the next launch. At that point every setting resets, not just the dictionary.

Suggested fix

  • Preferred: stop using ReferenceHandler.Preserve, for example by switching to IgnoreCycles. Existing files contain $id/$values, so keep a fallback for them: retry with Preserve when a plain read meets $id/$values, or migrate on first load.
  • At minimum: have Save() refuse (throw) before writing a document it can't read back, for example by round-tripping it or rejecting $-prefixed keys. That turns silent data loss into an error at save time.

Acceptance:

  • A test shows a $-prefixed dictionary key round-trips, or the save is refused.
  • A test shows a hand-added top-level $schema loads without creating a .corrupt file.

Related but different: #353, also caused by Preserve, is about a mismatched-type $ref throwing InvalidOperationException.

Activity

  1. added
    bug
    on Nov 11, 2026
  2. matt-edmondson commented on Nov 11, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    • Category: Bug
    • Priority: High. A key the library itself saved makes the next load discard the whole file. Every unrelated setting resets to defaults, and nothing is reported until the next launch.
    • Area / suggested assignment: Serialization options and load path (AppData.JsonSerializerOptions, LoadOrCreate). It belongs with whoever owns the ReferenceHandler.Preserve decision.
    • Duplicates: None. Same root cause as A settings file that loaded before an app update makes every Get() throw InvalidOperationException instead of being archived as .corrupt #353 (Preserve plus a mismatched $ref) but a different trigger. One redesign away from Preserve could close both, so consider fixing them together.
    • In progress: No open PR covers this. Bump System.Text.Json from 10.0.14 to 11.0.0 #372 (System.Text.Json 11.0.0 bump) does not change ReferenceHandler behaviour. Re-run the repro after it merges anyway.
    • Next step: Ship the "at minimum" guard first: refuse a save that can't be read back, which turns silent data loss into an error. Then migrate off Preserve, keeping a fallback reader for existing $id/$values files.

    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions