Visual Studio Code, rebuilt in Rust
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 withELECTRON_RUN_AS_NODE=1, andapp/src/cli.rs::mainhandles the command line as upstream'scli.tsdoes. 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_instancesends the arguments to the first instance over<user data>/<version>-main.sockand exits. The first instance listens withapp/src/launch.rs::listenand opens the windows inapp/src/launch.rs::handle_launches. - The update helper.
app/src/update/install.rs::apply_updatewaits for the app to quit, replaces the bundle and starts the app again.app/src/update.rsfinds and downloads the update.
Each window has its own child processes:
- The extension host: one Bun process per window (Node with
--node). A thread namedexthost-sessionowns 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 inapp/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, andapp/src/editor,app/src/terminaland 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.rsandapp/src/workbench.app/src/workbench.rs::Workbenchis one GPUI entity per window. It owns every model of the window as a field. The files inapp/src/workbenchareimpl Workbenchblocks: 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.
mainfinds the user data folder (~/.vsrustium) and the app root. The app root isResources/appin a packaged app, else the upstream checkout.app/src/product.rs::loadreadsproduct.jsonwith the overlayport/product.json.mainhandles 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).- GPUI starts. The callback of
runopens the windows of the last session (app/src/windows_state.rs) or the window of the command line, each withapp/src/main.rs::open_window. open_windowbuilds 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 theWorkbench.- 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. - The same callback starts the
exthost-sessionthread. The window does not wait for it. The thread starts the host, completes the handshake and activates the eager extensions. The markcode/didLoadExtensionsis 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
Workbenchper window holds the state.app/src/workbench/part_view.rs::PartViewis a thin view per workbench part (title bar, side bar, editor, panel, status bar) that draws its part from theWorkbenchand lets GPUI cache it. Other entities are rare:app/src/workbench/auxiliary_windows.rs::AuxiliaryWindow,app/src/update.rs::UpdateService. - Render.
Workbench::renderinapp/src/workbench.rsbuilds the grid of parts fromapp/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 theWorkbenchdraws 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.rsintercepts every keystroke andapp/src/workbench/keyboard.rs::dispatch_keyresolves it as upstream does:app/src/keybindings.rs::Resolverover the default, extension and user keybindings, with thewhenclauses ofapp/src/contextkey.rsevaluated againstapp/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 andvscode.commands.executeCommandall callapp/src/commands.rs::execute_command. - Focus. Each focusable part has a GPUI
FocusHandlefield onWorkbench. Context keys such aseditorTextFocuscome from which handle has the focus. - Async.
cx.spawnruns a future on the main thread, and it is the way back into theWorkbenchafter 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 aTestAppContextwith a deterministic scheduler. A test builds aWorkbenchin a test window, sends keys and clicks, and asserts on state or on element bounds (debug_bounds). The skill in.agents/skills/gpui-testcovers 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::ExtensionHoststarts the process and listens on a Unix socket (a named pipe on Windows,exthost/src/pipe.rs). Frames are upstream'sPersistentProtocol(exthost/src/frame.rs). Messages are upstream'srpcProtocol.ts(exthost/src/rpc.rs). Proxy ids come fromexthost/src/proxy_ids.rs, whichport/tools/gen-proxy-ids.tsgenerates from the upstream protocol file. - Session.
exthost/src/session.rs::Sessionruns on theexthost-sessionthread. 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::routenames the path and the handler of each. A forwarded call goes to the main thread as aexthost/src/session.rs::ForwardedRequest, andapp/src/main_thread_commands.rs::handle_host_requestanswers it later. A call that must see current document state goes throughapp/src/main_thread.rs::WorkbenchMainThreadwhile the session thread waits. - Workbench to host.
app/src/host_sync.rs::HostSyncqueues 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::Registryholds every registered setting.config/src/service.rs::Settingsholds the layers of one window (defaults, policy, user, workspace, folder) and merges them as upstream does (config/src/configuration.rs::Configuration). OneArc<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::Storageis upstream's SQLitestate.vscdbwith 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::FileStorageisstorage.json: the windows of the last session and other state the app needs before a window exists. - Profiles.
storage/src/profiles.rs::Profilesandapp/src/user_data_profile.rs. - Dirty files.
app/src/backups.rswrites working copy backups (hot exit).app/src/workbench/backup_tracker.rsrestores 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 iskeybindings.jsonof the profile. - Secrets.
exthost/src/main_thread_secret_state.rsstores 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::FileSystemis the provider interface.files/src/disk.rs::Diskis the disk provider. Extension file systems answer through the host (exthost/src/main_thread_files.rs).files/src/watcher.rs::Watcherwatches folders with thenotifycrate and batches events as upstream's watcher does. The workbench starts it inapp/src/workbench/text_files.rs::start_file_watcher.files/src/operations.rscreates, moves and deletes files with undo, and tells the host's file operation participants.files/src/encoding.rsdecodes and encodes text.files/src/glob.rsis upstream's glob dialect.search/src/file_search.rs::searchandsearch/src/text_search.rs::searchrun ripgrep and parse its output.search/src/query.rsbuilds 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::TerminalInstanceis one terminal: a pty and a screen. The screen model is libghostty-vt in the role of xterm.js, behindghostty_vt::Term. Shell integration, links and history are inapp/src/terminal. The view isapp/src/workbench/terminal_view.rs. - Webviews.
app/src/webview.rsholds the host page protocol and the resource serving that all platforms share (app/src/webview.rs::Resources). The native view, aNativeper OS, isapp/src/webview/macos.rs(WKWebView),app/src/webview/linux.rs(WebKitGTK) orapp/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.rsfollows upstream's update service against the port's own feed (port/notes.md, "Update feed").port/tools/update-checktests the whole path.
The port/ machinery
port/ is how the work is tracked and checked. It is not part of the app.
port/ledger.mdandport/ledger/: the one source of truth. The Work table has decisions and milestones, open inport/ledger.md, closed inport/ledger/done.md. The Units table, one file per area inport/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.tschecks 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 asport/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/studiomirrors the checkout to the build machine (the Studio) and runs a command there.port/tools/linux-buildruns it in a Linux container,port/tools/windows-buildwith the Windows cross toolchain.port/tools/gui-lanegives a GUI tool its own desktop. Builds and tests do not run on a laptop. - Comparison with VS Code.
port/tools/pixel-checkcompares a still window with the reference.port/tools/ui-pairdrives both apps through the same steps and saves both screenshots.port/tools/ref-dom,port/tools/ref-hoverand their siblings read sizes and styles from the reference. - Extension API.
port/tools/api-testsruns upstream'svscode-api-testsagainst the port.port/tools/extension-testsruns the tests of a built-in extension. - Performance.
port/tools/benchmeasures 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, needsgh),port/tools/check-licenses,port/tools/docs-check.ts.
Build, packaging, release
- Build inputs. The build needs the upstream checkout:
native/vscode_src.rsresolves it for the build scripts.app/build.rsmakes the NLS index from upstream'slocalizecalls 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, thencargo gc build --release -p app.README.mdhas the commands. The debug profile builds dependencies with optimization and the port's crates without (native/Cargo.toml). - Packaging.
port/tools/package-appmakes the signed macOS app and its update files.port/tools/package-linuxmakes the Linux archive.port/tools/app-files.tslists the files a package ships. - CI.
.github/workflows/ci.ymlruns the repository checks and every crate's tests on macOS and Linux for each push tomainand each pull request (Windows, paused, by hand inwindows.yml). By hand,.github/workflows/linux.ymlbuilds a package, and.github/workflows/windows.ymlbuilds 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.rsfile in the folder named after the module, or in atestsmodule at the end of its file. - Memory safety. docs/memory-safety.md has the rules, the inventory of
unsafecode and the checks. - Performance.
port/results/benchmark.mdhas the startup and memory numbers against VS Code, andport/notes.md("Benchmarks", "Scheduling differences") the frame time gates. - Errors. A function that can fail returns
anyhow::Resultor 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-captureandport/tools/telemetry-source-checkcheck 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.