Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

project: Don't send a project update when no worktree was removed (#65189)

CoinMan.dev04470cd9b504
diff --git a/crates/project/src/worktree_store.rs b/crates/project/src/worktree_store.rs
index 82220ea5acf..db59691dcb9 100644
--- a/crates/project/src/worktree_store.rs
+++ b/crates/project/src/worktree_store.rs
@@ -1020,6 +1020,7 @@ impl WorktreeStore {
}
pub fn remove_worktree(&mut self, id_to_remove: WorktreeId, cx: &mut Context<Self>) {
+ let mut removed = false;
self.worktrees.retain(|worktree| {
if let Some(worktree) = worktree.upgrade() {
if worktree.read(cx).id() == id_to_remove {
@@ -1027,6 +1028,7 @@ impl WorktreeStore {
worktree.entity_id(),
id_to_remove,
));
+ removed = true;
false
} else {
true
@@ -1035,6 +1037,14 @@ impl WorktreeStore {
false
}
});
+ // A remote client asks to remove a worktree whenever it releases a
+ // handle to it, which can happen again after the worktree is gone.
+ // Announcing the unchanged worktrees in response would make the
+ // client re-create and release handles for the ones it has dropped
+ // in the meantime, and send us even more removal requests.
+ if !removed {
+ return;
+ }
self.update_initial_scan_state(cx);
self.send_project_updates(cx);
}
diff --git a/crates/remote_server/src/remote_editing_tests.rs b/crates/remote_server/src/remote_editing_tests.rs
index 290b342aa62..20b24670409 100644
--- a/crates/remote_server/src/remote_editing_tests.rs
+++ b/crates/remote_server/src/remote_editing_tests.rs
@@ -54,6 +54,7 @@ use project::{
GlobalLogStore, LanguageServerKind, LanguageServerLogKey, LogKind, LogStore,
},
search::{SearchQuery, SearchResult},
+ worktree_store::WorktreeStoreEvent,
};
use remote::{ConnectionState, RemoteClient, RemoteClientEvent};
#[cfg(not(windows))]
@@ -3392,6 +3393,90 @@ async fn test_adding_then_removing_then_adding_worktrees(
})
}
+#[gpui::test]
+async fn test_removing_missing_worktree_does_not_send_project_update(
+ cx: &mut TestAppContext,
+ server_cx: &mut TestAppContext,
+) {
+ let fs = FakeFs::new(server_cx.executor());
+ fs.insert_tree(
+ path!("/code"),
+ json!({
+ "project1": {
+ "README.md": "# project 1",
+ },
+ "outside.txt": "not part of the project",
+ }),
+ )
+ .await;
+
+ let (project, headless) = init_test(&fs, cx, server_cx).await;
+ project
+ .update(cx, |project, cx| {
+ project.find_or_create_worktree(path!("/code/project1"), true, cx)
+ })
+ .await
+ .unwrap();
+ let (outside_worktree, _) = project
+ .update(cx, |project, cx| {
+ project.find_or_create_worktree(path!("/code/outside.txt"), false, cx)
+ })
+ .await
+ .unwrap();
+ let outside_worktree_id = outside_worktree.read_with(cx, |worktree, _| worktree.id());
+ cx.run_until_parked();
+
+ let server_worktree_store =
+ headless.read_with(server_cx, |headless, _| headless.worktree_store.clone());
+ server_worktree_store.read_with(server_cx, |worktree_store, cx| {
+ assert!(
+ worktree_store
+ .worktree_for_id(outside_worktree_id, cx)
+ .is_some()
+ );
+ });
+
+ // Releasing the client's handle removes the worktree on the server.
+ cx.update(|_| drop(outside_worktree));
+ cx.run_until_parked();
+ server_worktree_store.read_with(server_cx, |worktree_store, cx| {
+ assert!(
+ worktree_store
+ .worktree_for_id(outside_worktree_id, cx)
+ .is_none()
+ );
+ });
+
+ let project_updates_sent = Arc::new(AtomicUsize::new(0));
+ let _subscription = server_cx.update(|cx| {
+ let project_updates_sent = project_updates_sent.clone();
+ cx.subscribe(&server_worktree_store, move |_, event, _| {
+ if matches!(event, WorktreeStoreEvent::WorktreeUpdateSent(_)) {
+ project_updates_sent.fetch_add(1, Ordering::SeqCst);
+ }
+ })
+ });
+
+ // The client asks for the same removal again whenever it releases a handle
+ // that it re-created from a project update sent before the removal.
+ let proto_client = project.read_with(cx, |project, cx| {
+ project.remote_client().unwrap().read(cx).proto_client()
+ });
+ proto_client
+ .request(proto::RemoveWorktree {
+ worktree_id: outside_worktree_id.to_proto(),
+ })
+ .await
+ .unwrap();
+ cx.run_until_parked();
+
+ assert_eq!(
+ project_updates_sent.load(Ordering::SeqCst),
+ 0,
+ "removing a worktree that is already gone must not announce the unchanged worktrees again"
+ );
+}
+
#[gpui::test]
async fn test_open_server_settings(cx: &mut TestAppContext, server_cx: &mut TestAppContext) {
let fs = FakeFs::new(server_cx.executor());