Make sure `next_scan_complete` resolves

Antonio Scandurra and Nathan Sobo created

Sometimes the scan state could change so quickly that the consumer
wouldn't notice that it ever went to a scanning state before going back
to an idle state, hence never resolving.

Co-Authored-By: Nathan Sobo <nathan@zed.dev>

Change summary

zed/src/editor/buffer/mod.rs |  3 ++-
zed/src/workspace.rs         | 19 ++++++++-----------
zed/src/worktree.rs          | 28 ++++++++++++++--------------
3 files changed, 24 insertions(+), 26 deletions(-)

Detailed changes

zed/src/editor/buffer/mod.rs 🔗

@@ -3060,7 +3060,8 @@ mod tests {
 
             tree.flush_fs_events(&app).await;
             fs::remove_file(dir.path().join("file1")).unwrap();
-            app.read(|ctx| tree.read(ctx).next_scan_complete()).await;
+            tree.update(&mut app, |tree, ctx| tree.next_scan_complete(ctx))
+                .await;
 
             model.update(&mut app, |buffer, _| {
                 assert_eq!(*events.borrow(), &[Event::FileHandleChanged]);

zed/src/workspace.rs 🔗

@@ -732,7 +732,6 @@ mod tests {
     use gpui::App;
     use serde_json::json;
     use std::collections::HashSet;
-    use std::time;
     use tempdir::TempDir;
 
     #[test]
@@ -986,7 +985,7 @@ mod tests {
                 workspace.add_worktree(dir.path(), ctx);
                 workspace
             });
-            let worktree = app.read(|ctx| {
+            let tree = app.read(|ctx| {
                 workspace
                     .read(ctx)
                     .worktrees()
@@ -995,6 +994,7 @@ mod tests {
                     .unwrap()
                     .clone()
             });
+            tree.flush_fs_events(&app).await;
 
             // Create a new untitled buffer
             let editor = workspace.update(&mut app, |workspace, ctx| {
@@ -1027,15 +1027,12 @@ mod tests {
             });
 
             // When the save completes, the buffer's title is updated.
-            editor
-                .condition(&app, |editor, ctx| !editor.is_dirty(ctx))
-                .await;
-            worktree
-                .condition_with_duration(time::Duration::from_millis(500), &app, |worktree, _| {
-                    worktree.inode_for_path("the-new-name").is_some()
-                })
+            tree.update(&mut app, |tree, ctx| tree.next_scan_complete(ctx))
                 .await;
-            app.read(|ctx| assert_eq!(editor.title(ctx), "the-new-name"));
+            app.read(|ctx| {
+                assert!(!editor.is_dirty(ctx));
+                assert_eq!(editor.title(ctx), "the-new-name");
+            });
 
             // Edit the file and save it again. This time, there is no filename prompt.
             editor.update(&mut app, |editor, ctx| {
@@ -1057,7 +1054,7 @@ mod tests {
                 workspace.open_new_file(&(), ctx);
                 workspace.split_pane(workspace.active_pane().clone(), SplitDirection::Right, ctx);
                 assert!(workspace
-                    .open_entry((worktree.id(), Path::new("the-new-name").into()), ctx)
+                    .open_entry((tree.id(), Path::new("the-new-name").into()), ctx)
                     .is_none());
             });
             let editor2 = workspace.update(&mut app, |workspace, ctx| {

zed/src/worktree.rs 🔗

@@ -114,19 +114,17 @@ impl Worktree {
         }
     }
 
-    pub fn next_scan_complete(&self) -> impl Future<Output = ()> {
-        let mut scan_state_rx = self.scan_state.1.clone();
-        let mut did_scan = matches!(*scan_state_rx.borrow(), ScanState::Scanning);
-        async move {
-            loop {
-                if let ScanState::Scanning = *scan_state_rx.borrow() {
-                    did_scan = true;
-                } else if did_scan {
-                    break;
+    pub fn next_scan_complete(&self, ctx: &mut ModelContext<Self>) -> impl Future<Output = ()> {
+        let scan_id = self.snapshot.scan_id;
+        ctx.spawn_stream(
+            self.scan_state.1.clone(),
+            move |this, _, ctx| {
+                if this.snapshot.scan_id > scan_id {
+                    ctx.halt_stream();
                 }
-                scan_state_rx.recv().await;
-            }
-        }
+            },
+            |_, _| {},
+        )
     }
 
     fn observe_scan_state(&mut self, scan_state: ScanState, ctx: &mut ModelContext<Self>) {
@@ -1495,7 +1493,8 @@ mod tests {
             std::fs::remove_file(dir.path().join("b/c/file5")).unwrap();
             std::fs::rename(dir.path().join("b/c"), dir.path().join("d")).unwrap();
             std::fs::rename(dir.path().join("a/file2"), dir.path().join("a/file2.new")).unwrap();
-            app.read(|ctx| tree.read(ctx).next_scan_complete()).await;
+            tree.update(&mut app, |tree, ctx| tree.next_scan_complete(ctx))
+                .await;
 
             app.read(|ctx| {
                 assert_eq!(
@@ -1557,7 +1556,8 @@ mod tests {
 
             fs::write(dir.path().join("tracked-dir/tracked-file2"), "").unwrap();
             fs::write(dir.path().join("ignored-dir/ignored-file2"), "").unwrap();
-            app.read(|ctx| tree.read(ctx).next_scan_complete()).await;
+            tree.update(&mut app, |tree, ctx| tree.next_scan_complete(ctx))
+                .await;
             app.read(|ctx| {
                 let tree = tree.read(ctx);
                 let dot_git = tree.entry_for_path(".git").unwrap();