Visual Studio Code, rebuilt in Rust
Debug
Debugging is the breakpoint and watch model, the sessions that speak the debug adapter protocol (DAP), and the Run and Debug views. This file goes one level under ARCHITECTURE.md, section "Extension host bridge", and under extension-host.md. Upstream is src/vs/workbench/contrib/debug and src/vs/workbench/api/browser/mainThreadDebugService.ts. The decisions per feature (hover, inline values, memory, extension host debugging) are in port/notes.md, "Milestone 16: debug and testing".
What is where
Data without a window, in app/src/debug.rs and its submodules:
app/src/debug.rs::DebugModel: breakpoints of every kind and the watch expressions, debugModel.tsDebugModelwithout the sessions. It also holds the storage round trip of debugStorage.ts (storage_entries,from_storage) and the breakpoint DTOs for the extension host (dtos).app/src/debug/session.rs::Session: one session's threads, frames, capabilities and its DAP request bookkeeping (debugSession.ts and the request state of rawDebugSession.ts). The same file converts paths as debugUtils.ts does.app/src/debug/configuration.rs: the debuggers extensions contribute, launch.json asLaunch, and the${command:...}and${input:...}references of a configuration.app/src/debug/disassembly.rs::Disassembly: the instruction list of the Disassembly view, merged by address.app/src/debug/ansi.rs::parse: output text split into styled runs (debugANSIHandling.ts).
The service and the views, in app/src/workbench:
app/src/workbench/debug_service.rs::DebugService: the state of debugService.ts, debugViewModel.ts and debugAdapterManager.ts in one struct: the model, the sessions, the focus, the variables cache, the REPL. It is the fielddebugofapp/src/workbench.rs::Workbench. The file'simpl Workbenchblock is the protocol: start, stop, restart, events, breakpoints sent to sessions.app/src/workbench/debug_view.rs: the Run and Debug views.debug_console.rs,debug_toolbar.rs,breakpoint_widget.rs,inline_breakpoints.rs,debug_hover.rs,debug_inline_values.rs,disassembly_view.rs,debug_memory.rsanddebug_visualizers.rsbeside it each name the upstream files they port in their//!header.app/src/workbench/debug_commands.rs: the commands, anddebug_context, the context keys of debug.ts.app/src/workbench/debug_task_runner.rsandapp/src/workbench/debug_dynamic.rs:preLaunchTaskandpostDebugTask, and dynamic configurations.
Who owns the adapter
The extension host owns every debug adapter. Inline, executable and server adapters all run behind ExtHostDebugService, as on VS Code desktop, and the app starts no adapter process. The app owns the session: it decides which DAP request goes out and when, and it holds all state.
The path of a message:
- App to adapter:
app/src/workbench/debug_service.rs::dap_requestgives the request its sequence number (app/src/debug/session.rs::request), converts paths, and sends$sendDAMessagewith the session'shandlethroughapp/src/host_sync.rs::HostSync. It returns a receiver for the response. - Adapter to app: the host calls
MainThreadDebugService.$acceptDAMessage.app/src/main_thread_commands.rs::routeforwards the whole proxy (path 3 of extension-host.md), andapp/src/workbench/debug_service.rs::handle_debug_requesthands the message toaccept_adapter_message. A response resolves its pending request, an event goes toaccept_adapter_event, a reverse request (runInTerminal,startDebugging,launchVSCode) toaccept_reverse_request. - The adapter ends:
$acceptDAExitor$acceptDAErrorreachadapter_exited, andapp/src/debug/session.rs::cancel_pendingfails every request still waiting.
The host addresses a session by handle, the workbench by id. app/src/workbench/debug_service.rs::session_by_handle is the one lookup from a handle to its session, and it finds only running sessions.
How state reaches the views
There is no event emitter. The views are impl Workbench methods that read self.debug in render, and whoever changes debug state calls cx.notify(). Three functions bundle the follow-up work, use them instead of a bare field write:
app/src/workbench/debug_service.rs::breakpoints_changed: stores the model, sends$acceptBreakpointsDeltato the host, sends the breakpoints to every session again whensend_to_sessionsis set, and refreshes the Breakpoints hint. A change markedsession_onlygoes to neither. TheDebugModelmethods that add, remove, update or enable breakpoints return aapp/src/debug.rs::BreakpointsChangefor it; the three that set exception breakpoints per session do not.app/src/workbench/debug_service.rs::set_debug_focus: the way to move the focus to a session, thread or frame. It tells the host and reveals the frame. Three functions writefocuswithout it, to drop the frame of a resumed thread or to leave an ended session:threads_continued,session_endedandapp/src/workbench/debug_commands.rs::resume_thread.app/src/workbench/debug_service.rs::update_debug_views: fetches the shown variable rows and watch values again, each keeping its rows until the reply.
A flow that waits on the adapter (a start, a restart, a lazy variable) is an async function that takes a WeakEntity<Workbench> and re-enters through app/src/workbench/debug_service.rs::on_workbench for each step. It holds no borrow of the workbench across an await.
Invariants
- Message order at session start.
launch_or_attachcalls$startDASessionso the host creates the adapter, then sendsinitialize, thenlaunchorattach. Aninitializedevent that arrives with the initialize response setsinitialized_event_pendingand is handled only after the launch request went out, because js-debug rejectssetBreakpointsbeforelaunch.session_initializedthen sends all breakpoints, waits for their answers, sendsconfigurationDoneif the adapter supports it, and fetches the threads. - One sequence counter per session. Requests and the responses to reverse requests both take the next number (
app/src/debug/session.rs::number). - Paths convert at one boundary. Toward the adapter
to_adapter_pathsturns source paths intoUriComponents; from itfrom_adapter_pathsturns them back. Inside the app a source is a path. - Lines. A breakpoint's line is 1-based in the model and in DAP, and 0-based in the DTO the host gets (
debug::tests::dtos_carry_zero_based_lines). - Cancellation tokens. A host method whose last parameter is a
CancellationTokengets its token when the call goes out throughapp/src/host_sync.rs::call_cancellable, or throughcallwith an entry inapp/src/main_thread.rs::TOKEN_METHODS. Without either the extension getsundefinedas its token.
Traps
- A handler in
handle_debug_requestthat needs the adapter's answer returnsOutcome::Pendingwith the receiver. Do not wait in the handler: it runs on the main thread. app/src/workbench/debug_memory.rsserves thevscode-debug-memoryfile system from the workbench while the session thread waits, and its answer needs areadMemoryround trip to the host. This works only becauseexthost/src/session.rs::ask_workbenchkeeps sending the workbench's queued calls while it waits.port/notes.md("View Binary Data") has the case.sessionsholds running sessions,inactive_sessionsthe ended ones upstream keeps asState.Inactive.app/src/workbench/debug_service.rs::sessionfinds only running ones; usesessions_including_inactivefor a view that lists both.app/src/workbench/extension_host_restart.rs::stop_extension_hostclears the host's commands, language features, SCM model, webview views and markers. It does not clearprovidersoradapter_types, and no other code innative/cratesclears them on a restart: the new host's$registerDebugTypesreplacesadapter_types, and its$registerDebugConfigurationProvidercalls add to theprovidersof the old host. A running session vetoes a restart (stop_extension_hosts), and the user can answer Restart Anyway.- An
extensionHostsession needs a V8 inspector, so the host it debugs runs on Node, not Bun (port/notes.md, "Extension host debugging").
Check a change
port/tools/cargo-test -p app debug::runs the model, storage, session and path tests (upstream's breakpoints.test.ts and debugUtils.test.ts cases).port/tools/cargo-test -p app debug_serviceandport/tools/cargo-test -p app debug_viewrun the service and view tests, most of them in a test window.MOCHA_GREP="vscode API - debug" port/tools/api-testsruns upstream's API tests forMainThreadDebugService.port/tools/studio --lane 'FOLDER=port/fixtures/debug-node FILE=app.js port/tools/ui-pair /tmp/out port/fixtures/ui-pair/m16/breakpoint.json'compares a real session against the reference app: the script sets a breakpoint on line 5 of the open file and runs Launch app.js.port/fixtures/ui-pair/debugholds the scripts of more cases, one per file.