Skip to content

Visual Studio Code, rebuilt in Rust

Rust
7.9 KB
Raw Blame

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/node tree. port/tools/build-exthost copies upstream's src, applies port/patches, and bundles it into out-exthost/. The entry is bootstrap-exthost.js, added by port/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.patch gives extensions inert telemetry modules. port/patches/src-uncFsGuard.patch adds 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. --node starts the same bundle on Node, which is the control when a problem can come from Bun. exthost/src/lib.rs::Runtime selects the executable.
  • One host per window. exthost/src/lib.rs::ExtensionHost owns 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:

  1. 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).
  2. Frames: exthost/src/frame.rs is PersistentProtocol of ipc.net.ts: a 13-byte header with type, id and ack.
  3. Messages: exthost/src/rpc.rs is rpcProtocol.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-session thread runs app/src/extension_host_start.rs::run_extension_host. It owns the exthost/src/session.rs::Session and blocks in exthost/src/session.rs::pump on 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:

  1. 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).
  2. The workbench answers it while the session thread waits. This is for calls whose answer depends on the document state at that moment: Path::Blocking in route (open a document, apply an edit, set selections, the clipboard). They cross through app/src/main_thread.rs::WorkbenchMainThread.
  3. The session forwards it and goes on. The calls route gives Path::Forwarded go to the main thread as exthost/src/session.rs::ForwardedRequest, and app/src/main_thread_commands.rs::handle_host_request answers them later with exthost/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_change and their siblings tell the host about documents, editors and tabs, as mainThreadDocumentsAndEditors.ts does.
  • Requests: app/src/host_sync.rs::call returns a receiver for the answer. app/src/host_sync.rs::call_cancellable cancels the request in the host when the receiver is dropped.
  • Activation: app/src/host_sync.rs::activate_by_event sends one $activateByEvent per 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.rs ends the host, disposes what it registered, and starts a new session. app/src/workbench/extension_host_restart.rs::stop_extension_host names 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 exthost runs the protocol and session tests without a host.
  • exthost/src/bin/exthost-probe.rs starts a real host without a window, activates every built-in extension and prints a report.
  • port/tools/api-tests runs upstream's vscode-api-tests against the port. This is the check for any change to a main thread class.
  • port/tools/extension-tests runs the integration tests of one built-in extension.