-
Notifications
You must be signed in to change notification settings - Fork 39.3k
Expand file tree
/
Copy pathparcelWatcher.ts
More file actions
818 lines (644 loc) · 27.5 KB
/
parcelWatcher.ts
File metadata and controls
818 lines (644 loc) · 27.5 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import parcelWatcher from '@parcel/watcher';
import { promises } from 'fs';
import { tmpdir, homedir } from 'os';
import { URI } from '../../../../../base/common/uri.js';
import { DeferredPromise, RunOnceScheduler, RunOnceWorker, ThrottledWorker } from '../../../../../base/common/async.js';
import { CancellationToken, CancellationTokenSource } from '../../../../../base/common/cancellation.js';
import { toErrorMessage } from '../../../../../base/common/errorMessage.js';
import { Emitter, Event } from '../../../../../base/common/event.js';
import { randomPath, isEqual, isEqualOrParent } from '../../../../../base/common/extpath.js';
import { GLOBSTAR, ParsedPattern, patternsEquals } from '../../../../../base/common/glob.js';
import { BaseWatcher } from '../baseWatcher.js';
import { TernarySearchTree } from '../../../../../base/common/ternarySearchTree.js';
import { normalizeNFC } from '../../../../../base/common/normalization.js';
import { normalize, join } from '../../../../../base/common/path.js';
import { isLinux, isMacintosh, isWindows } from '../../../../../base/common/platform.js';
import { Promises, realcase } from '../../../../../base/node/pfs.js';
import { FileChangeType, IFileChange } from '../../../common/files.js';
import { coalesceEvents, IRecursiveWatchRequest, parseWatcherPatterns, IRecursiveWatcherWithSubscribe, isFiltered, IWatcherErrorEvent } from '../../../common/watcher.js';
import { Disposable, DisposableStore, IDisposable, toDisposable } from '../../../../../base/common/lifecycle.js';
export class ParcelWatcherInstance extends Disposable {
private readonly _onDidStop = this._register(new Emitter<{ joinRestart?: Promise<void> }>());
readonly onDidStop = this._onDidStop.event;
private readonly _onDidFail = this._register(new Emitter<void>());
readonly onDidFail = this._onDidFail.event;
private didFail = false;
get failed(): boolean { return this.didFail; }
private didStop = false;
get stopped(): boolean { return this.didStop; }
private readonly includes: ParsedPattern[] | undefined;
private readonly excludes: ParsedPattern[] | undefined;
private readonly subscriptions = new Map<string, Set<(change: IFileChange) => void>>();
constructor(
/**
* Signals when the watcher is ready to watch.
*/
readonly ready: Promise<unknown>,
readonly request: IRecursiveWatchRequest,
/**
* How often this watcher has been restarted in case of an unexpected
* shutdown.
*/
readonly restarts: number,
/**
* The cancellation token associated with the lifecycle of the watcher.
*/
readonly token: CancellationToken,
/**
* An event aggregator to coalesce events and reduce duplicates.
*/
readonly worker: RunOnceWorker<IFileChange>,
private readonly stopFn: () => Promise<void>
) {
super();
const ignoreCase = !isLinux;
this.includes = this.request.includes ? parseWatcherPatterns(this.request.path, this.request.includes, ignoreCase) : undefined;
this.excludes = this.request.excludes ? parseWatcherPatterns(this.request.path, this.request.excludes, ignoreCase) : undefined;
this._register(toDisposable(() => this.subscriptions.clear()));
}
subscribe(path: string, callback: (change: IFileChange) => void): IDisposable {
path = URI.file(path).fsPath; // make sure to store the path in `fsPath` form to match it with events later
let subscriptions = this.subscriptions.get(path);
if (!subscriptions) {
subscriptions = new Set();
this.subscriptions.set(path, subscriptions);
}
subscriptions.add(callback);
return toDisposable(() => {
const subscriptions = this.subscriptions.get(path);
if (subscriptions) {
subscriptions.delete(callback);
if (subscriptions.size === 0) {
this.subscriptions.delete(path);
}
}
});
}
get subscriptionsCount(): number {
return this.subscriptions.size;
}
notifyFileChange(path: string, change: IFileChange): void {
const subscriptions = this.subscriptions.get(path);
if (subscriptions) {
for (const subscription of subscriptions) {
subscription(change);
}
}
}
notifyWatchFailed(): void {
this.didFail = true;
this._onDidFail.fire();
}
include(path: string): boolean {
if (!this.includes || this.includes.length === 0) {
return true; // no specific includes defined, include all
}
return this.includes.some(include => include(path));
}
exclude(path: string): boolean {
return Boolean(this.excludes?.some(exclude => exclude(path)));
}
async stop(joinRestart: Promise<void> | undefined): Promise<void> {
this.didStop = true;
try {
await this.stopFn();
} finally {
this._onDidStop.fire({ joinRestart });
this.dispose();
}
}
}
export class ParcelWatcher extends BaseWatcher implements IRecursiveWatcherWithSubscribe {
private static readonly MAP_PARCEL_WATCHER_ACTION_TO_FILE_CHANGE = new Map<parcelWatcher.EventType, number>(
[
['create', FileChangeType.ADDED],
['update', FileChangeType.UPDATED],
['delete', FileChangeType.DELETED]
]
);
private static readonly PREDEFINED_EXCLUDES: { [platform: string]: string[] } = {
'win32': [],
'darwin': [
join(homedir(), 'Library', 'Containers') // Triggers access dialog from macOS 14 (https://github.com/microsoft/vscode/issues/208105)
],
'linux': []
};
private static readonly PARCEL_WATCHER_BACKEND = isWindows ? 'windows' : isLinux ? 'inotify' : 'fs-events';
private readonly _onDidError = this._register(new Emitter<IWatcherErrorEvent>());
readonly onDidError = this._onDidError.event;
private readonly _watchers = new Map<string /* path */ | number /* correlation ID */, ParcelWatcherInstance>();
get watchers() { return this._watchers.values(); }
// A delay for collecting file changes from Parcel
// before collecting them for coalescing and emitting.
// Parcel internally uses 50ms as delay, so we use 75ms,
// to schedule sufficiently after Parcel.
//
// Note: since Parcel 2.0.7, the very first event is
// emitted without delay if no events occurred over a
// duration of 500ms. But we always want to aggregate
// events to apply our coleasing logic.
//
private static readonly FILE_CHANGES_HANDLER_DELAY = 75;
// Reduce likelyhood of spam from file events via throttling.
// (https://github.com/microsoft/vscode/issues/124723)
private readonly throttledFileChangesEmitter = this._register(new ThrottledWorker<IFileChange>(
{
maxWorkChunkSize: 500, // only process up to 500 changes at once before...
throttleDelay: 200, // ...resting for 200ms until we process events again...
maxBufferedWork: 30000 // ...but never buffering more than 30000 events in memory
},
events => this._onDidChangeFile.fire(events)
));
private enospcErrorLogged = false;
constructor() {
super();
this.registerListeners();
}
private registerListeners(): void {
const onUncaughtException = (error: unknown) => this.onUnexpectedError(error);
const onUnhandledRejection = (error: unknown) => this.onUnexpectedError(error);
process.on('uncaughtException', onUncaughtException);
process.on('unhandledRejection', onUnhandledRejection);
this._register(toDisposable(() => {
process.off('uncaughtException', onUncaughtException);
process.off('unhandledRejection', onUnhandledRejection);
}));
}
protected override async doWatch(requests: IRecursiveWatchRequest[]): Promise<void> {
// Figure out duplicates to remove from the requests
requests = await this.removeDuplicateRequests(requests);
// Figure out which watchers to start and which to stop
const requestsToStart: IRecursiveWatchRequest[] = [];
const watchersToStop = new Set(Array.from(this.watchers));
for (const request of requests) {
const watcher = this._watchers.get(this.requestToWatcherKey(request));
if (watcher && patternsEquals(watcher.request.excludes, request.excludes) && patternsEquals(watcher.request.includes, request.includes) && watcher.request.pollingInterval === request.pollingInterval) {
watchersToStop.delete(watcher); // keep watcher
} else {
requestsToStart.push(request); // start watching
}
}
// Logging
if (requestsToStart.length) {
this.trace(`Request to start watching: ${requestsToStart.map(request => this.requestToString(request)).join(',')}`);
}
if (watchersToStop.size) {
this.trace(`Request to stop watching: ${Array.from(watchersToStop).map(watcher => this.requestToString(watcher.request)).join(',')}`);
}
// Stop watching as instructed
for (const watcher of watchersToStop) {
await this.stopWatching(watcher);
}
// Start watching as instructed
for (const request of requestsToStart) {
if (request.pollingInterval) {
await this.startPolling(request, request.pollingInterval);
} else {
await this.startWatching(request);
}
}
}
private requestToWatcherKey(request: IRecursiveWatchRequest): string | number {
return typeof request.correlationId === 'number' ? request.correlationId : this.pathToWatcherKey(request.path);
}
private pathToWatcherKey(path: string): string {
return isLinux ? path : path.toLowerCase() /* ignore path casing */;
}
private async startPolling(request: IRecursiveWatchRequest, pollingInterval: number, restarts = 0): Promise<void> {
const cts = new CancellationTokenSource();
const instance = new DeferredPromise<void>();
const snapshotFile = randomPath(tmpdir(), 'vscode-watcher-snapshot');
// Remember as watcher instance
const watcher: ParcelWatcherInstance = new ParcelWatcherInstance(
instance.p,
request,
restarts,
cts.token,
new RunOnceWorker<IFileChange>(events => this.handleParcelEvents(events, watcher), ParcelWatcher.FILE_CHANGES_HANDLER_DELAY),
async () => {
cts.dispose(true);
watcher.worker.flush();
watcher.worker.dispose();
pollingWatcher.dispose();
await promises.unlink(snapshotFile);
}
);
this._watchers.set(this.requestToWatcherKey(request), watcher);
// Path checks for symbolic links / wrong casing
const { realPath, realPathDiffers, realPathLength } = await this.normalizePath(request);
this.trace(`Started watching: '${realPath}' with polling interval '${pollingInterval}'`);
let counter = 0;
const pollingWatcher = new RunOnceScheduler(async () => {
counter++;
if (cts.token.isCancellationRequested) {
return;
}
// We already ran before, check for events since
const parcelWatcherLib = parcelWatcher;
try {
if (counter > 1) {
const parcelEvents = await parcelWatcherLib.getEventsSince(realPath, snapshotFile, { ignore: this.addPredefinedExcludes(request.excludes), backend: ParcelWatcher.PARCEL_WATCHER_BACKEND });
if (cts.token.isCancellationRequested) {
return;
}
// Handle & emit events
this.onParcelEvents(parcelEvents, watcher, realPathDiffers, realPathLength);
}
// Store a snapshot of files to the snapshot file
await parcelWatcherLib.writeSnapshot(realPath, snapshotFile, { ignore: this.addPredefinedExcludes(request.excludes), backend: ParcelWatcher.PARCEL_WATCHER_BACKEND });
} catch (error) {
this.onUnexpectedError(error, request);
}
// Signal we are ready now when the first snapshot was written
if (counter === 1) {
instance.complete();
}
if (cts.token.isCancellationRequested) {
return;
}
// Schedule again at the next interval
pollingWatcher.schedule();
}, pollingInterval);
pollingWatcher.schedule(0);
}
private async startWatching(request: IRecursiveWatchRequest, restarts = 0): Promise<void> {
const cts = new CancellationTokenSource();
const instance = new DeferredPromise<parcelWatcher.AsyncSubscription | undefined>();
// Remember as watcher instance
const watcher: ParcelWatcherInstance = new ParcelWatcherInstance(
instance.p,
request,
restarts,
cts.token,
new RunOnceWorker<IFileChange>(events => this.handleParcelEvents(events, watcher), ParcelWatcher.FILE_CHANGES_HANDLER_DELAY),
async () => {
cts.dispose(true);
watcher.worker.flush();
watcher.worker.dispose();
const watcherInstance = await instance.p;
await watcherInstance?.unsubscribe();
}
);
this._watchers.set(this.requestToWatcherKey(request), watcher);
// Path checks for symbolic links / wrong casing
const { realPath, realPathDiffers, realPathLength } = await this.normalizePath(request);
try {
const parcelWatcherLib = parcelWatcher;
const parcelWatcherInstance = await parcelWatcherLib.subscribe(realPath, (error, parcelEvents) => {
if (watcher.token.isCancellationRequested) {
return; // return early when disposed
}
// In any case of an error, treat this like a unhandled exception
// that might require the watcher to restart. We do not really know
// the state of parcel at this point and as such will try to restart
// up to our maximum of restarts.
if (error) {
this.onUnexpectedError(error, request);
}
// Handle & emit events
this.onParcelEvents(parcelEvents, watcher, realPathDiffers, realPathLength);
}, {
backend: ParcelWatcher.PARCEL_WATCHER_BACKEND,
ignore: this.addPredefinedExcludes(watcher.request.excludes)
});
this.trace(`Started watching: '${realPath}' with backend '${ParcelWatcher.PARCEL_WATCHER_BACKEND}'`);
instance.complete(parcelWatcherInstance);
} catch (error) {
this.onUnexpectedError(error, request);
instance.complete(undefined);
watcher.notifyWatchFailed();
this._onDidWatchFail.fire(request);
}
}
private addPredefinedExcludes(initialExcludes: string[]): string[] {
const excludes = [...initialExcludes];
const predefinedExcludes = ParcelWatcher.PREDEFINED_EXCLUDES[process.platform];
if (Array.isArray(predefinedExcludes)) {
for (const exclude of predefinedExcludes) {
if (!excludes.includes(exclude)) {
excludes.push(exclude);
}
}
}
return excludes;
}
private onParcelEvents(parcelEvents: parcelWatcher.Event[], watcher: ParcelWatcherInstance, realPathDiffers: boolean, realPathLength: number): void {
if (parcelEvents.length === 0) {
return;
}
// Normalize events: handle NFC normalization and symlinks
// It is important to do this before checking for includes
// to check on the original path.
this.normalizeEvents(parcelEvents, watcher.request, realPathDiffers, realPathLength);
// Check for includes
const includedEvents = this.handleIncludes(watcher, parcelEvents);
// Add to event aggregator for later processing
for (const includedEvent of includedEvents) {
watcher.worker.work(includedEvent);
}
}
private handleIncludes(watcher: ParcelWatcherInstance, parcelEvents: parcelWatcher.Event[]): IFileChange[] {
const events: IFileChange[] = [];
for (const { path, type: parcelEventType } of parcelEvents) {
const type = ParcelWatcher.MAP_PARCEL_WATCHER_ACTION_TO_FILE_CHANGE.get(parcelEventType)!;
if (this.verboseLogging) {
this.traceWithCorrelation(`${type === FileChangeType.ADDED ? '[ADDED]' : type === FileChangeType.DELETED ? '[DELETED]' : '[CHANGED]'} ${path}`, watcher.request);
}
// Apply include filter if any
if (!watcher.include(path)) {
if (this.verboseLogging) {
this.traceWithCorrelation(` >> ignored (not included) ${path}`, watcher.request);
}
} else {
events.push({ type, resource: URI.file(path), cId: watcher.request.correlationId });
}
}
return events;
}
private handleParcelEvents(parcelEvents: IFileChange[], watcher: ParcelWatcherInstance): void {
// Coalesce events: merge events of same kind
const coalescedEvents = coalesceEvents(parcelEvents);
// Filter events: check for specific events we want to exclude
const { events: filteredEvents, rootDeleted } = this.filterEvents(coalescedEvents, watcher);
// Broadcast to clients
this.emitEvents(filteredEvents, watcher);
// Handle root path deletes
if (rootDeleted) {
this.onWatchedPathDeleted(watcher);
}
}
private emitEvents(events: IFileChange[], watcher: ParcelWatcherInstance): void {
if (events.length === 0) {
return;
}
// Broadcast to clients via throttler
const worked = this.throttledFileChangesEmitter.work(events);
// Logging
if (!worked) {
this.warn(`started ignoring events due to too many file change events at once (incoming: ${events.length}, most recent change: ${events[0].resource.fsPath}). Use 'files.watcherExclude' setting to exclude folders with lots of changing files (e.g. compilation output).`);
} else {
if (this.throttledFileChangesEmitter.pending > 0) {
this.trace(`started throttling events due to large amount of file change events at once (pending: ${this.throttledFileChangesEmitter.pending}, most recent change: ${events[0].resource.fsPath}). Use 'files.watcherExclude' setting to exclude folders with lots of changing files (e.g. compilation output).`, watcher);
}
}
}
private async normalizePath(request: IRecursiveWatchRequest): Promise<{ realPath: string; realPathDiffers: boolean; realPathLength: number }> {
let realPath = request.path;
let realPathDiffers = false;
let realPathLength = request.path.length;
try {
// First check for symbolic link
realPath = await Promises.realpath(request.path);
// Second check for casing difference
// Note: this will be a no-op on Linux platforms
if (request.path === realPath) {
realPath = await realcase(request.path) ?? request.path;
}
// Correct watch path as needed
if (request.path !== realPath) {
realPathLength = realPath.length;
realPathDiffers = true;
this.trace(`correcting a path to watch that seems to be a symbolic link or wrong casing (original: ${request.path}, real: ${realPath})`);
}
} catch (error) {
// ignore
}
return { realPath, realPathDiffers, realPathLength };
}
private normalizeEvents(events: parcelWatcher.Event[], request: IRecursiveWatchRequest, realPathDiffers: boolean, realPathLength: number): void {
for (const event of events) {
// Mac uses NFD unicode form on disk, but we want NFC
if (isMacintosh) {
event.path = normalizeNFC(event.path);
}
// Workaround for https://github.com/parcel-bundler/watcher/issues/68
// where watching root drive letter adds extra backslashes.
if (isWindows) {
if (request.path.length <= 3) { // for ex. c:, C:\
event.path = normalize(event.path);
}
}
// Convert paths back to original form in case it differs
if (realPathDiffers) {
event.path = request.path + event.path.substr(realPathLength);
}
}
}
private filterEvents(events: IFileChange[], watcher: ParcelWatcherInstance): { events: IFileChange[]; rootDeleted?: boolean } {
const filteredEvents: IFileChange[] = [];
let rootDeleted = false;
const filter = this.isCorrelated(watcher.request) ? watcher.request.filter : undefined; // filtering is only enabled when correlating because watchers are otherwise potentially reused
for (const event of events) {
// Emit to instance subscriptions if any before filtering
if (watcher.subscriptionsCount > 0) {
watcher.notifyFileChange(event.resource.fsPath, event);
}
// Filtering
rootDeleted = event.type === FileChangeType.DELETED && isEqual(event.resource.fsPath, watcher.request.path, !isLinux);
if (isFiltered(event, filter)) {
if (this.verboseLogging) {
this.traceWithCorrelation(` >> ignored (filtered) ${event.resource.fsPath}`, watcher.request);
}
continue;
}
// Logging
this.traceEvent(event, watcher.request);
filteredEvents.push(event);
}
return { events: filteredEvents, rootDeleted };
}
private onWatchedPathDeleted(watcher: ParcelWatcherInstance): void {
this.warn('Watcher shutdown because watched path got deleted', watcher);
watcher.notifyWatchFailed();
this._onDidWatchFail.fire(watcher.request);
}
private onUnexpectedError(error: unknown, request?: IRecursiveWatchRequest): void {
const msg = toErrorMessage(error);
// Specially handle ENOSPC errors that can happen when
// the watcher consumes so many file descriptors that
// we are running into a limit. We only want to warn
// once in this case to avoid log spam.
// See https://github.com/microsoft/vscode/issues/7950
if (msg.indexOf('No space left on device') !== -1) {
if (!this.enospcErrorLogged) {
this.error('Inotify limit reached (ENOSPC)', request);
this.enospcErrorLogged = true;
}
}
// Version 2.5.1 introduces 3 new errors on macOS
// via https://github.dev/parcel-bundler/watcher/pull/196
else if (msg.indexOf('File system must be re-scanned') !== -1) {
this.error(msg, request);
}
// Any other error is unexpected and we should try to
// restart the watcher as a result to get into healthy
// state again if possible and if not attempted too much
else {
this.error(`Unexpected error: ${msg} (EUNKNOWN)`, request);
this._onDidError.fire({ request, error: msg });
}
}
override async stop(): Promise<void> {
await super.stop();
for (const watcher of this.watchers) {
await this.stopWatching(watcher);
}
}
protected restartWatching(watcher: ParcelWatcherInstance, delay = 800): void {
// Restart watcher delayed to accommodate for
// changes on disk that have triggered the
// need for a restart in the first place.
const scheduler = new RunOnceScheduler(async () => {
if (watcher.token.isCancellationRequested) {
return; // return early when disposed
}
const restartPromise = new DeferredPromise<void>();
try {
// Await the watcher having stopped, as this is
// needed to properly re-watch the same path
await this.stopWatching(watcher, restartPromise.p);
// Start watcher again counting the restarts
if (watcher.request.pollingInterval) {
await this.startPolling(watcher.request, watcher.request.pollingInterval, watcher.restarts + 1);
} else {
await this.startWatching(watcher.request, watcher.restarts + 1);
}
} finally {
restartPromise.complete();
}
}, delay);
scheduler.schedule();
watcher.token.onCancellationRequested(() => scheduler.dispose());
}
private async stopWatching(watcher: ParcelWatcherInstance, joinRestart?: Promise<void>): Promise<void> {
this.trace(`stopping file watcher`, watcher);
this._watchers.delete(this.requestToWatcherKey(watcher.request));
try {
await watcher.stop(joinRestart);
} catch (error) {
this.error(`Unexpected error stopping watcher: ${toErrorMessage(error)}`, watcher.request);
}
}
protected async removeDuplicateRequests(requests: IRecursiveWatchRequest[], validatePaths = true): Promise<IRecursiveWatchRequest[]> {
// Sort requests by path length to have shortest first
// to have a way to prevent children to be watched if
// parents exist.
requests.sort((requestA, requestB) => requestA.path.length - requestB.path.length);
// Ignore requests for the same paths that have the same correlation
const mapCorrelationtoRequests = new Map<number | undefined /* correlation */, Map<string, IRecursiveWatchRequest>>();
for (const request of requests) {
if (request.excludes.includes(GLOBSTAR)) {
continue; // path is ignored entirely (via `**` glob exclude)
}
let requestsForCorrelation = mapCorrelationtoRequests.get(request.correlationId);
if (!requestsForCorrelation) {
requestsForCorrelation = new Map<string, IRecursiveWatchRequest>();
mapCorrelationtoRequests.set(request.correlationId, requestsForCorrelation);
}
const path = this.pathToWatcherKey(request.path);
if (requestsForCorrelation.has(path)) {
this.trace(`ignoring a request for watching who's path is already watched: ${this.requestToString(request)}`);
}
requestsForCorrelation.set(path, request);
}
const normalizedRequests: IRecursiveWatchRequest[] = [];
for (const requestsForCorrelation of mapCorrelationtoRequests.values()) {
// Only consider requests for watching that are not
// a child of an existing request path to prevent
// duplication. In addition, drop any request where
// everything is excluded (via `**` glob).
//
// However, allow explicit requests to watch folders
// that are symbolic links because the Parcel watcher
// does not allow to recursively watch symbolic links.
const requestTrie = TernarySearchTree.forPaths<IRecursiveWatchRequest>(!isLinux);
for (const request of requestsForCorrelation.values()) {
// Check for overlapping request paths (but preserve symbolic links)
if (requestTrie.findSubstr(request.path)) {
if (requestTrie.has(request.path)) {
this.trace(`ignoring a request for watching who's path is already watched: ${this.requestToString(request)}`);
} else {
try {
if (!(await promises.lstat(request.path)).isSymbolicLink()) {
this.trace(`ignoring a request for watching who's parent is already watched: ${this.requestToString(request)}`);
continue;
}
} catch (error) {
this.trace(`ignoring a request for watching who's lstat failed to resolve: ${this.requestToString(request)} (error: ${error})`);
this._onDidWatchFail.fire(request);
continue;
}
}
}
// Check for invalid paths
if (validatePaths && !(await this.isPathValid(request.path))) {
this._onDidWatchFail.fire(request);
continue;
}
requestTrie.set(request.path, request);
}
normalizedRequests.push(...Array.from(requestTrie).map(([, request]) => request));
}
return normalizedRequests;
}
private async isPathValid(path: string): Promise<boolean> {
try {
const stat = await promises.stat(path);
if (!stat.isDirectory()) {
this.trace(`ignoring a path for watching that is a file and not a folder: ${path}`);
return false;
}
} catch (error) {
this.trace(`ignoring a path for watching who's stat info failed to resolve: ${path} (error: ${error})`);
return false;
}
return true;
}
subscribe(path: string, callback: (error: true | null, change?: IFileChange) => void): IDisposable | undefined {
for (const watcher of this.watchers) {
if (watcher.failed) {
continue; // watcher has already failed
}
if (!isEqualOrParent(path, watcher.request.path, !isLinux)) {
continue; // watcher does not consider this path
}
if (
watcher.exclude(path) ||
!watcher.include(path)
) {
continue; // parcel instance does not consider this path
}
const disposables = new DisposableStore();
disposables.add(Event.once(watcher.onDidStop)(async e => {
await e.joinRestart; // if we are restarting, await that so that we can possibly reuse this watcher again
if (disposables.isDisposed) {
return;
}
callback(true /* error */);
}));
disposables.add(Event.once(watcher.onDidFail)(() => callback(true /* error */)));
disposables.add(watcher.subscribe(path, change => callback(null, change)));
return disposables;
}
return undefined;
}
protected trace(message: string, watcher?: ParcelWatcherInstance): void {
if (this.verboseLogging) {
this._onDidLogMessage.fire({ type: 'trace', message: this.toMessage(message, watcher?.request) });
}
}
protected warn(message: string, watcher?: ParcelWatcherInstance) {
this._onDidLogMessage.fire({ type: 'warn', message: this.toMessage(message, watcher?.request) });
}
private error(message: string, request?: IRecursiveWatchRequest) {
this._onDidLogMessage.fire({ type: 'error', message: this.toMessage(message, request) });
}
private toMessage(message: string, request?: IRecursiveWatchRequest): string {
return request ? `[File Watcher ('parcel')] ${message} (path: ${request.path})` : `[File Watcher ('parcel')] ${message}`;
}
protected get recursiveWatcher() { return this; }
}