summaryrefslogtreecommitdiff
path: root/gnu/packages/patches/zed-0.225.10-keep-regular-file-workspaces.patch
blob: 683ff922be0def648ee7e5425c9531217509cfe0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
From: Danny Milosavljevic <dannym@friendly-machines.com>
Date: Fri, 6 Mar 2026 20:20:00 +0000
Subject: [PATCH] workspace: Keep local workspaces whose roots are only regular files
License: expat

Zed persists local workspaces for standalone regular files as well as
directory-backed projects. Recent-workspace cleanup and last-session
restoration were using a helper that only kept local workspaces if all
saved paths existed and at least one of them was a directory.

That made local workspaces whose roots were only regular files look
invalid. A background recent-workspaces pass could then delete the
workspace row, which in turn cascaded to item-specific state such as
editors. In the window edited-state restore test, reopening a
standalone regular file could therefore fail because the saved editor
row had been deleted by that cleanup path.

Fix this by treating a local workspace as valid whenever all of its
saved paths still exist, whether those paths are directories or
regular files. Apply the same rule to last-session workspace
restoration, and add regression coverage for both code paths.

diff --git a/crates/workspace/src/persistence.rs b/crates/workspace/src/persistence.rs
--- a/crates/workspace/src/persistence.rs
+++ b/crates/workspace/src/persistence.rs
@@ -1783,19 +1783,13 @@ impl WorkspaceDb {
         }
     }
 
-    async fn all_paths_exist_with_a_directory(paths: &[PathBuf], fs: &dyn Fs) -> bool {
-        let mut any_dir = false;
+    async fn all_paths_exist(paths: &[PathBuf], fs: &dyn Fs) -> bool {
         for path in paths {
-            match fs.metadata(path).await.ok().flatten() {
-                None => return false,
-                Some(meta) => {
-                    if meta.is_dir {
-                        any_dir = true;
-                    }
-                }
+            if fs.metadata(path).await.ok().flatten().is_none() {
+                return false;
             }
         }
-        any_dir
+        true
     }
 
     // Returns the recent locations which are still valid on disk and deletes ones which no longer
@@ -1843,7 +1837,11 @@ impl WorkspaceDb {
             // If a local workspace points to WSL, this check will cause us to wait for the
             // WSL VM and file server to boot up. This can block for many seconds.
             // Supported scenarios use remote workspaces.
-            if !has_wsl_path && Self::all_paths_exist_with_a_directory(paths.paths(), fs).await {
+            //
+            // Local workspaces may legitimately contain only regular files (for example, a
+            // standalone file opened outside any worktree). Those should be preserved as
+            // long as all of their saved paths still exist.
+            if !has_wsl_path && Self::all_paths_exist(paths.paths(), fs).await {
                 result.push((id, SerializedWorkspaceLocation::Local, paths, timestamp));
             } else {
                 delete_tasks.push(self.delete_workspace_by_id(id));
@@ -1903,7 +1901,7 @@ impl WorkspaceDb {
                     window_id,
                 });
             } else {
-                if Self::all_paths_exist_with_a_directory(paths.paths(), fs).await {
+                if Self::all_paths_exist(paths.paths(), fs).await {
                     workspaces.push(SessionWorkspace {
                         workspace_id,
                         location: SerializedWorkspaceLocation::Local,
@@ -3425,6 +3423,91 @@ mod tests {
         );
     }
 
+    #[gpui::test]
+    async fn test_recent_workspaces_on_disk_keeps_regular_file_only_workspace(
+        cx: &mut gpui::TestAppContext,
+    ) {
+        let dir = tempfile::TempDir::with_prefix("regular-file-workspace").unwrap();
+        let regular_file = dir.path().join("a");
+
+        let fs = fs::FakeFs::new(cx.executor());
+        fs.insert_tree(dir.path(), json!({"a": "hey"})).await;
+
+        let db = WorkspaceDb::open_test_db("test_recent_workspaces_keeps_regular_file_only")
+            .await;
+        let workspace = SerializedWorkspace {
+            id: WorkspaceId(1),
+            paths: PathList::new(&[regular_file.clone()]),
+            location: SerializedWorkspaceLocation::Local,
+            center_group: Default::default(),
+            window_bounds: Default::default(),
+            display: Default::default(),
+            docks: Default::default(),
+            centered_layout: false,
+            breakpoints: Default::default(),
+            session_id: None,
+            window_id: Some(1),
+            user_toolchains: Default::default(),
+        };
+
+        db.save_workspace(workspace.clone()).await;
+
+        let recent = db.recent_workspaces_on_disk(fs.as_ref()).await.unwrap();
+        assert_eq!(
+            recent,
+            vec![(
+                workspace.id,
+                SerializedWorkspaceLocation::Local,
+                workspace.paths.clone(),
+                recent[0].3,
+            )]
+        );
+        assert_eq!(db.workspace_for_roots(&[regular_file]).unwrap().id, workspace.id);
+    }
+
+    #[gpui::test]
+    async fn test_last_session_workspace_locations_keeps_regular_file_only_workspace(
+        cx: &mut gpui::TestAppContext,
+    ) {
+        let dir = tempfile::TempDir::with_prefix("regular-file-workspace-session").unwrap();
+        let regular_file = dir.path().join("a");
+
+        let fs = fs::FakeFs::new(cx.executor());
+        fs.insert_tree(dir.path(), json!({"a": "hey"})).await;
+
+        let db = WorkspaceDb::open_test_db("test_last_session_keeps_regular_file_only").await;
+        let workspace = SerializedWorkspace {
+            id: WorkspaceId(1),
+            paths: PathList::new(&[regular_file.clone()]),
+            location: SerializedWorkspaceLocation::Local,
+            center_group: Default::default(),
+            window_bounds: Default::default(),
+            display: Default::default(),
+            docks: Default::default(),
+            centered_layout: false,
+            breakpoints: Default::default(),
+            session_id: Some("one-session".to_owned()),
+            window_id: Some(7),
+            user_toolchains: Default::default(),
+        };
+
+        db.save_workspace(workspace.clone()).await;
+
+        let locations = db
+            .last_session_workspace_locations("one-session", None, fs.as_ref())
+            .await
+            .unwrap();
+        assert_eq!(
+            locations,
+            vec![SessionWorkspace {
+                workspace_id: workspace.id,
+                location: SerializedWorkspaceLocation::Local,
+                paths: workspace.paths,
+                window_id: Some(WindowId::from(7u64)),
+            }]
+        );
+    }
+
     #[gpui::test]
     async fn test_get_or_create_ssh_project() {
         let db = WorkspaceDb::open_test_db("test_get_or_create_ssh_project").await;