From d30603da9ac2f1d0a916b4e1bc26d93d0996c52f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E5=A3=B0?= Date: Wed, 3 Jul 2024 16:29:50 +0800 Subject: [PATCH 1/2] fix: will watch same folder multiple times --- .../node/recursive/file-service-watcher.ts | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/file-service/src/node/recursive/file-service-watcher.ts b/packages/file-service/src/node/recursive/file-service-watcher.ts index 0200c854c2..29ea60547d 100644 --- a/packages/file-service/src/node/recursive/file-service-watcher.ts +++ b/packages/file-service/src/node/recursive/file-service-watcher.ts @@ -28,6 +28,14 @@ export interface WatcherOptions { excludes: string[]; } +const watcherPlaceHolder = { + path: '', + disposable: { + dispose: () => {}, + }, + handlers: [], +}; + /** * @deprecated */ @@ -88,7 +96,6 @@ export class FileSystemWatcherServer implements IFileSystemWatcherServer { */ async watchFileChanges(uri: string, options?: WatchOptions): Promise { const basePath = FileUri.fsPath(uri); - const exist = await fs.pathExists(basePath); let watcherId = this.checkIsAlreadyWatched(basePath); if (watcherId) { @@ -96,10 +103,14 @@ export class FileSystemWatcherServer implements IFileSystemWatcherServer { } watcherId = FileSystemWatcherServer.WATCHER_SEQUENCE++; + this.WATCHER_HANDLERS.set(watcherId, watcherPlaceHolder); + const toDisposeWatcher = new DisposableCollection(); - let watchPath; + let watchPath: string; + + const exist = await fs.pathExists(basePath); if (exist) { - const stat = await fs.lstatSync(basePath); + const stat = await fs.lstat(basePath); if (stat && stat.isDirectory()) { watchPath = basePath; } else { @@ -116,14 +127,16 @@ export class FileSystemWatcherServer implements IFileSystemWatcherServer { } events = this.trimChangeEvent(events); for (const event of events) { - if (event.type === 'create') { - this.pushAdded(event.path); - } - if (event.type === 'delete') { - this.pushDeleted(event.path); - } - if (event.type === 'update') { - this.pushUpdated(event.path); + switch (event.type) { + case 'create': + this.pushAdded(event.path); + break; + case 'delete': + this.pushDeleted(event.path); + break; + case 'update': + this.pushUpdated(event.path); + break; } } }; From 7bb07c5c5dec27b6906f5b9f45fe1d8b7b9baebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E5=A3=B0?= Date: Wed, 3 Jul 2024 16:32:31 +0800 Subject: [PATCH 2/2] fix: cannot find watcher --- .../file-service/src/node/recursive/file-service-watcher.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/file-service/src/node/recursive/file-service-watcher.ts b/packages/file-service/src/node/recursive/file-service-watcher.ts index 29ea60547d..31f823ed81 100644 --- a/packages/file-service/src/node/recursive/file-service-watcher.ts +++ b/packages/file-service/src/node/recursive/file-service-watcher.ts @@ -29,7 +29,6 @@ export interface WatcherOptions { } const watcherPlaceHolder = { - path: '', disposable: { dispose: () => {}, }, @@ -103,7 +102,10 @@ export class FileSystemWatcherServer implements IFileSystemWatcherServer { } watcherId = FileSystemWatcherServer.WATCHER_SEQUENCE++; - this.WATCHER_HANDLERS.set(watcherId, watcherPlaceHolder); + this.WATCHER_HANDLERS.set(watcherId, { + ...watcherPlaceHolder, + path: basePath, + }); const toDisposeWatcher = new DisposableCollection(); let watchPath: string;