Skip to content

Visual Studio Code, rebuilt in Rust

Rust
25.1 KB
Raw Blame

Architecture

This file is the map of VSRustium: what is where, why, and which rules hold everywhere. Read it before the first change. It does not explain how each module works: the module's own //! header does that, and docs/ has one file per large area.

The file names code as a path plus a symbol, never a line number: app/src/main.rs::open_window is the word open_window in native/crates/app/src/main.rs. A path starts at the repo root or at a crate of native/crates. If a cited path or symbol is gone, bun port/tools/docs-check.ts fails.

What the app is

VSRustium is VS Code 1.148.0 with the Electron, DOM and TypeScript workbench replaced by one native Rust process that draws with GPUI. Extensions do not change: they run in VS Code's own extension host, a JavaScript process on Bun, and talk to the Rust side over upstream's RPC protocol.

The port is 1:1. Each Rust module follows a named upstream file and ports its behavior and its tests. The module header names that file, so the upstream source is the specification. A deliberate difference is a decision in port/notes.md.

Upstream VS Code is a checkout of the 1.148.0 tag in upstream/. The build reads it and never edits it. The repository holds only the port: native/ (Rust), port/ (ledger, notes, patches, tools, fixtures) and the documents.

Process model

One app process owns every window. app/src/main.rs::main is the entry point of the binary vsrustium. The same binary also runs in three short-lived modes, chosen at the top of main:

  • The CLI. bin/<applicationName> starts the binary with ELECTRON_RUN_AS_NODE=1, and app/src/cli.rs::main handles the command line as upstream's cli.ts does. Extension commands (--install-extension) run in that process (app/src/cli_process.rs). Other command lines start the app.
  • The second instance. app/src/launch.rs::send_to_running_instance sends the arguments to the first instance over <user data>/<version>-main.sock and exits. The first instance listens with app/src/launch.rs::listen and opens the windows in app/src/launch.rs::handle_launches.
  • The update helper. app/src/update/install.rs::apply_update waits for the app to quit, replaces the bundle and starts the app again. app/src/update.rs finds and downloads the update.

Each window has its own child processes:

  • The extension host: one Bun process per window (Node with --node). A thread named exthost-session owns the connection (app/src/extension_host_start.rs::run_extension_host). See "Extension host bridge".
  • Terminals: one shell per terminal behind a pty (app/src/terminal/pty.rs::Pty, ConPTY in app/src/terminal/pty/conpty.rs). Two threads per terminal read the pty and wait for the exit.
  • Search: one ripgrep process per search (search/src/ripgrep.rs::run).
  • Webviews: a native web view inside the app window. The OS web engine runs its own content processes.

Extensions start more processes (git, language servers). Those are children of the extension host, not of the app.

Invariant: the GPUI main thread never waits for another process. Every wait for the extension host, a pty, ripgrep or the disk is on a different thread or in an async task.

Code map

Crates

native/ is one Cargo workspace (native/Cargo.toml) with the crates of native/crates. The dependencies point one way: app uses all, exthost uses config, files, search and storage, search uses files, files uses config.

Crate Owns Upstream
app The binary: the workbench, the editor, the terminal, the webviews, the platform glue. src/vs/workbench, src/vs/editor, src/vs/code, parts of src/vs/platform
exthost The parent side of the extension host: process start, framing, RPC, the session, the MainThread calls that need no window, extension scan, install and gallery, HTTP requests. src/vs/workbench/services/extensions, src/vs/workbench/api/browser, src/vs/platform/extensionManagement
config Settings: the registry, the layers and their merge, JSON with comments, JSON edits, variables, workspaces. src/vs/platform/configuration, src/vs/base/common/json.ts
files The file service over providers, the disk, text encodings, globs, the file watcher, file operations with undo. src/vs/platform/files, src/vs/base/common/glob.ts
search File and text search over ripgrep, and the query builder. src/vs/workbench/services/search
storage SQLite key-value storage, storage.json state, user data profiles. src/vs/base/parts/storage, src/vs/platform/state, src/vs/platform/userDataProfile
textmate TextMate grammars and tokenization on Oniguruma, token colors. the vscode-textmate package
diff The line and character diff. src/vs/editor/common/diff
ghostty_vt The terminal screen: libghostty-vt behind the types the terminal draws from. none, it takes the place of xterm.js
pixeldiff Screenshot comparison for the port's tools. none

Invariant: only app depends on GPUI. The other crates are plain data and I/O, so their tests need no window and run in milliseconds.

Invariant: config, diff, files, ghostty_vt, pixeldiff, search, storage and textmate contain no unsafe code. #![forbid(unsafe_code)] at each crate root enforces it. See docs/memory-safety.md.

GPUI comes from gpui-fast, a Zed GPUI fork with Retained Mode (a frame redraws only the views that called cx.notify()), pinned by commit in native/Cargo.toml. The GPUI crates the port changes are copies in native/vendor/gpui-fast (native/vendor/README.md). Retained Mode keeps a view's last frame until it notifies, so a change to what a view draws must notify it; a value measured in paint and read by the next render draws again through part_view::redraw_after_frame (port/notes.md, "GPUI on gpui-fast"). Do not edit native/vendor for a feature: a change there is a change to the framework, and git log -p native/vendor must show only such changes.

Layers inside app

app/src has two levels, and the split is the main rule of the crate.

  • Models: the files directly in app/src, and app/src/editor, app/src/terminal and the other folders beside them. A model is plain data with functions: app/src/explorer.rs::FileTree, app/src/editor.rs::TextModel, app/src/terminal.rs::TerminalInstance, app/src/scm.rs, app/src/keybindings.rs::Resolver. Most models do not import GPUI. Their tests are plain #[test] functions, usually upstream's cases.
  • The workbench: app/src/workbench.rs and app/src/workbench. app/src/workbench.rs::Workbench is one GPUI entity per window. It owns every model of the window as a field. The files in app/src/workbench are impl Workbench blocks: one file per part, view or contribution, with the drawing, the input handling and the commands of that feature.

Upstream's src/vs layout maps onto this as follows:

Upstream Here
src/vs/base No layer of its own. A ported helper sits beside its user (app/src/fuzzy_scorer.rs, files/src/glob.rs, config/src/json.rs).
src/vs/platform Services without UI: the config, files and storage crates, and models in app/src (app/src/commands.rs, app/src/contextkey.rs, app/src/keybindings.rs, app/src/menus.rs, app/src/theme.rs, app/src/update.rs).
src/vs/editor app/src/editor.rs and app/src/editor (model, cursors, edits, folding, word wrap), app/src/syntax.rs (tokens), app/src/brackets. The view is app/src/workbench/editor_view.rs.
src/vs/workbench/browser (layout and parts) app/src/workbench/layout.rs, app/src/workbench/part_grid.rs, app/src/workbench/part_view.rs, app/src/editor_grid.rs, app/src/workbench/editor_groups.rs.
src/vs/workbench/contrib A model in app/src plus a view in app/src/workbench: app/src/search_model.rs with app/src/workbench/search_view.rs, app/src/debug.rs with app/src/workbench/debug_view.rs.
src/vs/workbench/services Fields and methods of Workbench, or a model in app/src.
src/vs/workbench/api/browser (MainThread classes) exthost/src/main_thread_files.rs and its siblings for calls that need no window. app/src/main_thread.rs, app/src/main_thread_commands.rs and app/src/workbench/host_requests.rs for calls that need the workbench.
src/vs/code (Electron main) app/src/main.rs, app/src/launch.rs, app/src/windows_state.rs, app/src/cli.rs.
extensions Not ported. The built-in extensions run as they are.

To find the code of an upstream folder, search the Units table of the ledger: rg -n '^\| workbench/contrib/scm ' port/ledger/units. The last column names the files and the tests.

Invariant: Workbench is the only owner of window state. A part view, a task or a callback reaches state through the Workbench entity. There is no second copy to keep in sync.

Invariant: a model does not call the workbench. Data goes into a model function and a result comes out. The workbench applies the result, tells the extension host and calls notify.

Platform glue per OS

Code that calls an OS API sits in a file named after the OS, beside the portable module that uses it: app/src/power/macos.rs and app/src/power/windows.rs under app/src/power.rs, and the same for app/src/file_dialog, app/src/keyboard_layout, app/src/policy, app/src/system_wide_keybindings, app/src/webview and exthost/src/request. A module whose code for one OS is short keeps it inline: app/src/dialog.rs has files for Linux and Windows only, exthost/src/main_thread_secret_state.rs for Windows only. The pty is app/src/terminal/pty.rs on Unix and app/src/terminal/pty/conpty.rs on Windows. app/src/win32.rs and app/src/x11.rs hold shared helpers. docs/platform.md has the table of what lives where. The portable module selects the file with cfg(target_os) and gives every OS the same functions.

Invariant: unsafe code is allowed only in this glue: FFI to the OS, the pty and the web views. The full list is the inventory in docs/memory-safety.md. If a file outside the list contains unsafe, port/tools/docs-check.ts fails.

Startup

The sequence is in app/src/main.rs. app/src/perf.rs::mark records the same code/... marks as upstream. Developer: Startup Performance shows them.

  1. main finds the user data folder (~/.vsrustium) and the app root. The app root is Resources/app in a packaged app, else the upstream checkout. app/src/product.rs::load reads product.json with the overlay port/product.json.
  2. main handles the short-lived modes of "Process model". Then it resolves the shell environment (app/src/shell_env.rs), the display language (exthost/src/language_packs.rs::resolve_nls) and scans the built-in extensions (app/src/main.rs::scan_system_extensions).
  3. GPUI starts. The callback of run opens the windows of the last session (app/src/windows_state.rs) or the window of the command line, each with app/src/main.rs::open_window.
  4. open_window builds what one window owns: the profile, the user extensions, the storage (exthost/src/main_thread_storage.rs::MainThreadStorage), the settings (config/src/service.rs::Settings), the workspace trust and the theme. Then it opens the GPUI window and makes the Workbench.
  5. Inside the window callback the workbench loads keybindings, starts the watchers, restores its state from storage and opens the editors (app/src/workbench.rs::restore_profile_state, open_command_line_files). The first frame draws after this callback returns. This is "window" in the benchmark.
  6. The same callback starts the exthost-session thread. The window does not wait for it. The thread starts the host, completes the handshake and activates the eager extensions. The mark code/didLoadExtensions is the handshake. "Ready" in the benchmark is later: the first two seconds in which the process tree is idle.

Invariant: nothing between step 3 and the first frame waits for the extension host or the network. The workbench draws from files and storage. Extension contributions come from the manifests that step 2 and step 4 scan, not from the running host.

GPUI as the port uses it

GPUI gives entities (state the app context owns), a render function per view that returns an element tree, and notify to draw a view again. The port uses a small part of it, on purpose.

  • Entities. One Workbench per window holds the state. app/src/workbench/part_view.rs::PartView is a thin view per workbench part (title bar, side bar, editor, panel, status bar) that draws its part from the Workbench and lets GPUI cache it. Other entities are rare: app/src/workbench/auxiliary_windows.rs::AuxiliaryWindow, app/src/update.rs::UpdateService.
  • Render. Workbench::render in app/src/workbench.rs builds the grid of parts from app/src/workbench/layout.rs. Sizes are upstream's CSS pixels. Render functions read state and build elements. They must not do I/O.
  • Notify. cx.notify() on the Workbench draws every part again. A change inside one part notifies only its part view: app/src/workbench/part_view.rs::notify_editor_view.
  • Keys. The port does not use GPUI's action and keymap system for commands. app/src/workbench.rs intercepts every keystroke and app/src/workbench/keyboard.rs::dispatch_key resolves it as upstream does: app/src/keybindings.rs::Resolver over the default, extension and user keybindings, with the when clauses of app/src/contextkey.rs evaluated against app/src/workbench/keyboard.rs::key_context. A key that no keybinding takes goes to the focused element (app/src/workbench/text_input.rs).
  • Commands. Every action is a command with upstream's id in app/src/commands.rs::Commands. Keybindings, menus (app/src/menus.rs), the command palette and vscode.commands.executeCommand all call app/src/commands.rs::execute_command.
  • Focus. Each focusable part has a GPUI FocusHandle field on Workbench. Context keys such as editorTextFocus come from which handle has the focus.
  • Async. cx.spawn runs a future on the main thread, and it is the way back into the Workbench after an await. The background executor (cx.background_spawn) and plain threads do blocking work. A thread sends its result over a channel that a spawned task reads.
  • Tests. A #[gpui::test] gets a TestAppContext with a deterministic scheduler. A test builds a Workbench in a test window, sends keys and clicks, and asserts on state or on element bounds (debug_bounds). The skill in .agents/skills/gpui-test covers seeds and scheduler failures.

Invariant: user-visible behavior goes through a command. A click handler calls execute_command, so the keybinding, the menu item and the extension API reach the same code.

Extension host bridge

The host is upstream's extension host code, bundled by port/tools/build-exthost into out-exthost/ from the checkout with port/patches applied. The port does not edit upstream: a needed change is a patch file there.

  • Connection. exthost/src/lib.rs::ExtensionHost starts the process and listens on a Unix socket (a named pipe on Windows, exthost/src/pipe.rs). Frames are upstream's PersistentProtocol (exthost/src/frame.rs). Messages are upstream's rpcProtocol.ts (exthost/src/rpc.rs). Proxy ids come from exthost/src/proxy_ids.rs, which port/tools/gen-proxy-ids.ts generates from the upstream protocol file.
  • Session. exthost/src/session.rs::Session runs on the exthost-session thread. It answers the MainThread calls that need no window by itself: files, configuration, storage, secrets, workspace search.
  • Calls that need the workbench take one of two paths, and app/src/main_thread_commands.rs::route names the path and the handler of each. A forwarded call goes to the main thread as a exthost/src/session.rs::ForwardedRequest, and app/src/main_thread_commands.rs::handle_host_request answers it later. A call that must see current document state goes through app/src/main_thread.rs::WorkbenchMainThread while the session thread waits.
  • Workbench to host. app/src/host_sync.rs::HostSync queues what the workbench tells the host (documents, editors, tabs, events) and its requests (call). The session thread sends the queue in order before each reply, so the host sees edits before answers that depend on them.

Invariant: the order of messages to the host is the order in which the workbench made them. Do not send to the host from a second place.

Invariant: the port sends no telemetry, and the host gets telemetry level off (exthost/src/session.rs::start).

State and persistence

All user data is under ~/.vsrustium (--user-data-dir changes it), in upstream's layout, so a VS Code user data folder has the same shape.

  • Settings. config/src/registry.rs::Registry holds every registered setting. config/src/service.rs::Settings holds the layers of one window (defaults, policy, user, workspace, folder) and merges them as upstream does (config/src/configuration.rs::Configuration). One Arc<Mutex<Settings>> per window is shared with the session thread. The workbench watches the settings files and reloads (app/src/workbench/configuration.rs::watch_settings_files).
  • Storage. storage/src/lib.rs::Storage is upstream's SQLite state.vscdb with a cache in front. A window has three: application (one file for all windows, read by each window into its own cache), profile and workspace. UI state uses upstream's storage keys, so the keys in a state database match VS Code's.
  • State file. storage/src/state.rs::FileStorage is storage.json: the windows of the last session and other state the app needs before a window exists.
  • Profiles. storage/src/profiles.rs::Profiles and app/src/user_data_profile.rs.
  • Dirty files. app/src/backups.rs writes working copy backups (hot exit). app/src/workbench/backup_tracker.rs restores them.
  • Keybindings. The defaults are generated tables in app/src/keybindings (port/tools/default-keybindings), one for macOS and one for Windows; Linux reads the macOS table until it has its own (app/src/keybindings.rs::default_table). The user file is keybindings.json of the profile.
  • Secrets. exthost/src/main_thread_secret_state.rs stores them in the Keychain, the Secret Service or the Credential Manager.

Invariant: a lock on settings or storage is short and never held across an await or a call to the host. The session thread takes the same locks.

Files, search and watchers

  • files/src/lib.rs::FileSystem is the provider interface. files/src/disk.rs::Disk is the disk provider. Extension file systems answer through the host (exthost/src/main_thread_files.rs).
  • files/src/watcher.rs::Watcher watches folders with the notify crate and batches events as upstream's watcher does. The workbench starts it in app/src/workbench/text_files.rs::start_file_watcher.
  • files/src/operations.rs creates, moves and deletes files with undo, and tells the host's file operation participants.
  • files/src/encoding.rs decodes and encodes text. files/src/glob.rs is upstream's glob dialect.
  • search/src/file_search.rs::search and search/src/text_search.rs::search run ripgrep and parse its output. search/src/query.rs builds the queries from the settings.
  • The Explorer reads folders through app/src/explorer.rs::FileTree, which caches a folder until its modification time changes.

Terminal, webviews, updates

  • Terminal. app/src/terminal.rs::TerminalInstance is one terminal: a pty and a screen. The screen model is libghostty-vt in the role of xterm.js, behind ghostty_vt::Term. Shell integration, links and history are in app/src/terminal. The view is app/src/workbench/terminal_view.rs.
  • Webviews. app/src/webview.rs holds the host page protocol and the resource serving that all platforms share (app/src/webview.rs::Resources). The native view, a Native per OS, is app/src/webview/macos.rs (WKWebView), app/src/webview/linux.rs (WebKitGTK) or app/src/webview/windows.rs (WebView2). A webview is a native child view placed over the GPUI window. GPUI does not draw it.
  • Updates. app/src/update.rs follows upstream's update service against the port's own feed (port/notes.md, "Update feed"). port/tools/update-check tests the whole path.

The port/ machinery

port/ is how the work is tracked and checked. It is not part of the app.

  • port/ledger.md and port/ledger/: the one source of truth. The Work table has decisions and milestones, open in port/ledger.md, closed in port/ledger/done.md. The Units table, one file per area in port/ledger/units, has one row per upstream folder with its status, open gaps and the narrowest check that proves it. bun port/tools/ledger-check.ts checks that the tables agree. Search the files, do not read them whole.
  • port/notes.md: decisions, traps and measurements per milestone. A ledger row points to its section.
  • port/patches: the changes the extension host needs in upstream files, applied to a build copy.
  • port/product.json: the port's product overlay (name, data folder, URLs).
  • port/fixtures: workspaces, test extensions and step files for the tools.
  • port/results: the output of the checks that a ledger row cites, such as port/results/benchmark.md.
  • port/plans: plans for multi-session work.

The tools in port/tools each start with a header that states their use. The main groups:

  • Where things run. port/tools/studio mirrors the checkout to the build machine (the Studio) and runs a command there. port/tools/linux-build runs it in a Linux container, port/tools/windows-build with the Windows cross toolchain. port/tools/gui-lane gives a GUI tool its own desktop. Builds and tests do not run on a laptop.
  • Comparison with VS Code. port/tools/pixel-check compares a still window with the reference. port/tools/ui-pair drives both apps through the same steps and saves both screenshots. port/tools/ref-dom, port/tools/ref-hover and their siblings read sizes and styles from the reference.
  • Extension API. port/tools/api-tests runs upstream's vscode-api-tests against the port. port/tools/extension-tests runs the tests of a built-in extension.
  • Performance. port/tools/bench measures startup, memory, idle CPU and quit of whole apps from outside.
  • Generators. port/tools/gen-ledger.ts, port/tools/gen-proxy-ids.ts, port/tools/default-keybindings, port/tools/dump-settings.
  • Repository checks. port/tools/ledger-check.ts, port/tools/ledger-issues.ts (by hand, needs gh), port/tools/check-licenses, port/tools/docs-check.ts.

Build, packaging, release

  • Build inputs. The build needs the upstream checkout: native/vscode_src.rs resolves it for the build scripts. app/build.rs makes the NLS index from upstream's localize calls and renders the Dock icons. A few modules embed upstream files, and tests read upstream fixtures. A binary outside a package also reads the checkout at run time as its app root: product.json, icons and the built-in extensions.
  • Build order. Compile upstream once, run bun port/tools/build-exthost, then cargo gc build --release -p app. README.md has the commands. The debug profile builds dependencies with optimization and the port's crates without (native/Cargo.toml).
  • Packaging. port/tools/package-app makes the signed macOS app and its update files. port/tools/package-linux makes the Linux archive. port/tools/app-files.ts lists the files a package ships.
  • CI. .github/workflows/ci.yml runs the repository checks and every crate's tests on macOS and Linux for each push to main and each pull request (Windows, paused, by hand in windows.yml). By hand, .github/workflows/linux.yml builds a package, and .github/workflows/windows.yml builds the app and runs its tests without a package. A release is a manual step.

Cross-cutting rules

  • Tests. A fix comes with a test that fails without it. A ported module ports upstream's test cases under their names. The tests of a module are in a tests.rs file in the folder named after the module, or in a tests module at the end of its file.
  • Memory safety. docs/memory-safety.md has the rules, the inventory of unsafe code and the checks.
  • Performance. port/results/benchmark.md has the startup and memory numbers against VS Code, and port/notes.md ("Benchmarks", "Scheduling differences") the frame time gates.
  • Errors. A function that can fail returns anyhow::Result or the crate's error type (files/src/lib.rs::FileError). A failure the user must see becomes a notification, as upstream shows it. A value that cannot be missing is used directly: a wrong assumption must panic in a test, not hide behind a default.
  • Text positions. The editor counts chars where upstream counts UTF-16 units. Each module header states its unit. Conversion happens at the extension host boundary.
  • Strings. UI text comes from upstream's NLS keys through app/src/nls.rs, so language packs work. Labels use title-style capitalization.
  • No network without the user: no telemetry, no experiments. port/tools/network-capture and port/tools/telemetry-source-check check it.

In flight

Work on other branches that this file does not describe yet: Views get drag and drop between containers. Other sessions fix the Windows (M24) and Linux (M23) test suites.