Visual Studio Code, rebuilt in Rust
Extension host bridge
Extensions run in VS Code's own extension host, unchanged. The port is the other end of that connection: it plays the role of the workbench's "main thread" classes. This file goes one level under ARCHITECTURE.md, section "Extension host bridge". Upstream is src/vs/workbench/api and src/vs/workbench/services/extensions.
The host process
- The code is upstream's
src/vs/workbench/api/nodetree.port/tools/build-exthostcopies upstream'ssrc, appliesport/patches, and bundles it intoout-exthost/. The entry isbootstrap-exthost.js, added byport/patches/src-bootstrap-exthost.patch. - The patches are small and each has one reason. Most make the host run on Bun (
port/patches/src-extHostExtensionService.patch,port/patches/src-extensionHostProcess.patch).port/patches/src-extHostInertModules.patchgives extensions inert telemetry modules.port/patches/src-uncFsGuard.patchadds the UNC allowlist check that Electron's Node has.port/notes.md("Bun (1.4.5) as the extension host runtime") explains each Bun change. - The runtime is Bun.
--nodestarts the same bundle on Node, which is the control when a problem can come from Bun.exthost/src/lib.rs::Runtimeselects the executable. - One host per window.
exthost/src/lib.rs::ExtensionHostowns the child process. Dropping it sends Terminate and kills the process.
Do not edit files in upstream/. To change host code, edit the file there, write the diff to port/patches, and restore the file (port/notes.md, "Layout").
Wire format
Three layers, each a port of one upstream file, so a byte on the wire means what it means in VS Code:
- Transport: a Unix socket whose path the host gets in
VSCODE_EXTHOST_IPC_HOOK. On Windows it is a named pipe (exthost/src/pipe.rs). - Frames:
exthost/src/frame.rsisPersistentProtocolofipc.net.ts: a 13-byte header with type, id and ack. - Messages:
exthost/src/rpc.rsisrpcProtocol.ts: requests, replies, errors and cancel, with JSON and binary buffer arguments.
A call names a proxy and a method. A proxy is a number on the wire. exthost/src/proxy_ids.rs lists them in the order upstream creates them, and port/tools/gen-proxy-ids.ts writes that file from upstream's extHost.protocol.ts. Run the generator again after an upstream update.
The handshake is in exthost/src/lib.rs::start: the host sends Ready, the parent sends the init data (exthost/src/session.rs::init_data), the host sends Initialized. Then exthost/src/session.rs::start makes the calls upstream's main thread classes make in their constructors, and starts the extensions.
Threads
Two threads share the work for one window:
- The
exthost-sessionthread runsapp/src/extension_host_start.rs::run_extension_host. It owns theexthost/src/session.rs::Sessionand blocks inexthost/src/session.rs::pumpon the host's next message. - The GPUI main thread owns the
Workbench.
Anything else that has work for the session (the workbench, a file watcher, a search) queues it and wakes the session with an exthost/src/lib.rs::Waker.
Host calls the port answers
A request from the host for MainThreadX.$method takes one of three paths. app/src/main_thread_commands.rs::route names the path and the handler of every call the workbench serves; exthost/src/session.rs::Session::forward reads the path through app/src/main_thread_commands.rs::path, and a call without one stays with the session:
- The session answers it on its own thread. This is for state that needs no window: the disk and file watchers (
exthost/src/main_thread_files.rs), settings (exthost/src/main_thread_configuration.rs), extension storage (exthost/src/main_thread_storage.rs), secrets (exthost/src/main_thread_secret_state.rs), workspace search (exthost/src/main_thread_workspace.rs). - The workbench answers it while the session thread waits. This is for calls whose answer depends on the document state at that moment:
Path::Blockinginroute(open a document, apply an edit, set selections, the clipboard). They cross throughapp/src/main_thread.rs::WorkbenchMainThread. - The session forwards it and goes on. The calls
routegivesPath::Forwardedgo to the main thread asexthost/src/session.rs::ForwardedRequest, andapp/src/main_thread_commands.rs::handle_host_requestanswers them later withexthost/src/session.rs::reply. Most of the API is here: commands, languages, webviews, debug, SCM, terminals, tree views.
A Path::Observed call takes path 1 and the workbench gets a copy (the Window log of console lines and errors).
A method of a proxy route claims whole and that has no arm answers Unknown method $bar on actor MainThreadFoo, as rpcProtocol.ts _doInvokeHandler does. An upstream method the port does not implement yet answers undefined through route's not_ported, and the ledger row of its main thread class lists it. A call the session serves and does not know answers undefined too.
Trap: on path 2 the session thread blocks until the main thread answers (app/src/main_thread.rs::ask). A handler there must be short and must not wait for an answer from the host: the thread that reads the host is the one that waits. A save is the one exception. It runs format on save and dialogs, so it uses app/src/main_thread.rs::ask_while_pumping, which keeps the session sending and reading.
Calls the port makes
app/src/host_sync.rs::HostSync is the workbench's only door to the host.
- State:
send_changes,send_tabs,send_model_changeand their siblings tell the host about documents, editors and tabs, asmainThreadDocumentsAndEditors.tsdoes. - Requests:
app/src/host_sync.rs::callreturns a receiver for the answer.app/src/host_sync.rs::call_cancellablecancels the request in the host when the receiver is dropped. - Activation:
app/src/host_sync.rs::activate_by_eventsends one$activateByEventper event and shares its answer.
Everything goes into one queue. The session thread sends the queue in order, and it sends what is queued before it sends a reply to the host (exthost/src/session.rs::flush_main_thread).
Invariant: one queue, one order. The host must see "document opened" before a request about that document, and an edit before the reply that depends on it. A second channel to the host breaks this.
Invariant: positions and ranges change units at this boundary, and nowhere else: chars in the port, UTF-16 units in the host.
Extensions as data
The workbench does not need a running host to know what extensions contribute. exthost/src/scan.rs reads each package.json into a ScannedExtension, and the workbench builds menus, keybindings, settings, languages, grammars, themes and views from that at startup. The host is needed only when extension code must run.
- Install, uninstall and update:
exthost/src/extension_management.rs, against Open VSX (exthost/src/gallery.rs) with signature checks (exthost/src/sign.rs). - Which extensions run: enablement (
app/src/extension_enablement.rs), workspace trust (app/src/workspace_trust.rs), proposed API (exthost/src/proposed_api.rs). - Restart:
app/src/workbench/extension_host_restart.rsends the host, disposes what it registered, and starts a new session.app/src/workbench/extension_host_restart.rs::stop_extension_hostnames each thing it clears (commands, language features, SCM, webview views, URL handlers, diagnostics). State of the host that it does not name survives the restart, so a feature that keeps host registrations adds its line there.
Check a change
port/tools/cargo-test -p exthostruns the protocol and session tests without a host.exthost/src/bin/exthost-probe.rsstarts a real host without a window, activates every built-in extension and prints a report.port/tools/api-testsruns upstream'svscode-api-testsagainst the port. This is the check for any change to a main thread class.port/tools/extension-testsruns the integration tests of one built-in extension.