Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
lsp: Fix duplicate workspace/didChangeConfiguration notifications (#56853)
Shuhei Kadowaki3d9852ae040e
diff --git a/crates/project/src/lsp_store.rs b/crates/project/src/lsp_store.rsindex f73ecfa81ef..811b9aebac6 100644--- a/crates/project/src/lsp_store.rs+++ b/crates/project/src/lsp_store.rs@@ -8437,26 +8437,21 @@ impl LspStore {}fn maintain_workspace_config(- external_refresh_requests: watch::Receiver<()>,+ mut external_refresh_requests: watch::Receiver<()>,cx: &mut Context<Self>,) -> Task<Result<()>> {- let (mut settings_changed_tx, mut settings_changed_rx) = watch::channel();- let _ = postage::stream::Stream::try_recv(&mut settings_changed_rx);-- let settings_observation = cx.observe_global::<SettingsStore>(move |_, _| {- *settings_changed_tx.borrow_mut() = ();- });-- let mut joint_future =- futures::stream::select(settings_changed_rx, external_refresh_requests);// Multiple things can happen when a workspace environment (selected toolchain + settings) change:// - We might shut down a language server if it's no longer enabled for a given language (and there are no buffers using it otherwise).// - We might also shut it down when the workspace configuration of all of the users of a given language server converges onto that of the other.// - In the same vein, we might also decide to start a new language server if the workspace configuration *diverges* from the other.// - In the easiest case (where we're not wrangling the lifetime of a language server anyhow), if none of the roots of a single language server diverge in their configuration,// but it is still different to what we had before, we're gonna send out a workspace configuration update.+ //+ // Settings-store changes reach this loop via `on_settings_changed` -> `request_workspace_config_refresh`,+ // which writes to `external_refresh_requests`. Observing `SettingsStore` here as well would cause every+ // settings change to drive the loop twice and emit duplicate `workspace/didChangeConfiguration` notifications.cx.spawn(async move |this, cx| {- while let Some(()) = joint_future.next().await {+ while let Some(()) = external_refresh_requests.next().await {this.update(cx, |this, cx| {this.refresh_server_tree(cx);})@@ -8465,7 +8460,6 @@ impl LspStore {Self::refresh_workspace_configurations(&this, cx).await;}- drop(settings_observation);anyhow::Ok(())})}diff --git a/crates/project/tests/integration/project_tests.rs b/crates/project/tests/integration/project_tests.rsindex 97f6b4b437b..daaaa0bd2c6 100644--- a/crates/project/tests/integration/project_tests.rs+++ b/crates/project/tests/integration/project_tests.rs@@ -3355,6 +3355,68 @@ async fn test_toggling_enable_language_server(cx: &mut gpui::TestAppContext) {.await;}+#[gpui::test]+async fn test_updating_lsp_settings_sends_one_did_change_configuration(+ cx: &mut gpui::TestAppContext,+) {+ init_test(cx);++ let fs = FakeFs::new(cx.executor());+ fs.insert_tree(path!("/dir"), json!({ "a.rs": "" })).await;++ let project = Project::test(fs, [path!("/dir").as_ref()], cx).await;+ let language_registry = project.read_with(cx, |project, _| project.languages().clone());++ let mut fake_rust_servers = language_registry.register_fake_lsp(+ "Rust",+ FakeLspAdapter {+ name: "rust-lsp",+ ..Default::default()+ },+ );+ language_registry.add(rust_lang());++ let _rs_buffer = project+ .update(cx, |project, cx| {+ project.open_local_buffer_with_lsp(path!("/dir/a.rs"), cx)+ })+ .await+ .unwrap();++ let fake_rust_server = fake_rust_servers.next().await.unwrap();++ let did_change_count = Arc::new(atomic::AtomicUsize::new(0));+ fake_rust_server.handle_notification::<lsp::notification::DidChangeConfiguration, _>({+ let did_change_count = did_change_count.clone();+ move |_, _| {+ did_change_count.fetch_add(1, atomic::Ordering::SeqCst);+ }+ });+ cx.executor().run_until_parked();+ did_change_count.store(0, atomic::Ordering::SeqCst);++ cx.update(|cx| {+ SettingsStore::update_global(cx, |settings, cx| {+ settings.update_user_settings(cx, |settings| {+ settings.project.lsp.0.insert(+ "rust-lsp".into(),+ settings::LspSettings {+ settings: Some(json!({ "foo": true })),+ ..Default::default()+ },+ );+ });+ })+ });+ cx.executor().run_until_parked();++ assert_eq!(+ did_change_count.load(atomic::Ordering::SeqCst),+ 1,+ "expected exactly one workspace/didChangeConfiguration after a settings change"+ );+}+#[gpui::test(iterations = 3)]async fn test_transforming_diagnostics(cx: &mut gpui::TestAppContext) {init_test(cx);