Skip to content

Visual Studio Code, rebuilt in Rust

Rust
11.1 KB
Raw Blame

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 = "...")] mod and 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: Assertion is a u32 on macOS, an inhibitor on Linux, a request handle on Windows, and PowerSaveBlockers stores it without knowing which. There is no trait and no dyn: 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_state returns unknown, as Electron has a thermal state on macOS only). The caller has no cfg.
  • 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::Os is upstream's OperatingSystem, and app/src/keybindings.rs::OS the build's value. Labels, accelerators, the default table (app/src/keybindings.rs::default_table) and the isMac, isWindows, isLinux context keys (app/src/contextkey.rs::constant_in) take an Os.
  • app/src/uri.rs::fs_path_on takes windows: bool; app/src/uri.rs::fs_path passes 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 of exthost/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 unsafe block goes in the OS file, and the functions the portable module imports are safe to call. Of the portable modules in the map, two have unsafe, each as one OS's side: app/src/dialog.rs, whose macOS code is inline, and app/src/terminal/pty.rs, the Unix side. Outside the map, a module with inline cfg functions 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 of native/crates/app/Cargo.toml and native/crates/exthost/Cargo.toml: objc2 and its framework crates on macOS, windows-sys for Win32 and windows for COM on Windows, zbus, ashpd, gtk and x11rb on 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 in port/notes.md.
  • An API with no binding is declared in an extern "C" block in the file that uses it, as app/src/metered_connection.rs does for nw_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 cfg at each call site: app/src/power/linux.rs::animations_disabled has no macOS or Windows twin, and its caller in app/src/workbench/accessibility.rs is under cfg(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::supported is 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_xid returns none for a Wayland window.
  • GTK has no loop of its own here: app/src/webview/linux.rs::pump_gtk runs 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.rs replaces its pasteboard module with a per-thread one under cfg(test), and app/src/file_dialog.rs uses 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::OS has no Linux value: a Linux build reads Macintosh. Code that must tell Linux from macOS today uses cfg, 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.rs for 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.yml runs on each push to main and each pull request: clippy's SAFETY check on macOS arm64 and Linux x64 and arm64, a smoke start on macOS arm64 and Linux x64, and cargo test --workspace on macOS arm64 and, under xvfb-run, on Linux x64 and arm64. The Linux test jobs are continue-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 and cargo test -p app -p exthost on x64 and arm64. Run it for every change to a windows.rs file or to code under cfg(windows).
  • .github/workflows/linux.yml builds and uploads the Linux x64 package by hand. It runs no tests.