Visual Studio Code, rebuilt in Rust
Platform glue per OS
This file covers the code that calls an OS API or behaves differently per OS: where it lives in app/src and exthost/src, the shape a per-OS module has, how its tests run on a machine of another OS, and how a change is checked on an OS the author is not on. It goes one level under ARCHITECTURE.md, section "Platform glue per OS". Upstream has no single folder for this: the port follows Electron's and Chromium's per-OS code behind src/vs/platform/native/electron-main/nativeHostMainService.ts, and src/vs/base/common/platform.ts for the OS constants. Which OS has which feature is the ledger's (the "M23 Linux" and "M24 Windows" rows of port/ledger.md), not this file's.
The shape of a per-OS module
app/src/power.rs is the small example. The rows of the map below that have a file for each OS have the same shape; the other rows keep the code of one or two OSes in the portable file.
- The portable module holds what upstream holds in TypeScript: the request names, the ids, the state (
app/src/power.rs::PowerSaveBlockers,app/src/power.rs::request). It has no OS call. - Each OS has one file beside it, named after the OS:
app/src/power/macos.rs,app/src/power/linux.rs,app/src/power/windows.rs. The portable module declares each with#[cfg(target_os = "...")] modand re-exports the same names from the one that is built. - The contract between them is a set of plain functions with one signature on every OS (
system_idle_time,on_battery_power,watch), plus a type alias when the OS handle differs:Assertionis au32on macOS, an inhibitor on Linux, a request handle on Windows, andPowerSaveBlockersstores it without knowing which. There is no trait and nodyn: one OS is built, so a direct call is enough. - An OS that has no such feature still has the function, with the answer Electron gives there (
app/src/power/windows.rs::thermal_statereturnsunknown, as Electron has a thermal state on macOS only). The caller has nocfg. - Events leave the OS file through a channel the caller passes in (
watch(events: UnboundedSender<PowerEvent>)). The OS callback, which may run on any thread, only sends; the workbench reads the channel on the main thread.
When the split is small, the OS code stays in the portable file as cfg functions of one name: app/src/metered_connection.rs::watch has three bodies, app/src/clipboard.rs three mod pasteboard blocks.
cfg(unix) and cfg(windows) are for what macOS and Linux share against Windows: the pty (app/src/terminal/pty.rs against app/src/terminal/pty/conpty.rs), sockets against named pipes, signals, file modes. cfg(target_os) is for an OS API.
What lives where
| Behavior | Portable module | macOS | Linux | Windows |
|---|---|---|---|---|
| Power monitor and sleep blockers | app/src/power.rs |
CoreGraphics, IOKit | D-Bus: logind, UPower, ScreenSaver | Power requests |
| Message box with a checkbox | app/src/dialog.rs |
NSAlert sheet, in the module itself | GTK dialog, app/src/dialog/linux.rs |
Task dialog, app/src/dialog/windows.rs |
| Open and save dialogs | app/src/file_dialog.rs |
NSOpenPanel, NSSavePanel | xdg-desktop-portal FileChooser | Common item dialogs |
| Keyboard layout | app/src/keyboard_layout.rs |
UCKeyTranslate | The X server's XKB keymap | app/src/keyboard_layout/windows.rs |
| Policies | app/src/policy.rs |
CFPreferences | A policy file | app/src/policy/windows.rs |
| System-wide keybindings | app/src/system_wide_keybindings.rs |
Carbon hot keys | X11 key grabs or the GlobalShortcuts portal | app/src/system_wide_keybindings/windows.rs |
| Webview | app/src/webview.rs |
WKWebView | WebKitGTK | WebView2 |
| HTTP requests and proxy | exthost/src/request.rs |
NSURLSession | curl, exthost/src/request/linux.rs |
The same curl code, with the proxy from WinHTTP |
| Secrets | exthost/src/main_thread_secret_state.rs |
Keychain | Secret Service | Credential Manager |
| Terminal process | app/src/terminal/pty.rs |
openpty and a child in a new session |
The same code | app/src/terminal/pty/conpty.rs |
| IPC transport | exthost/src/lib.rs |
Unix socket | Unix socket | exthost/src/pipe.rs |
Shared helpers on GPUI's native window: app/src/dialog.rs::ns_window (macOS), app/src/x11.rs (Linux: the X window id, window manager states), app/src/win32.rs (Windows: the HWND). Smaller per-OS branches sit inline in app/src/main.rs (the app root, the console), app/src/workbench/host.rs (always on top), app/src/workbench/accessibility.rs (screen reader and reduced motion), app/src/accessibility_signals.rs (the sound player), app/src/diagnostics.rs (machine info) and app/src/app_icon.rs (the Dock icon, macOS only). rg -c 'target_os' native/crates/app/src native/crates/exthost/src lists every file.
Why each OS uses the API it uses is in port/notes.md, "Milestone 23: Linux build" and "Milestone 24: Windows build": the rule is Electron's choice for that OS, then Chromium's, each entry naming the file it follows.
The OS as a value
A behavior that depends on the OS but calls nothing of it is not cfg code. It takes the OS as a parameter, so one test run on one machine checks every branch.
app/src/keybindings.rs::Osis upstream'sOperatingSystem, andapp/src/keybindings.rs::OSthe build's value. Labels, accelerators, the default table (app/src/keybindings.rs::default_table) and theisMac,isWindows,isLinuxcontext keys (app/src/contextkey.rs::constant_in) take anOs.app/src/uri.rs::fs_path_ontakeswindows: bool;app/src/uri.rs::fs_pathpasses the build's.- The pure half of an OS file moves up into the portable module under
#[cfg(any(target_os = "windows", test))]:app/src/dialog.rs::windows_mnemonic,app/src/dialog.rs::task_dialog_answer, and the whole ofexthost/src/request/proxy_rules.rs. It is built on Windows and in every test build, so the macOS test run covers it and no release build of another OS carries it.
Invariant: cfg(target_os) and cfg!(...) are for code that cannot compile or cannot run on the other OS. A rule about labels, paths, button order or key names reads Os. A rule behind cfg is tested only on its own OS, and Windows tests do not run on a push.
FFI and unsafe
memory-safety.md owns the rules, the inventory and the review list. Three things follow from the layout here:
- An
unsafeblock goes in the OS file, and the functions the portable module imports are safe to call. Of the portable modules in the map, two haveunsafe, each as one OS's side:app/src/dialog.rs, whose macOS code is inline, andapp/src/terminal/pty.rs, the Unix side. Outside the map, a module with inlinecfgfunctions holds its own:app/src/clipboard.rs,app/src/metered_connection.rs. - The binding crates of each OS are in the
[target.'cfg(target_os = ...)'.dependencies]tables ofnative/crates/app/Cargo.tomlandnative/crates/exthost/Cargo.toml:objc2and its framework crates on macOS,windows-sysfor Win32 andwindowsfor COM on Windows,zbus,ashpd,gtkandx11rbon Linux. Where GPUI already builds a crate (windows,zbus,x11rb,xkbcommon,oo7), the version is GPUI's, as the comments in those tables say, so the binary links one copy. A new crate for an API one of these covers needs a reason inport/notes.md. - An API with no binding is declared in an
extern "C"block in the file that uses it, asapp/src/metered_connection.rsdoes fornw_path_monitor_create.
Traps
- A new function in the contract needs a body in all three OS files in the same change. The author's build passes without them; the other two fail to compile, and only the cross builds below show it.
- A name that one OS exports alone needs
cfgat each call site:app/src/power/linux.rs::animations_disabledhas no macOS or Windows twin, and its caller inapp/src/workbench/accessibility.rsis undercfg(target_os = "linux"). Prefer a function on every OS. - Linux is two session types. GTK code (the webview, the message box) runs only when
app/src/webview/linux.rs::supportedis true: GPUI's X11 session, on the main thread. On Wayland and in unit tests the caller takes the fallback, so each GTK feature needs one.app/src/x11.rs::window_xidreturns none for a Wayland window. - GTK has no loop of its own here:
app/src/webview/linux.rs::pump_gtkruns its events from GPUI's loop. GTK lives on the thread that initialized it, GPUI's main thread, so every GTK call is made there. - Tests run in parallel on a developer's machine, so a test build must not touch machine state.
app/src/clipboard.rsreplaces itspasteboardmodule with a per-thread one undercfg(test), andapp/src/file_dialog.rsuses GPUI's path prompts in tests. A new glue module that writes shared state (the pasteboard, global hot keys, the Dock) needs the same switch. app/src/keybindings.rs::OShas no Linux value: a Linux build readsMacintosh. Code that must tell Linux from macOS today usescfg, and the M23 rows of the ledger name what moves when the value exists.- A Windows child process that is a console program opens a console window unless it is started through
exthost/src/lib.rs::hide_console. - An OS file may be a
shortcut:stub that compiles and answers a constant (app/src/power/windows.rsfor idle time and battery). Read the//!header of the OS file before relying on it.
Check a change
Run each from the checkout root. The macOS commands build on the Studio through port/tools/studio.
- The portable half and the host tests of the Windows and Linux pure parts, on macOS:
port/tools/cargo-test power::tests,port/tools/cargo-test dialog::tests,port/tools/cargo-test file_dialog::tests,port/tools/cargo-test system_wide_keybindings,port/tools/cargo-test -p exthost proxy_rules. - Linux compiles and its tests pass:
port/tools/linux-build 'cd native && cargo gc check -p app -p exthost --all-targets', then the module's tests in the same container, under Xvfb when they open a window (dialog::linux, the keyboard layout, the X11 key grabs). - Linux starts:
port/tools/linux-build port/tools/linux/smoke. - Windows compiles:
port/tools/windows-build 'cargo gc xwin check -p app -p exthost --all-targets --target x86_64-pc-windows-msvc'. This is a compile check only; nothing runs. - The SAFETY gate per OS: the three commands of memory-safety.md, "The checks".
- CI, after the push.
.github/workflows/ci.ymlruns on each push tomainand each pull request: clippy's SAFETY check on macOS arm64 and Linux x64 and arm64, a smoke start on macOS arm64 and Linux x64, andcargo test --workspaceon macOS arm64 and, underxvfb-run, on Linux x64 and arm64. The Linux test jobs arecontinue-on-error, so a red one does not fail the run: open the job and read it. - Windows tests run only in
.github/workflows/windows.yml, started by hand (gh workflow run windows.yml --ref <branch>): a release build andcargo test -p app -p exthoston x64 and arm64. Run it for every change to awindows.rsfile or to code undercfg(windows). .github/workflows/linux.ymlbuilds and uploads the Linux x64 package by hand. It runs no tests.