Skip to content

settings: Use fallible parsing in edits_for_update to preserve settings#52081

Closed
OmChillure wants to merge 4 commits intozed-industries:mainfrom
OmChillure:fix-preserve-settings-during-migrations
Closed

settings: Use fallible parsing in edits_for_update to preserve settings#52081
OmChillure wants to merge 4 commits intozed-industries:mainfrom
OmChillure:fix-preserve-settings-during-migrations

Conversation

@OmChillure
Copy link
Copy Markdown
Contributor

@OmChillure OmChillure commented Mar 21, 2026

Context

update_settings_file deletes unrelated settings when the settings file contains deprecated keys that need migration. For example, changing the model from the Agent Panel overwrites the entire agent block instead of just updating default_model.

The root cause is that edits_for_update used parse_json_with_comments (strict parser), which returns Err on deprecated/unknown fields. The error is swallowed by log_err(), falling back to Default::default() (empty settings). The diff then sees everything as new and replaces the entire block.

The fix switches to parse_json (the fallible/lenient parser), which returns Some(parsed_value) even when deprecated fields are present - the same pattern already used by parse_and_migrate_zed_settings.

Fixes #41344

How to Review

Single-file change in settings_store.rs, focus on edits_for_update . Compare with parse_and_migrate_zed_settings (line 702) which already uses the same parse_json approach.

Self-Review Checklist

  • I've reviewed my own diff for quality, security, and reliability
  • Unsafe blocks (if any) have justifying comments
  • The content is consistent with the UI/UX checklist
  • Tests cover the new/changed behavior
  • Performance impact has been considered and is acceptable

Video

Screencast.from.2026-03-21.00-36-12.webm

Release Notes:

  • Fixed settings being overwritten when updating a single setting via UI while the settings file contains deprecated keys.

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Mar 21, 2026
@zed-codeowner-coordinator zed-codeowner-coordinator bot requested review from a team, SomeoneToIgnore and maxbrunsfeld and removed request for a team March 21, 2026 10:18
@zed-community-bot zed-community-bot bot added the guild Pull requests by someone in Zed Guild. NOTE: the label application is automated via github actions label Mar 21, 2026
Copy link
Copy Markdown
Member

@MrSubidubi MrSubidubi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing by and am fairly sure that we want a test case for this so that we both know the fix is good and that it will not regress in the future.

Similarly, I think we should probably at least discuss what to do when the parsing failed as oppposed to just falling back to defaults here and then messing around with the user settings anyway despite them having obvious errors.

@OmChillure
Copy link
Copy Markdown
Contributor Author

OmChillure commented Mar 23, 2026

Passing by and am fairly sure that we want a test case for this so that we both know the fix is good and that it will not regress in the future.

Similarly, I think we should probably at least discuss what to do when the parsing failed as oppposed to just falling back to defaults here and then messing around with the user settings anyway despite them having obvious errors.

Sorry, will write the tests too !

For the parsing failure case :
I think it's better to do nothing and return empty edits rather than falling back to defaults and risking overwriting the user's settings.

and can add a toast if necessary ?

What are your suggestions?

@MrSubidubi
Copy link
Copy Markdown
Member

I think it's better to do nothing and return empty edits rather than falling back to defaults and risking overwriting the user's settings.

That makes sense, although we probably need a result for that.

and can add a toast if necessary ?

Playing around with this might similarly make sense, think that sounds like a good path forward.

@MrSubidubi MrSubidubi changed the title settings: Use fallible parsing in edits_for_update to preserve settin… settings: Use fallible parsing in edits_for_update to preserve settings Mar 23, 2026
@OmChillure
Copy link
Copy Markdown
Contributor Author

OmChillure commented Mar 23, 2026

Hello @MrSubidubi

I have Updated edits_for_update to return Result - when the settings file can't be parsed at all (syntax errors etc.), it now bails out early with an error instead of falling back to Default::default(), which was silently overwriting the user's entire settings.

Added two tests:

  • test_edits_for_update_preserves_unknown_keys -> verifies unknown/deprecated keys survive when updating a known setting
  • test_edits_for_update_returns_error_on_invalid_json -> verifies a completely broken settings file returns an error instead of corrupting the file

Need some help here :

For surfacing the parse failure to the user - should we use a StatusToast, workspace.show_error() (red error box), or something else?

@OmChillure
Copy link
Copy Markdown
Contributor Author

OmChillure commented Mar 23, 2026

The Result signature change on edits_for_update / new_text_for_update broke 3 downstream callers - my mistake should have checked it before pushing. This commit fixes them using .log_err() with early return, which is the idiomatic error handling pattern in those crates.

Also fixed an edge case where empty settings input (e.g. a new settings.json with no content) was incorrectly treated as a parse error. Empty input now falls back to defaults, matching the original behavior.

On parse failure, callers now skip editing instead of proceeding with bad data. This is consistent with the PR's goal of not silently corrupting settings.

An alternative would be to revert the signature change and handle the error internally (return empty Vec + log), keeping the PR as a single-file change. But that would mean callers have no way to know parsing failed, and silently returning empty edits felt too close to the original bug. Happy to go either way !!

Thank you

@github-actions github-actions bot added Size M and removed Size S labels Mar 24, 2026
@OmChillure OmChillure closed this by deleting the head repository Mar 29, 2026
probably-neb pushed a commit that referenced this pull request Mar 30, 2026
## Context

`update_settings_file` deletes unrelated settings when the settings file
contains deprecated keys that need migration. For example, changing the
model from the Agent Panel overwrites the entire `agent` block instead
of just updating `default_model`.

The root cause is that `edits_for_update` used
`parse_json_with_comments` (strict parser), which returns `Err` on
deprecated/unknown fields. The error is swallowed by `log_err()`,
falling back to `Default::default()` (empty settings). The diff then
sees everything as new and replaces the entire block.

The fix switches to `parse_json` (the fallible/lenient parser), which
returns `Some(parsed_value)` even when deprecated fields are present -
the same pattern already used by `parse_and_migrate_zed_settings`.

## Fixes #41344

## How to Review

Single-file change in `settings_store.rs`, focus on `edits_for_update` .
Compare with `parse_and_migrate_zed_settings` (line 702) which already
uses the same `parse_json` approach.

## Self-Review Checklist

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

## Video
[Screencast from 2026-03-21
00-36-12.webm](https://github.com/user-attachments/assets/31bd584a-2674-4c91-bdb2-69ed8fa35e88)


### Note : Reopens previous work from closed PR #52081 (fork was
deleted)

Release Notes:

- Fixed settings being overwritten when updating a single setting via UI
while the settings file contains deprecated keys.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement guild Pull requests by someone in Zed Guild. NOTE: the label application is automated via github actions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

update_settings_file deletes settings if settings file needs to be migrated

4 participants