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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Event } from 'vs/base/common/event';
import { GLOBSTAR, IRelativePattern, parse, ParsedPattern } from 'vs/base/common/glob';
import { Disposable, DisposableStore, MutableDisposable } from 'vs/base/common/lifecycle';
import { isAbsolute } from 'vs/base/common/path';
import { isLinux } from 'vs/base/common/platform';
import { URI as uri } from 'vs/base/common/uri';
import { FileChangeType, IFileChange, isParent } from 'vs/platform/files/common/files';
interface IWatchRequest {
/**
* The path to watch.
*/
path: string;
/**
* Whether to watch recursively or not.
*/
recursive: boolean;
/**
* A set of glob patterns or paths to exclude from watching.
*
* Paths or basic glob patterns that are relative will be
* resolved to an absolute path using the currently opened
* workspace. Complex glob patterns must match on absolute
* paths via leading or trailing `**`.
*/
excludes: string[];
/**
* An optional set of glob patterns or paths to include for
* watching. If not provided, all paths are considered for
* events.
*
* Paths or basic glob patterns that are relative will be
* resolved to an absolute path using the currently opened
* workspace. Complex glob patterns must match on absolute
* paths via leading or trailing `**`.
*/
includes?: Array<string | IRelativePattern>;
}
export interface INonRecursiveWatchRequest extends IWatchRequest {
/**
* The watcher will be non-recursive.
*/
recursive: false;
}
export interface IRecursiveWatchRequest extends IWatchRequest {
/**
* The watcher will be recursive.
*/
recursive: true;
/**
* @deprecated this only exists for WSL1 support and should never
* be used in any other case.
*/
pollingInterval?: number;
}
export function isRecursiveWatchRequest(request: IWatchRequest): request is IRecursiveWatchRequest {
return request.recursive === true;
}
export type IUniversalWatchRequest = IRecursiveWatchRequest | INonRecursiveWatchRequest;
interface IWatcher {
/**
* A normalized file change event from the raw events
* the watcher emits.
*/
readonly onDidChangeFile: Event<IDiskFileChange[]>;
/**
* An event to indicate a message that should get logged.
*/
readonly onDidLogMessage: Event<ILogMessage>;
/**
* An event to indicate an error occurred from the watcher
* that is unrecoverable. Listeners should restart the
* watcher if possible.
*/
readonly onDidError: Event<string>;
/**
* Configures the watcher to watch according to the
* requests. Any existing watched path that is not
* in the array, will be removed from watching and
* any new path will be added to watching.
*/
watch(requests: IWatchRequest[]): Promise<void>;
/**
* Enable verbose logging in the watcher.
*/
setVerboseLogging(enabled: boolean): Promise<void>;
/**
* Stop all watchers.
*/
stop(): Promise<void>;
}
export interface IRecursiveWatcher extends IWatcher {
watch(requests: IRecursiveWatchRequest[]): Promise<void>;
}
export interface IRecursiveWatcherOptions {
/**
* If `true`, will enable polling for all watchers, otherwise
* will enable it for paths included in the string array.
*
* @deprecated this only exists for WSL1 support and should never
* be used in any other case.
*/
usePolling: boolean | string[];
/**
* If polling is enabled (via `usePolling`), defines the duration
* in which the watcher will poll for changes.
*
* @deprecated this only exists for WSL1 support and should never
* be used in any other case.
*/
pollingInterval?: number;
}
export interface INonRecursiveWatcher extends IWatcher {
watch(requests: INonRecursiveWatchRequest[]): Promise<void>;
}
export interface IUniversalWatcher extends IWatcher {
watch(requests: IUniversalWatchRequest[]): Promise<void>;
}
export abstract class AbstractWatcherClient extends Disposable {
private static readonly MAX_RESTARTS = 5;
private watcher: IWatcher | undefined;
private readonly watcherDisposables = this._register(new MutableDisposable());
private requests: IWatchRequest[] | undefined = undefined;
private restartCounter = 0;
constructor(
private readonly onFileChanges: (changes: IDiskFileChange[]) => void,
private readonly onLogMessage: (msg: ILogMessage) => void,
private verboseLogging: boolean,
private options: {
type: string;
restartOnError: boolean;
}
) {
super();
}
protected abstract createWatcher(disposables: DisposableStore): IWatcher;
protected init(): void {
// Associate disposables to the watcher
const disposables = new DisposableStore();
this.watcherDisposables.value = disposables;
// Ask implementors to create the watcher
this.watcher = this.createWatcher(disposables);
this.watcher.setVerboseLogging(this.verboseLogging);
// Wire in event handlers
disposables.add(this.watcher.onDidChangeFile(changes => this.onFileChanges(changes)));
disposables.add(this.watcher.onDidLogMessage(msg => this.onLogMessage(msg)));
disposables.add(this.watcher.onDidError(error => this.onError(error)));
}
protected onError(error: string): void {
// Restart on error (up to N times, if enabled)
if (this.options.restartOnError) {
if (this.restartCounter < AbstractWatcherClient.MAX_RESTARTS && this.requests) {
this.error(`restarting watcher after error: ${error}`);
this.restart(this.requests);
} else {
this.error(`gave up attempting to restart watcher after error: ${error}`);
}
}
// Do not attempt to restart if not enabled
else {
this.error(error);
}
}
private restart(requests: IUniversalWatchRequest[]): void {
this.restartCounter++;
this.init();
this.watch(requests);
}
async watch(requests: IUniversalWatchRequest[]): Promise<void> {
this.requests = requests;
await this.watcher?.watch(requests);
}
async setVerboseLogging(verboseLogging: boolean): Promise<void> {
this.verboseLogging = verboseLogging;
await this.watcher?.setVerboseLogging(verboseLogging);
}
private error(message: string) {
this.onLogMessage({ type: 'error', message: `[File Watcher (${this.options.type})] ${message}` });
}
override dispose(): void {
// Render the watcher invalid from here
this.watcher = undefined;
return super.dispose();
}
}
export abstract class AbstractNonRecursiveWatcherClient extends AbstractWatcherClient {
constructor(
onFileChanges: (changes: IDiskFileChange[]) => void,
onLogMessage: (msg: ILogMessage) => void,
verboseLogging: boolean
) {
super(onFileChanges, onLogMessage, verboseLogging, { type: 'node.js', restartOnError: false });
}
protected abstract override createWatcher(disposables: DisposableStore): INonRecursiveWatcher;
}
export abstract class AbstractUniversalWatcherClient extends AbstractWatcherClient {
constructor(
onFileChanges: (changes: IDiskFileChange[]) => void,
onLogMessage: (msg: ILogMessage) => void,
verboseLogging: boolean
) {
super(onFileChanges, onLogMessage, verboseLogging, { type: 'universal', restartOnError: true });
}
protected abstract override createWatcher(disposables: DisposableStore): IUniversalWatcher;
}
export interface IDiskFileChange {
type: FileChangeType;
path: string;
}
export interface ILogMessage {
type: 'trace' | 'warn' | 'error' | 'info' | 'debug';
message: string;
}
export function toFileChanges(changes: IDiskFileChange[]): IFileChange[] {
return changes.map(change => ({
type: change.type,
resource: uri.file(change.path)
}));
}
export function coalesceEvents(changes: IDiskFileChange[]): IDiskFileChange[] {
// Build deltas
const coalescer = new EventCoalescer();
for (const event of changes) {
coalescer.processEvent(event);
}
return coalescer.coalesce();
}
export function parseWatcherPatterns(path: string, patterns: Array<string | IRelativePattern>): ParsedPattern[] {
const parsedPatterns: ParsedPattern[] = [];
for (const pattern of patterns) {
let normalizedPattern = pattern;
// Patterns are always matched on the full absolute path
// of the event. As such, if the pattern is not absolute
// and does not start with a leading `**`, we have to
// convert it to a relative pattern with the given `base`
if (typeof normalizedPattern === 'string' && !normalizedPattern.startsWith(GLOBSTAR) && !isAbsolute(normalizedPattern)) {
normalizedPattern = { base: path, pattern: normalizedPattern };
}
parsedPatterns.push(parse(normalizedPattern));
}
return parsedPatterns;
}
class EventCoalescer {
private readonly coalesced = new Set<IDiskFileChange>();
private readonly mapPathToChange = new Map<string, IDiskFileChange>();
private toKey(event: IDiskFileChange): string {
if (isLinux) {
return event.path;
}
return event.path.toLowerCase(); // normalise to file system case sensitivity
}
processEvent(event: IDiskFileChange): void {
const existingEvent = this.mapPathToChange.get(this.toKey(event));
let keepEvent = false;
// Event path already exists
if (existingEvent) {
const currentChangeType = existingEvent.type;
const newChangeType = event.type;
// macOS/Windows: track renames to different case
// by keeping both CREATE and DELETE events
if (existingEvent.path !== event.path && (event.type === FileChangeType.DELETED || event.type === FileChangeType.ADDED)) {
keepEvent = true;
}
// Ignore CREATE followed by DELETE in one go
else if (currentChangeType === FileChangeType.ADDED && newChangeType === FileChangeType.DELETED) {
this.mapPathToChange.delete(this.toKey(event));
this.coalesced.delete(existingEvent);
}
// Flatten DELETE followed by CREATE into CHANGE
else if (currentChangeType === FileChangeType.DELETED && newChangeType === FileChangeType.ADDED) {
existingEvent.type = FileChangeType.UPDATED;
}
// Do nothing. Keep the created event
else if (currentChangeType === FileChangeType.ADDED && newChangeType === FileChangeType.UPDATED) { }
// Otherwise apply change type
else {
existingEvent.type = newChangeType;
}
}
// Otherwise keep
else {
keepEvent = true;
}
if (keepEvent) {
this.coalesced.add(event);
this.mapPathToChange.set(this.toKey(event), event);
}
}
coalesce(): IDiskFileChange[] {
const addOrChangeEvents: IDiskFileChange[] = [];
const deletedPaths: string[] = [];
// This algorithm will remove all DELETE events up to the root folder
// that got deleted if any. This ensures that we are not producing
// DELETE events for each file inside a folder that gets deleted.
//
// 1.) split ADD/CHANGE and DELETED events
// 2.) sort short deleted paths to the top
// 3.) for each DELETE, check if there is a deleted parent and ignore the event in that case
return Array.from(this.coalesced).filter(e => {
if (e.type !== FileChangeType.DELETED) {
addOrChangeEvents.push(e);
return false; // remove ADD / CHANGE
}
return true; // keep DELETE
}).sort((e1, e2) => {
return e1.path.length - e2.path.length; // shortest path first
}).filter(e => {
if (deletedPaths.some(deletedPath => isParent(e.path, deletedPath, !isLinux /* ignorecase */))) {
return false; // DELETE is ignored if parent is deleted already
}
// otherwise mark as deleted
deletedPaths.push(e.path);
return true;
}).concat(addOrChangeEvents);
}
}
|