| ... | ... | @@ -23,10 +23,11 @@ export class WorkspaceSearchIndexCreateFailed extends Schema.TaggedErrorClass<Wo |
| 23 | 23 | { |
| 24 | 24 | cwd: Schema.String, |
| 25 | 25 | reason: Schema.String, |
| 26 | cause: Schema.optional(Schema.Defect()), |
| 26 | 27 | }, |
| 27 | 28 | ) { |
| 28 | 29 | override get message(): string { |
| 29 | | return `Failed to create the workspace search index for '${this.cwd}': ${this.reason}`; |
| 30 | return `Failed to create the workspace search index for '${this.cwd}'.`; |
| 30 | 31 | } |
| 31 | 32 | } |
| 32 | 33 | |
| ... | ... | @@ -46,11 +47,14 @@ export class WorkspaceSearchIndexSearchFailed extends Schema.TaggedErrorClass<Wo |
| 46 | 47 | "WorkspaceSearchIndexSearchFailed", |
| 47 | 48 | { |
| 48 | 49 | cwd: Schema.String, |
| 50 | queryLength: Schema.Number, |
| 51 | pageSize: Schema.Number, |
| 49 | 52 | reason: Schema.String, |
| 53 | cause: Schema.optional(Schema.Defect()), |
| 50 | 54 | }, |
| 51 | 55 | ) { |
| 52 | 56 | override get message(): string { |
| 53 | | return `Workspace search failed for '${this.cwd}': ${this.reason}`; |
| 57 | return `Workspace search failed for '${this.cwd}'.`; |
| 54 | 58 | } |
| 55 | 59 | } |
| 56 | 60 | |
| ... | ... | @@ -59,10 +63,11 @@ export class WorkspaceSearchIndexRefreshFailed extends Schema.TaggedErrorClass<W |
| 59 | 63 | { |
| 60 | 64 | cwd: Schema.String, |
| 61 | 65 | reason: Schema.String, |
| 66 | cause: Schema.optional(Schema.Defect()), |
| 62 | 67 | }, |
| 63 | 68 | ) { |
| 64 | 69 | override get message(): string { |
| 65 | | return `Failed to refresh the workspace search index for '${this.cwd}': ${this.reason}`; |
| 70 | return `Failed to refresh the workspace search index for '${this.cwd}'.`; |
| 66 | 71 | } |
| 67 | 72 | } |
| 68 | 73 | |
| ... | ... | @@ -153,23 +158,35 @@ function withDirectoryAncestors(entries: ReadonlyArray<ProjectEntry>): ProjectEn |
| 153 | 158 | } |
| 154 | 159 | |
| 155 | 160 | const createFinder = Effect.fn("WorkspaceSearchIndex.createFinder")(function* (cwd: string) { |
| 156 | | const result = FileFinder.create({ |
| 157 | | basePath: cwd, |
| 158 | | disableMmapCache: true, |
| 159 | | disableContentIndexing: true, |
| 160 | | aiMode: false, |
| 161 | | enableFsRootScanning: true, |
| 162 | | enableHomeDirScanning: true, |
| 161 | const result = yield* Effect.try({ |
| 162 | try: () => |
| 163 | FileFinder.create({ |
| 164 | basePath: cwd, |
| 165 | disableMmapCache: true, |
| 166 | disableContentIndexing: true, |
| 167 | aiMode: false, |
| 168 | enableFsRootScanning: true, |
| 169 | enableHomeDirScanning: true, |
| 170 | }), |
| 171 | catch: (cause) => |
| 172 | new WorkspaceSearchIndexCreateFailed({ |
| 173 | cwd, |
| 174 | reason: "FileFinder.create threw unexpectedly.", |
| 175 | cause, |
| 176 | }), |
| 163 | 177 | }); |
| 164 | 178 | if (result.ok) return result.value; |
| 165 | | return yield* new WorkspaceSearchIndexCreateFailed({ cwd, reason: result.error }); |
| 179 | return yield* new WorkspaceSearchIndexCreateFailed({ |
| 180 | cwd, |
| 181 | reason: result.error, |
| 182 | }); |
| 166 | 183 | }); |
| 167 | 184 | |
| 168 | | const waitForScan = Effect.fn("WorkspaceSearchIndex.waitForScan")(function* ( |
| 169 | | cwd: string, |
| 170 | | finder: FileFinder, |
| 171 | | ) { |
| 172 | | yield* Effect.sync(() => finder.isScanning()).pipe( |
| 185 | const waitForScan = <E>(cwd: string, finder: FileFinder, onFailure: (cause: unknown) => E) => |
| 186 | Effect.try({ |
| 187 | try: () => finder.isScanning(), |
| 188 | catch: onFailure, |
| 189 | }).pipe( |
| 173 | 190 | Effect.repeat({ |
| 174 | 191 | while: (scanning) => scanning, |
| 175 | 192 | schedule: Schedule.spaced(WORKSPACE_INDEX_SCAN_POLL_INTERVAL), |
| ... | ... | @@ -179,22 +196,46 @@ const waitForScan = Effect.fn("WorkspaceSearchIndex.waitForScan")(function* ( |
| 179 | 196 | orElse: () => |
| 180 | 197 | new WorkspaceSearchIndexScanTimedOut({ cwd, timeout: WORKSPACE_INDEX_SCAN_TIMEOUT }), |
| 181 | 198 | }), |
| 199 | Effect.withSpan("WorkspaceSearchIndex.waitForScan"), |
| 182 | 200 | ); |
| 183 | | }); |
| 184 | 201 | |
| 185 | 202 | export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (cwd: string) { |
| 186 | 203 | const finder = yield* Effect.acquireRelease(createFinder(cwd), (finder) => |
| 187 | 204 | Effect.sync(() => finder.destroy()), |
| 188 | 205 | ); |
| 189 | | yield* waitForScan(cwd, finder); |
| 206 | yield* waitForScan( |
| 207 | cwd, |
| 208 | finder, |
| 209 | (cause) => |
| 210 | new WorkspaceSearchIndexCreateFailed({ |
| 211 | cwd, |
| 212 | reason: "FileFinder.isScanning threw while creating the index.", |
| 213 | cause, |
| 214 | }), |
| 215 | ); |
| 190 | 216 | |
| 191 | 217 | const runMixedSearch = Effect.fn("WorkspaceSearchIndex.runMixedSearch")(function* ( |
| 192 | 218 | query: string, |
| 193 | 219 | pageSize: number, |
| 194 | 220 | ) { |
| 195 | | const result = yield* Effect.sync(() => finder.mixedSearch(query, { pageSize })); |
| 221 | const result = yield* Effect.try({ |
| 222 | try: () => finder.mixedSearch(query, { pageSize }), |
| 223 | catch: (cause) => |
| 224 | new WorkspaceSearchIndexSearchFailed({ |
| 225 | cwd, |
| 226 | queryLength: query.length, |
| 227 | pageSize, |
| 228 | reason: "FileFinder.mixedSearch threw unexpectedly.", |
| 229 | cause, |
| 230 | }), |
| 231 | }); |
| 196 | 232 | if (!result.ok) { |
| 197 | | return yield* new WorkspaceSearchIndexSearchFailed({ cwd, reason: result.error }); |
| 233 | return yield* new WorkspaceSearchIndexSearchFailed({ |
| 234 | cwd, |
| 235 | queryLength: query.length, |
| 236 | pageSize, |
| 237 | reason: result.error, |
| 238 | }); |
| 198 | 239 | } |
| 199 | 240 | return result.value; |
| 200 | 241 | }); |
| ... | ... | @@ -202,11 +243,31 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* (cwd: strin |
| 202 | 243 | const refresh: WorkspaceSearchIndex["Service"]["refresh"] = Effect.fn( |
| 203 | 244 | "WorkspaceSearchIndex.refresh", |
| 204 | 245 | )(function* () { |
| 205 | | const result = yield* Effect.sync(() => finder.scanFiles()); |
| 246 | const result = yield* Effect.try({ |
| 247 | try: () => finder.scanFiles(), |
| 248 | catch: (cause) => |
| 249 | new WorkspaceSearchIndexRefreshFailed({ |
| 250 | cwd, |
| 251 | reason: "FileFinder.scanFiles threw unexpectedly.", |
| 252 | cause, |
| 253 | }), |
| 254 | }); |
| 206 | 255 | if (!result.ok) { |
| 207 | | return yield* new WorkspaceSearchIndexRefreshFailed({ cwd, reason: result.error }); |
| 256 | return yield* new WorkspaceSearchIndexRefreshFailed({ |
| 257 | cwd, |
| 258 | reason: result.error, |
| 259 | }); |
| 208 | 260 | } |
| 209 | | yield* waitForScan(cwd, finder); |
| 261 | yield* waitForScan( |
| 262 | cwd, |
| 263 | finder, |
| 264 | (cause) => |
| 265 | new WorkspaceSearchIndexRefreshFailed({ |
| 266 | cwd, |
| 267 | reason: "FileFinder.isScanning threw while refreshing the index.", |
| 268 | cause, |
| 269 | }), |
| 270 | ); |
| 210 | 271 | }); |
| 211 | 272 | |
| 212 | 273 | const list: WorkspaceSearchIndex["Service"]["list"] = Effect.fn("WorkspaceSearchIndex.list")( |