Skip to content

Visual Studio Code, rebuilt in Rust

Rust
14.8 KB
Raw Blame

Settings, storage, profiles and keybinding tables

Settings are the config crate, storage and profiles are the storage crate, and the keybinding tables are app/src/keybindings.rs. This file covers how they are layered, where each lives on disk, how a change reaches the window, and how to add a setting or a stored key. It goes one level under ARCHITECTURE.md ("State and persistence"). Upstream is src/vs/platform/configuration, src/vs/workbench/services/configuration, src/vs/base/parts/storage, src/vs/platform/userDataProfile and src/vs/workbench/services/keybinding. Key dispatch is in workbench.md, the host's view of settings and storage in extension-host.md.

What is where

  • config/src/registry.rs::Registry: every registered setting with its schema, scope and default. Upstream's own settings are data: config/core-settings.json, dumped from the reference's registry by port/tools/dump-settings, with config/core-settings-stable.json for what a stable build registers differently. The port's own settings are config/port-settings.json. Two sets are registered in code: the port's update.releaseChannel (config/src/registry.rs::core_of_quality) and upstream's Windows-only UNC settings (config/src/registry.rs::windows_settings). Extension settings come from each contributes.configuration and contributes.configurationDefaults.
  • config/src/model.rs::ConfigurationModel: one layer, its parser with the scope filter (config/src/model.rs::ParseFilter), and config/src/model.rs::compare, which names the keys a reload touched.
  • config/src/configuration.rs::Configuration: the layers of one window and the merged view.
  • config/src/service.rs::Settings: loads the files into layers, reloads them, writes values as configurationEditing.ts does.
  • config/src/json.rs and config/src/json_edit.rs: JSON with comments, and the minimal text edits that set one value. A write of one value into a settings or keybindings file goes through config/src/json_edit.rs::set_property, so comments and layout survive. A write of the whole tasks, launch or mcp key into its own file replaces the file's text (config/src/service.rs::write).
  • config/src/policy.rs: the policy layer. app/src/policy.rs reads the values from the OS.
  • storage/src/lib.rs::Storage: one state.vscdb. storage/src/state.rs::FileStorage: storage.json. storage/src/profiles.rs::Profiles: the profiles and their workspace associations.
  • app/src/workbench/configuration.rs: the watchers and the workbench's change listeners. app/src/user_data_profile.rs and app/src/workbench/user_data_profile.rs: the profile of a window and the switch.
  • app/src/keybindings.rs: the parser, app/src/keybindings.rs::Resolver, and the default, extension and user rules. app/src/keybinding_editing.rs computes the edited text of keybindings.json, and app/src/workbench/keybindings_editor_view.rs::write_keybindings_file writes it.

Layers and precedence

config/src/configuration.rs::consolidated merges, later wins: defaults, application, user, workspace, the folder that holds the resource, then the [language] block of that merge, then policy over everything. The port has no remote and no in-memory layer; config/src/configuration.rs::to_data still sends the remote one empty (userRemote), in upstream's shape.

Layer File Scopes it accepts
Defaults the registry all
Application the default profile's settings.json, only in a profile with its own settings APPLICATION_SCOPES, plus the keys in workbench.settings.applyToAllProfiles
User the profile's settings.json, tasks.json, mcp.json all in the default profile, PROFILE_SCOPES in another
Workspace settings, launch, tasks of the .code-workspace file; in a folder window, the folder's layer WORKSPACE_SCOPES
Folder settings.json, tasks.json, launch.json, mcp.json of the folder's configuration folders FOLDER_SCOPES in a workspace, WORKSPACE_SCOPES in a folder window
Policy CFPreferences on macOS, a policy file on Linux settings that declare a policy

A key outside the accepted scopes is dropped at parse time, not at read time. An untrusted window also drops restricted and unregistered keys from the workspace and folder layers (config/src/service.rs::update_workspace_trust).

Port decision, not upstream: a folder's configuration is read from .vscode, .vsrustium and .vsrustium, the later winning per key (config/src/service.rs::FOLDER_CONFIG_DIRS), and written to the last one that exists (config/src/service.rs::folder_config_dir). Code that builds a .vscode/... path by hand misses the other two.

Invariant: the merged models are cached per folder and language, and the config.* context keys as one map (config/src/configuration.rs::context_keys, read on every key through app/src/workbench/keyboard.rs::key_context). Code that assigns a layer field of Configuration calls config/src/configuration.rs::layers_changed after it. Without the call, reads return the old value.

Reading, writing and change

A read is config/src/service.rs::get_value with config/src/configuration.rs::Overrides (a resource and a language). In the workbench use app/src/workbench/explorer_actions.rs::setting for a plain key and app/src/workbench/format.rs::language_setting for a value of the editor's language. A read takes the settings lock and reads the cached merge, so a render can ask for the value it needs each frame and needs no listener.

A write returns a config/src/model.rs::Change, the keys and language overrides that changed. Nothing is emitted: the caller carries the Change to whoever must hear it. There are four sources:

  1. A file edited on disk. app/src/workbench/configuration.rs::watch_settings_files watches the folders of config/src/service.rs::watched_files, and config/src/service.rs::reload_changed_files reloads the layers whose size or modification time moved.
  2. The workbench. config/src/service.rs::update_value_derived writes where the key is already set, else into user settings, where the default value removes the key. config/src/service.rs::update_value writes to a named target.
  3. The extension host. exthost/src/main_thread_configuration.rs::MainThreadConfiguration runs on the session thread on the same Arc<Mutex<Settings>> and sends $acceptConfigurationChanged before its reply, as upstream's event fires before the update resolves.
  4. A new registry (extensions added or removed), a policy change, a trust change, a profile switch, another workspace or other folders: set_registry, set_policy_values, update_workspace_trust, set_profile, set_workspace, write_workspace_folders of config/src/service.rs::Settings.

app/src/workbench/configuration.rs::configuration_changed is the one place that plays upstream's onDidChangeConfiguration listeners: it tells the host, then refreshes what the workbench derived from a setting and keeps (the theme, icon themes, model indentation, folding, the proxy), then notifies.

Trap: app/src/workbench/explorer_actions.rs::update_setting tells the host and stops there. It is right for a setting the views read live. A setting with a branch in configuration_changed must be written through a path that calls it, as app/src/workbench/settings_editor_view.rs::write_setting_value does.

Trap: a write from the host (source 3) refreshes the file stamps (config/src/service.rs::reload_after_write), so the reload the watcher starts (config/src/service.rs::reload_changed_files) finds no file changed and configuration_changed does not run for it. The request is served on the session thread and is not forwarded to the workbench. The views see the new value on their next frame; derived state does not follow.

Invariant: a settings.json or tasks.json under the user home is written through config/src/service.rs::write_user_data_file (a temporary file renamed over the target, as FileUserDataProvider; a symbolic link is written in place). mcp.json and the folder and workspace files are written in place (config/src/service.rs::write_settings_file). A policy value blocks the write with upstream's error (config/src/service.rs::validate).

Settings that upstream renamed are moved by app/src/workbench/configuration_migration.rs, a port of each registered migration of the kept features.

Storage

Storage is upstream's ItemTable behind an in-memory map. A read never touches SQLite. A write updates the map and runs one SQLite statement at once, where upstream batches for 100 ms. On drop a healthy file is copied to .backup; a file that failed at runtime is rebuilt from the map.

Scope Field of app/src/workbench.rs::Workbench File under the user data folder
Application application_storage User/globalStorage/state.vscdb
Profile profile_storage the same file in the default profile, else User/profiles/<id>/globalStorage/state.vscdb
Workspace workspace_storage User/workspaceStorage/<id>/state.vscdb, in memory for an empty window

The workspace id is upstream's (exthost/src/session.rs::workspace_id). The profile and workspace storages are shared with the session thread through exthost/src/main_thread_storage.rs::MainThreadStorage: an extension's globalState is one JSON string under its id in profile storage, its workspaceState the same in workspace storage. Extension test runs use storage/src/lib.rs::in_memory.

storage.json (User/globalStorage/storage.json) is for state needed before a window exists: the profiles, the windows of the last session (app/src/windows_state.rs), the telemetry ids. Each writer opens it again for its change (app/src/user_data_profile.rs::edit), so the keys another writer set are kept. Keep a FileStorage only for the length of one change.

Invariant: keys and value formats are upstream's, so a VS Code user data folder loads as it is and the settings import can copy rows (app/src/settings_import.rs). A key the port invents is a decision in port/notes.md.

Invariant: a storage lock is held for one get or set. In the default profile application_storage and profile_storage are the same mutex: locking both in one expression deadlocks (see the read of the bisect state in app/src/main.rs::open_window).

Trap: each window opens its own Storage over the file. A value another window writes is not in this window's map until storage/src/lib.rs::refresh rereads the file. Two reads call it: app/src/workbench/opener.rs::trusted_domains on application storage and app/src/recently_opened.rs::load on the shared_storage field (app/src/main.rs::open_shared_storage, one per process). Every other read returns the map as that Storage loaded it or last wrote it.

Profiles

Profiles keeps upstream's userDataProfiles and profileAssociations keys of storage.json. It emits nothing: the create, update, remove and association methods return a storage/src/profiles.rs::Change; clean_up and clean_up_transient_profiles return none. storage/src/profiles.rs::to_profile gives every resource path of a profile; a resource whose use_default_flags entry is true points at the default profile's file. The default profile is User, another is User/profiles/<id>. Take a path from the storage/src/profiles.rs::Profile field, never from a joined string.

app/src/user_data_profile.rs::UserDataProfilesService is one GPUI global for the process. app/src/user_data_profile.rs::resolve picks a window's profile: --profile, --profile-temp, the workspace's association, window.newWindowProfile, the default. app/src/user_data_profile.rs::user_files turns it into the config/src/service.rs::UserFiles the settings read.

A switch (app/src/workbench/user_data_profile.rs::change_current_profile) stops the extension host, moves the settings layers, keybindings.json, profile storage and the user extensions file to the new profile, runs configuration_changed, and starts the host again. State the workbench read from profile storage at startup is read again in app/src/workbench.rs::restore_profile_state; state that is loaded only once keeps the old profile's value.

Keybinding tables

The default rules are data, not code: app/src/keybindings/default-macos.json and app/src/keybindings/default-win.json are the reference's --export-default-keybindings output without the built-in extensions' rows, written by port/tools/default-keybindings. Linux reads the macOS table for now (app/src/keybindings.rs::default_table). A new command gets its default key from a new export, not from a hand-written row.

app/src/keybindings.rs::default_keybindings_in orders the rules as upstream's sorter: table rows without a weight first, then rows by weight, command and registration order, with each extension's contributes.keybindings at 300 (built-in) or 400 plus its index. app/src/keybindings.rs::user_keybindings reads the profile's keybindings.json, and the resolver puts those rules over both. app/src/workbench/keyboard.rs::load_keybindings builds the resolver; app/src/workbench/keyboard.rs::start_keybindings_watcher reloads the user file 50 ms after its last change.

Add a setting or a stored key

A setting of upstream is already registered: read it with setting or language_setting under upstream's key. If the workbench keeps state derived from it, add a branch to configuration_changed. A setting only the port has is an entry of config/port-settings.json in the shape of the dump (numeric scope as config/src/registry.rs::Scope, section for the Settings editor) and a decision in port/notes.md. Do not edit config/core-settings.json by hand: rerun port/tools/dump-settings.

A stored key takes upstream's key string and upstream's StorageScope, written as a string to the matching field of Workbench. If the value must follow a profile switch, load it from restore_profile_state.

Check a change

  • Layers, scopes, writes and their errors: port/tools/cargo-test -p config service, and port/tools/cargo-test -p config profile_tests for the application layer of a profile.
  • The registry and the models: port/tools/cargo-test -p config registry, port/tools/cargo-test -p config model.
  • JSON with comments and edits: port/tools/cargo-test -p config json.
  • The database, storage.json and profiles: port/tools/cargo-test -p storage.
  • Keybinding rules and the resolver: port/tools/cargo-test keybindings::tests. User rules over defaults in a window: port/tools/cargo-test user_keybindings_remove_defaults_and_bind_chords.
  • Against the reference app, after a release build: port/tools/studio 'port/tools/settings-check configuration state', and port/tools/studio 'port/tools/settings-check preferences' for the default settings and keybindings documents.
  • After a rename: bun port/tools/docs-check.ts.