From 2c2f737ab2f3db793b7a313bc8e5307f1708d2e8 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 29 Oct 2026 09:12:10 +0000 Subject: [PATCH] gpui: Add sendable foreground execution for web HTTP clients (#66165) ## Summary This cleans up the gpui_platform code a bit, and in combination the way we set up the http client for the web. This does introduce a `ForegroundExecutor` variant that it itself is Send-able, allowing to dispatch foreground futures from background workers (by requiring the future to be Send, unlike `ForegroundExecutor`), which is a requirement for web fetch. a31c7b54c60738b82524e4e4c12aec2f6caf101d fixes a bunch of warnings with our binary names Release Notes: - [gpui] N/A --- Cargo.lock | 1 + crates/auto_update_helper/Cargo.toml | 2 +- crates/docs_preprocessor/Cargo.toml | 2 +- crates/edit_prediction_metrics/Cargo.toml | 4 + crates/edit_prediction_metrics/src/main.rs | 2 +- crates/editor_benchmarks/Cargo.toml | 4 + crates/editor_benchmarks/src/main.rs | 4 +- crates/fs_benchmarks/Cargo.toml | 4 + crates/gpui/Cargo.toml | 3 +- crates/gpui/examples/image/image.rs | 13 ++- crates/gpui/examples/image_gallery.rs | 11 +-- crates/gpui/src/app/bench_context.rs | 13 ++- crates/gpui/src/executor.rs | 43 ++++++++++ crates/gpui/src/platform/test/platform.rs | 5 ++ crates/gpui_platform/src/gpui_platform.rs | 70 +++++----------- crates/gpui_web/examples/hello_web/Cargo.toml | 16 ++-- .../examples/hello_web/http_client_test.rs | 6 +- crates/gpui_web/examples/hello_web/index.html | 26 +++--- .../hello_web/test_canvas_fallback.mjs | 2 +- .../examples/hello_web/test_http_client.mjs | 2 +- .../examples/hello_web/test_queue.mjs | 2 +- crates/gpui_web/src/dispatcher.rs | 16 +--- crates/gpui_web/src/http_client.rs | 37 +++++---- crates/gpui_web/src/platform.rs | 6 +- crates/http_proxy/Cargo.toml | 2 +- crates/http_proxy/src/pinned_host.rs | 2 +- crates/project_benchmarks/Cargo.toml | 2 +- crates/remote/src/transport.rs | 12 ++- crates/remote_server/Cargo.toml | 3 +- crates/remote_server/tests/command.rs | 6 +- crates/sandbox/Cargo.toml | 8 +- crates/sandbox/src/bwrap_test_helper.rs | 2 +- crates/sandbox/src/wsl_sandbox_test_helper.rs | 8 +- crates/scheduler/src/executor.rs | 38 +++++++++ crates/scheduler/src/tests.rs | 83 +++++++++++++++++++ crates/schema_generator/Cargo.toml | 4 + crates/theme_importer/Cargo.toml | 4 + crates/worktree_benchmarks/Cargo.toml | 4 + crates/zed/Cargo.toml | 2 +- crates/zed/src/visual_test_runner.rs | 4 +- docs/README.md | 4 +- docs/src/development/macos.md | 6 +- docs/src/remote-development.md | 4 +- nix/modules/devshells.nix | 6 +- nix/tests/sandboxing/default.nix | 4 +- nix/tests/sandboxing/helper.nix | 6 +- script/bundle-freebsd | 12 +-- script/bundle-linux | 8 +- script/bundle-mac | 10 +-- script/bundle-windows.ps1 | 10 ++- script/test-wsl-sandbox.ps1 | 2 +- tooling/xtask/src/tasks/wsl_sandbox_tests.rs | 4 +- 52 files changed, 359 insertions(+), 195 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9fdd182c65d..3d9de6a982c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7364,6 +7364,7 @@ dependencies = [ "gpui_platform", "gpui_shared_string", "gpui_util", + "gpui_web", "hdrhistogram", "heapless", "http_client", diff --git a/crates/auto_update_helper/Cargo.toml b/crates/auto_update_helper/Cargo.toml index 289779752e6..b267d612cd8 100644 --- a/crates/auto_update_helper/Cargo.toml +++ b/crates/auto_update_helper/Cargo.toml @@ -9,7 +9,7 @@ license = "GPL-3.0-or-later" workspace = true [[bin]] -name = "auto_update_helper" +name = "auto-update-helper" path = "src/auto_update_helper.rs" doctest = false diff --git a/crates/docs_preprocessor/Cargo.toml b/crates/docs_preprocessor/Cargo.toml index cb57c7b97ea..09d8affb821 100644 --- a/crates/docs_preprocessor/Cargo.toml +++ b/crates/docs_preprocessor/Cargo.toml @@ -26,5 +26,5 @@ theme.workspace = true workspace = true [[bin]] -name = "docs_preprocessor" +name = "docs-preprocessor" path = "src/main.rs" diff --git a/crates/edit_prediction_metrics/Cargo.toml b/crates/edit_prediction_metrics/Cargo.toml index 860226e7c71..2c567682fb8 100644 --- a/crates/edit_prediction_metrics/Cargo.toml +++ b/crates/edit_prediction_metrics/Cargo.toml @@ -12,6 +12,10 @@ workspace = true path = "src/edit_prediction_metrics.rs" doctest = false +[[bin]] +name = "edit-prediction-metrics" +path = "src/main.rs" + [features] tree-sitter = ["dep:tree-sitter"] diff --git a/crates/edit_prediction_metrics/src/main.rs b/crates/edit_prediction_metrics/src/main.rs index c7c55ceff60..8c2d23774f2 100644 --- a/crates/edit_prediction_metrics/src/main.rs +++ b/crates/edit_prediction_metrics/src/main.rs @@ -144,7 +144,7 @@ fn report_from_json_example( fn print_usage() { eprintln!( - "Usage:\n edit_prediction_metrics --base --expected-patch --actual-patch \n edit_prediction_metrics --json [--prediction-index ]" + "Usage:\n edit-prediction-metrics --base --expected-patch --actual-patch \n edit-prediction-metrics --json [--prediction-index ]" ); } diff --git a/crates/editor_benchmarks/Cargo.toml b/crates/editor_benchmarks/Cargo.toml index 8db5d4b26ae..db0ae0a9402 100644 --- a/crates/editor_benchmarks/Cargo.toml +++ b/crates/editor_benchmarks/Cargo.toml @@ -4,6 +4,10 @@ version = "0.1.0" publish.workspace = true edition.workspace = true +[[bin]] +name = "editor-benchmarks" +path = "src/main.rs" + [dependencies] anyhow.workspace = true editor.workspace = true diff --git a/crates/editor_benchmarks/src/main.rs b/crates/editor_benchmarks/src/main.rs index 09e100bc08d..f7ffc734d79 100644 --- a/crates/editor_benchmarks/src/main.rs +++ b/crates/editor_benchmarks/src/main.rs @@ -42,7 +42,7 @@ fn parse_args() -> Args { } "--help" | "-h" => { eprintln!( - "Usage: editor_benchmarks [OPTIONS] \n\n\ + "Usage: editor-benchmarks [OPTIONS] \n\n\ Arguments:\n \ Path to the file to search in\n \ The search query string\n\n\ @@ -60,7 +60,7 @@ fn parse_args() -> Args { } if positional.len() < 2 { - eprintln!("Usage: editor_benchmarks [OPTIONS] "); + eprintln!("Usage: editor-benchmarks [OPTIONS] "); std::process::exit(1); } parsed.file = positional.remove(0); diff --git a/crates/fs_benchmarks/Cargo.toml b/crates/fs_benchmarks/Cargo.toml index c597ec6469e..081b64aea0c 100644 --- a/crates/fs_benchmarks/Cargo.toml +++ b/crates/fs_benchmarks/Cargo.toml @@ -4,6 +4,10 @@ version = "0.1.0" publish.workspace = true edition.workspace = true +[[bin]] +name = "fs-benchmarks" +path = "src/main.rs" + [dependencies] fs.workspace = true gpui.workspace = true diff --git a/crates/gpui/Cargo.toml b/crates/gpui/Cargo.toml index ff5a7030b44..f215c006fd6 100644 --- a/crates/gpui/Cargo.toml +++ b/crates/gpui/Cargo.toml @@ -20,6 +20,7 @@ workspace = true [features] default = ["font-kit", "wayland", "x11", "windows-manifest"] test-support = [ + "bench_metrics?/test-support", "collections/test-support", "http_client/test-support", "wayland", @@ -173,7 +174,6 @@ windows = { workspace = true, features = [ [dev-dependencies] backtrace.workspace = true -bench_metrics = { workspace = true, features = ["test-support"] } collections = { workspace = true, features = ["test-support"] } env_logger.workspace = true futures-concurrency.workspace = true @@ -192,6 +192,7 @@ proptest.workspace = true reqwest_client = { workspace = true, features = ["test-support"] } [target.'cfg(target_family = "wasm")'.dev-dependencies] +gpui_web.workspace = true wasm-bindgen.workspace = true web-sys.workspace = true diff --git a/crates/gpui/examples/image/image.rs b/crates/gpui/examples/image/image.rs index 30160c65dd0..f3a7a1dd62a 100644 --- a/crates/gpui/examples/image/image.rs +++ b/crates/gpui/examples/image/image.rs @@ -154,10 +154,13 @@ actions!(image, [Quit]); fn run_example() { let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let app = gpui_platform::application(); #[cfg(not(target_family = "wasm"))] - let app = gpui_platform::application(); + let http_client = + ReqwestClient::user_agent("gpui example").expect("failed to create HTTP client"); #[cfg(target_family = "wasm")] - let app = gpui_platform::application(); + let http_client = gpui_web::FetchHttpClient::new(app.foreground_executor().to_send()); + let app = app.with_http_client(Arc::new(http_client)); app.with_assets(Assets { base: manifest_dir.join("examples"), }) @@ -165,12 +168,6 @@ fn run_example() { if !example_support::load_fonts(cx) { return; } - #[cfg(not(target_family = "wasm"))] - { - let http_client = ReqwestClient::user_agent("gpui example").unwrap(); - cx.set_http_client(Arc::new(http_client)); - } - cx.activate(true); cx.on_action(|_: &Quit, cx| cx.quit()); cx.bind_keys([KeyBinding::new("cmd-q", Quit, None)]); diff --git a/crates/gpui/examples/image_gallery.rs b/crates/gpui/examples/image_gallery.rs index d1cb919698f..d5929e190a4 100644 --- a/crates/gpui/examples/image_gallery.rs +++ b/crates/gpui/examples/image_gallery.rs @@ -241,16 +241,17 @@ fn run_example() { let app = gpui_platform::application(); #[cfg(target_family = "wasm")] let app = gpui_platform::single_threaded_web(); + #[cfg(not(target_family = "wasm"))] + let http_client = + ReqwestClient::user_agent("gpui example").expect("failed to create HTTP client"); + #[cfg(target_family = "wasm")] + let http_client = gpui_web::FetchHttpClient::new(app.foreground_executor().to_send()); + let app = app.with_http_client(Arc::new(http_client)); app.run(move |cx: &mut App| { if !example_support::load_fonts(cx) { return; } - #[cfg(not(target_family = "wasm"))] - { - let http_client = ReqwestClient::user_agent("gpui example").unwrap(); - cx.set_http_client(Arc::new(http_client)); - } cx.activate(true); cx.on_action(|_: &Quit, cx| cx.quit()); diff --git a/crates/gpui/src/app/bench_context.rs b/crates/gpui/src/app/bench_context.rs index 2cf504c510c..a577a0bc407 100644 --- a/crates/gpui/src/app/bench_context.rs +++ b/crates/gpui/src/app/bench_context.rs @@ -1352,13 +1352,9 @@ impl VisualContext for BenchWindowContext<'_, '_> { #[cfg(test)] mod tests { - use std::{ - rc::Rc, - sync::{ - Arc, - atomic::{AtomicU64, Ordering}, - }, - }; + #[cfg(feature = "test-support")] + use std::sync::atomic::{AtomicU64, Ordering}; + use std::{rc::Rc, sync::Arc}; use super::*; use crate::profiler::journal::install_test_foreground_journal; @@ -1366,6 +1362,7 @@ mod tests { /// Runs `benchmark` under a wall-time primary with a fake counter as the /// secondary metric and returns the per-iteration values the report /// recorded for it. + #[cfg(feature = "test-support")] fn secondary_values_per_iteration( counter: Arc, benchmark: impl Fn(&mut BenchAppContext, &Arc), @@ -1407,6 +1404,7 @@ mod tests { .collect() } + #[cfg(feature = "test-support")] #[test] fn secondary_metric_is_normalized_per_iteration_for_bench_iter() { let values = secondary_values_per_iteration(Arc::new(AtomicU64::new(0)), |cx, counter| { @@ -1421,6 +1419,7 @@ mod tests { ); } + #[cfg(feature = "test-support")] #[test] fn secondary_metric_is_accumulated_across_per_iteration_batches() { let values = secondary_values_per_iteration(Arc::new(AtomicU64::new(0)), |cx, counter| { diff --git a/crates/gpui/src/executor.rs b/crates/gpui/src/executor.rs index 78928d0f541..18f45e7aac5 100644 --- a/crates/gpui/src/executor.rs +++ b/crates/gpui/src/executor.rs @@ -32,6 +32,23 @@ pub struct ForegroundExecutor { not_send: PhantomData>, } +/// A thread-safe handle for spawning `Send` futures on the main thread. +#[derive(Clone)] +pub struct SendForegroundExecutor { + inner: scheduler::SendLocalExecutor, +} + +impl SendForegroundExecutor { + /// Enqueues a future to run on the main thread, even when submitted from another thread. + #[track_caller] + pub fn spawn(&self, future: impl Future + Send + 'static) -> Task + where + R: Send + 'static, + { + self.inner.spawn(future) + } +} + /// Extension trait for `Task>` that adds `detach_and_log_err` with an `&App` context. /// /// This trait is automatically implemented for all `Task>` types. @@ -297,6 +314,13 @@ impl BackgroundExecutor { } impl ForegroundExecutor { + /// Creates a thread-safe handle that submits `Send` futures to this foreground executor. + pub fn to_send(&self) -> SendForegroundExecutor { + SendForegroundExecutor { + inner: self.inner.to_send(), + } + } + /// Creates a new ForegroundExecutor from the given PlatformDispatcher. pub fn new(dispatcher: Arc) -> Self { #[cfg(any(test, feature = "test-support"))] @@ -555,6 +579,25 @@ mod test { (dispatcher, background_executor, app) } + #[test] + fn send_foreground_executor_submits_from_worker() { + let dispatcher = TestDispatcher::new(0); + let foreground_executor = ForegroundExecutor::new(Arc::new(dispatcher.clone())); + let main_thread = std::thread::current().id(); + let foreground_executor = foreground_executor.to_send(); + let task = std::thread::spawn(move || { + foreground_executor.spawn(async move { + assert_eq!(std::thread::current().id(), main_thread); + 42 + }) + }) + .join() + .expect("worker must submit the foreground task"); + + dispatcher.run_until_parked(); + assert_eq!(task.now_or_never(), Some(42)); + } + #[test] fn sanity_test_tasks_run() { let (dispatcher, _background_executor, app) = create_test_app(); diff --git a/crates/gpui/src/platform/test/platform.rs b/crates/gpui/src/platform/test/platform.rs index 7b20738889c..b417fd0ab21 100644 --- a/crates/gpui/src/platform/test/platform.rs +++ b/crates/gpui/src/platform/test/platform.rs @@ -145,6 +145,7 @@ impl TestPlatform { Self::with_platform(executor, foreground_executor, text_system, None) } + #[cfg(any(test, feature = "test-support"))] pub(crate) fn simulate_display_added( &self, display_id: DisplayId, @@ -156,6 +157,7 @@ impl TestPlatform { /// Adds a display without telling App yet, as when the platform's /// notifications about a window arrive before those about displays. + #[cfg(any(test, feature = "test-support"))] pub(crate) fn connect_display( &self, display_id: DisplayId, @@ -166,6 +168,7 @@ impl TestPlatform { self.displays.borrow_mut().push(display); } + #[cfg(any(test, feature = "test-support"))] pub(crate) fn simulate_display_removed(&self, display_id: DisplayId) { self.displays .borrow_mut() @@ -173,6 +176,7 @@ impl TestPlatform { self.report_displays_changed(); } + #[cfg(any(test, feature = "test-support"))] pub(crate) fn simulate_display_refresh_interval_change( &self, display_id: DisplayId, @@ -186,6 +190,7 @@ impl TestPlatform { self.report_displays_changed(); } + #[cfg(any(test, feature = "test-support"))] fn report_displays_changed(&self) { let callback = self.displays_changed_callback.borrow_mut().take(); if let Some(mut callback) = callback { diff --git a/crates/gpui_platform/src/gpui_platform.rs b/crates/gpui_platform/src/gpui_platform.rs index 706caf80db4..0b2dde5296d 100644 --- a/crates/gpui_platform/src/gpui_platform.rs +++ b/crates/gpui_platform/src/gpui_platform.rs @@ -3,6 +3,9 @@ pub use gpui::Platform; +#[cfg(target_family = "wasm")] +pub use gpui_web::WebBackendPreference; + use std::rc::Rc; /// Returns a background executor for the current platform. @@ -11,12 +14,6 @@ pub fn background_executor() -> gpui::BackgroundExecutor { } pub fn application() -> gpui::Application { - #[cfg(target_family = "wasm")] - { - application_with_web_backend(gpui_web::WebBackendPreference::Auto) - } - - #[cfg(not(target_family = "wasm"))] gpui::Application::with_platform(current_platform(false)) } @@ -37,25 +34,20 @@ pub fn linux(allowed_modes: gpui::WindowingModes) -> gpui::Application { gpui::Application::with_platform(gpui_linux::linux_platform(allowed_modes)) } -#[cfg(target_family = "wasm")] -pub use gpui_web::WebBackendPreference; - #[cfg(target_family = "wasm")] pub fn application_with_web_backend(backend_preference: WebBackendPreference) -> gpui::Application { let platform = Rc::new(gpui_web::WebPlatform::new_with_backend( true, backend_preference, )); - let http_client = std::sync::Arc::new(platform.fetch_http_client()); - gpui::Application::with_platform(platform).with_http_client(http_client) + gpui::Application::with_platform(platform) } /// Unlike `application`, this function returns a single-threaded web application. #[cfg(target_family = "wasm")] pub fn single_threaded_web() -> gpui::Application { let platform = Rc::new(gpui_web::WebPlatform::new(false)); - let http_client = std::sync::Arc::new(platform.fetch_http_client()); - gpui::Application::with_platform(platform).with_http_client(http_client) + gpui::Application::with_platform(platform) } /// Initializes panic hooks and logging for the web platform. @@ -68,44 +60,24 @@ pub fn web_init() { /// Returns the default [`Platform`] for the current OS. pub fn current_platform(headless: bool) -> Rc { - #[cfg(target_os = "ios")] - { - gpui_ios::current_platform(headless) - } - - #[cfg(target_os = "macos")] - { - Rc::new(gpui_macos::MacPlatform::new(headless)) - } - - #[cfg(target_os = "windows")] - { - Rc::new( + cfg_select! { + target_os = "ios" => gpui_ios::current_platform(headless), + target_os = "macos" => Rc::new(gpui_macos::MacPlatform::new(headless)), + target_os = "windows" => Rc::new( gpui_windows::WindowsPlatform::new(headless) .expect("failed to initialize Windows platform"), - ) - } - - #[cfg(target_env = "ohos")] - { - let _ = headless; - panic!( - "gpui_platform does not select an OpenHarmony backend; inject gpui-ohos with Application::with_platform" - ) - } - - #[cfg(all( - any(target_os = "linux", target_os = "freebsd"), - not(target_env = "ohos") - ))] - { - gpui_linux::current_platform(headless) - } - - #[cfg(target_family = "wasm")] - { - let _ = headless; - Rc::new(gpui_web::WebPlatform::new(true)) + ), + all( + any(target_os = "linux", target_os = "freebsd"), + not(target_env = "ohos") + ) => gpui_linux::current_platform(headless), + target_family = "wasm" => { + let _ = headless; + Rc::new(gpui_web::WebPlatform::new_with_backend( + true, + gpui_web::WebBackendPreference::Auto, + )) + }, } } diff --git a/crates/gpui_web/examples/hello_web/Cargo.toml b/crates/gpui_web/examples/hello_web/Cargo.toml index 4266cb4e61b..17abc604190 100644 --- a/crates/gpui_web/examples/hello_web/Cargo.toml +++ b/crates/gpui_web/examples/hello_web/Cargo.toml @@ -7,11 +7,11 @@ edition = "2024" publish = false [[bin]] -name = "hello_web" +name = "hello-web" path = "main.rs" [[bin]] -name = "hello_world" +name = "hello-world" path = "../../../gpui/examples/hello_world.rs" [[bin]] @@ -19,11 +19,11 @@ name = "text" path = "../../../gpui/examples/text.rs" [[bin]] -name = "text_layout" +name = "text-layout" path = "../../../gpui/examples/text_layout.rs" [[bin]] -name = "text_wrapper" +name = "text-wrapper" path = "../../../gpui/examples/text_wrapper.rs" [[bin]] @@ -31,19 +31,19 @@ name = "input" path = "../../../gpui/examples/input.rs" [[bin]] -name = "font_fallback_test" +name = "font-fallback-test" path = "font_fallback_test.rs" [[bin]] -name = "queue_test" +name = "queue-test" path = "queue_test.rs" [[bin]] -name = "dynamic_fonts" +name = "dynamic-fonts" path = "dynamic_fonts.rs" [[bin]] -name = "http_client_test" +name = "http-client-test" path = "http_client_test.rs" [dependencies] diff --git a/crates/gpui_web/examples/hello_web/http_client_test.rs b/crates/gpui_web/examples/hello_web/http_client_test.rs index 70a8d7d1327..42b1eda1b3b 100644 --- a/crates/gpui_web/examples/hello_web/http_client_test.rs +++ b/crates/gpui_web/examples/hello_web/http_client_test.rs @@ -5,7 +5,7 @@ use futures::AsyncReadExt as _; use gpui::Platform as _; -use gpui_web::WebPlatform; +use gpui_web::{FetchHttpClient, WebPlatform}; use http_client::HttpClient as _; use std::sync::Arc; use wasm_bindgen::prelude::*; @@ -15,7 +15,9 @@ fn main() {} #[wasm_bindgen] pub async fn test_worker_fetch(url: String) -> Result<(), JsValue> { let platform = WebPlatform::new(true); - let client = Arc::new(platform.fetch_http_client()); + let client = Arc::new(FetchHttpClient::new( + platform.foreground_executor().to_send(), + )); let background = platform.background_executor(); let main_thread = std::thread::current().id(); let readers = (0..8).map(|_| { diff --git a/crates/gpui_web/examples/hello_web/index.html b/crates/gpui_web/examples/hello_web/index.html index ef47840cef3..e46ce384e28 100644 --- a/crates/gpui_web/examples/hello_web/index.html +++ b/crates/gpui_web/examples/hello_web/index.html @@ -4,19 +4,19 @@ GPUI Web Examples - + - + - - + + - - - - + + + +