Skip to content

Commit e905d4b

Browse files
authored
fix(runner): replace chokidar with native fs.watch in the test watcher (#43716)
1 parent 62d25ac commit e905d4b

4 files changed

Lines changed: 185 additions & 43 deletions

File tree

‎packages/playwright/src/runner/DEPS.list‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
../plugins/
88
../util.ts
99
../isomorphic/
10-
node_modules/chokidar
1110
node_modules/colors/safe
1211
node_modules/debug
1312
node_modules/diff

‎packages/playwright/src/runner/fsWatcher.ts‎

Lines changed: 148 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,67 +14,176 @@
1414
* limitations under the License.
1515
*/
1616

17-
import chokidar from 'chokidar';
18-
import type { FSWatcher as ChokidarWatcher } from 'chokidar';
17+
import fs from 'fs';
18+
import path from 'path';
1919

20-
export type FSEvent = { event: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir', file: string };
20+
export type FSWatcherParams = {
21+
// Directories are watched recursively, files individually.
22+
paths: string[];
23+
// Never reported, never entered.
24+
ignoredFolders: string[];
25+
};
26+
27+
// Node.js watches recursively with FSEvents on macOS and ReadDirectoryChangesW on Windows,
28+
// a single handle per root. On other platforms it falls back to a JavaScript implementation
29+
// that watches every single file and does not skip node_modules, so we walk the tree ourselves
30+
// and watch each directory instead. Node 26 adds an `ignore` option to that fallback, so once
31+
// it is the minimum supported version, the walker can go.
32+
const kNativeRecursiveWatch = process.platform === 'darwin' || process.platform === 'win32';
2133

2234
export class FSWatcher {
23-
private _onChange: (events: FSEvent[]) => void;
24-
private _watchedPaths: string[] = [];
25-
private _ignoredFolders: string[] = [];
26-
private _collector: FSEvent[] = [];
27-
private _fsWatcher: ChokidarWatcher | undefined;
35+
private _onChange: (files: string[]) => void;
36+
private _params: FSWatcherParams = { paths: [], ignoredFolders: [] };
37+
private _watchedFiles = new Set<string>();
38+
private _watchers = new Map<string, fs.FSWatcher>();
39+
private _collector = new Set<string>();
2840
private _throttleTimer: NodeJS.Timeout | undefined;
2941

30-
constructor(onChange: (events: FSEvent[]) => void) {
42+
constructor(onChange: (files: string[]) => void) {
3143
this._onChange = onChange;
3244
}
3345

34-
async update(watchedPaths: string[], ignoredFolders: string[], reportPending: boolean) {
35-
if (JSON.stringify([this._watchedPaths, this._ignoredFolders]) === JSON.stringify([watchedPaths, ignoredFolders]))
46+
async update(params: FSWatcherParams, reportPending: boolean) {
47+
if (JSON.stringify(this._params) === JSON.stringify(params))
3648
return;
3749

3850
if (reportPending)
3951
this._reportEventsIfAny();
4052

41-
this._watchedPaths = watchedPaths;
42-
this._ignoredFolders = ignoredFolders;
43-
void this._fsWatcher?.close();
44-
this._fsWatcher = undefined;
45-
this._collector.length = 0;
53+
this._params = params;
54+
this._closeAll();
55+
this._collector.clear();
4656
clearTimeout(this._throttleTimer);
4757
this._throttleTimer = undefined;
4858

49-
if (!this._watchedPaths.length)
50-
return;
59+
// The test dir may come with a trailing separator.
60+
const paths = params.paths.map(p => p.length > 1 && p.endsWith(path.sep) ? p.slice(0, -1) : p);
61+
const dirs = new Set(paths.filter(p => isDirectory(p)));
62+
// Anything inside a watched directory is covered by its recursive watch.
63+
const isCovered = (p: string) => [...dirs].some(dir => isInside(dir, p));
64+
65+
for (const dir of dirs) {
66+
if (isCovered(dir))
67+
continue;
68+
if (kNativeRecursiveWatch)
69+
this._watch(dir, true, file => this._onDirEvent(file));
70+
else
71+
this._watchTree(dir);
72+
}
5173

52-
// Chokidar calls the matcher with paths normalized to forward slashes.
53-
const ignoredPrefixes = this._ignoredFolders.map(folder => folder.replace(/\\/g, '/') + '/');
54-
const ignored = (file: string) => {
55-
if (file.split('/').includes('node_modules'))
56-
return true;
57-
return ignoredPrefixes.some(prefix => (file + '/').startsWith(prefix));
58-
};
59-
this._fsWatcher = chokidar.watch(watchedPaths, { ignoreInitial: true, ignored }).on('all', async (event, file) => {
60-
if (event !== 'add' && event !== 'addDir' && event !== 'change' && event !== 'unlink' && event !== 'unlinkDir')
61-
return;
62-
if (this._throttleTimer)
63-
clearTimeout(this._throttleTimer);
64-
this._collector.push({ event, file });
65-
this._throttleTimer = setTimeout(() => this._reportEventsIfAny(), 250);
66-
});
67-
68-
await new Promise<void>((resolve, reject) => this._fsWatcher!.once('ready', () => resolve()).once('error', reject));
74+
// Files are watched through their parent directory, so that editors
75+
// that replace the file on save do not break the watcher.
76+
this._watchedFiles = new Set(paths.filter(p => !dirs.has(p) && !isCovered(p)));
77+
for (const dir of new Set([...this._watchedFiles].map(file => path.dirname(file))))
78+
this._watch(dir, false, file => this._onFileEvent(file));
6979
}
7080

7181
async close() {
72-
await this._fsWatcher?.close();
82+
this._closeAll();
83+
clearTimeout(this._throttleTimer);
84+
this._throttleTimer = undefined;
85+
}
86+
87+
private _watch(dir: string, recursive: boolean, onEvent: (file: string) => void) {
88+
if (this._watchers.has(dir))
89+
return;
90+
try {
91+
const watcher = fs.watch(dir, { persistent: true, recursive }, (eventType, filename) => {
92+
onEvent(filename ? path.join(dir, filename) : dir);
93+
});
94+
watcher.on('error', () => this._close(dir));
95+
this._watchers.set(dir, watcher);
96+
} catch {
97+
// The directory does not exist, is not accessible, or we have run out of watches.
98+
}
99+
}
100+
101+
private _watchTree(dir: string) {
102+
if (this._isIgnored(dir) || this._watchers.has(dir))
103+
return;
104+
this._watch(dir, false, file => this._onTreeEvent(file));
105+
if (!this._watchers.has(dir))
106+
return;
107+
let entries: fs.Dirent[] = [];
108+
try {
109+
entries = fs.readdirSync(dir, { withFileTypes: true });
110+
} catch {
111+
}
112+
for (const entry of entries) {
113+
// Dirent.isDirectory() is false for symlinks, so they are not followed.
114+
if (entry.isDirectory())
115+
this._watchTree(path.join(dir, entry.name));
116+
}
117+
}
118+
119+
private _onDirEvent(file: string) {
120+
if (!this._isIgnored(file))
121+
this._collect(file);
122+
}
123+
124+
private _onTreeEvent(file: string) {
125+
if (this._isIgnored(file))
126+
return;
127+
this._collect(file);
128+
// A directory may have been added, removed or replaced.
129+
if (this._watchers.has(file)) {
130+
if (!isDirectory(file))
131+
this._closeTree(file);
132+
} else if (isDirectory(file)) {
133+
this._watchTree(file);
134+
}
135+
}
136+
137+
private _onFileEvent(file: string) {
138+
if (this._watchedFiles.has(file))
139+
this._collect(file);
140+
}
141+
142+
private _isIgnored(file: string) {
143+
if (file.split(path.sep).includes('node_modules'))
144+
return true;
145+
return this._params.ignoredFolders.some(folder => file === folder || isInside(folder, file));
146+
}
147+
148+
private _collect(file: string) {
149+
this._collector.add(file);
150+
clearTimeout(this._throttleTimer);
151+
this._throttleTimer = setTimeout(() => this._reportEventsIfAny(), 250);
73152
}
74153

75154
private _reportEventsIfAny() {
76-
if (this._collector.length)
77-
this._onChange(this._collector.slice());
78-
this._collector.length = 0;
155+
if (this._collector.size)
156+
this._onChange([...this._collector]);
157+
this._collector.clear();
158+
}
159+
160+
private _close(dir: string) {
161+
this._watchers.get(dir)?.close();
162+
this._watchers.delete(dir);
163+
}
164+
165+
private _closeTree(dir: string) {
166+
for (const watched of [...this._watchers.keys()]) {
167+
if (watched === dir || isInside(dir, watched))
168+
this._close(watched);
169+
}
170+
}
171+
172+
private _closeAll() {
173+
for (const watcher of this._watchers.values())
174+
watcher.close();
175+
this._watchers.clear();
176+
}
177+
}
178+
179+
function isInside(dir: string, file: string) {
180+
return file.startsWith(dir + path.sep);
181+
}
182+
183+
function isDirectory(file: string) {
184+
try {
185+
return fs.lstatSync(file).isDirectory();
186+
} catch {
187+
return false;
79188
}
80189
}

‎packages/playwright/src/runner/testRunner.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ export class TestRunner extends EventEmitter<TestRunnerEventMap> {
107107
super();
108108
this.configLocation = configLocation;
109109
this._configCLIOverrides = configCLIOverrides;
110-
this._watcher = new FSWatcher(events => {
110+
this._watcher = new FSWatcher(files => {
111111
const collector = new Set<string>();
112-
events.forEach(f => cc.collectAffectedTestFiles(f.file, collector));
112+
files.forEach(file => cc.collectAffectedTestFiles(file, collector));
113113
this.emit(TestRunnerEvent.TestFilesChanged, [...collector]);
114114
});
115115
}
@@ -269,7 +269,10 @@ export class TestRunner extends EventEmitter<TestRunnerEventMap> {
269269
}
270270

271271
private async _updateWatcher(reportPending: boolean) {
272-
await this._watcher.update([...this._watchedProjectDirs, ...this._watchedTestDependencies], [...this._ignoredProjectOutputs], reportPending);
272+
await this._watcher.update({
273+
paths: [...this._watchedProjectDirs, ...this._watchedTestDependencies],
274+
ignoredFolders: [...this._ignoredProjectOutputs],
275+
}, reportPending);
273276
}
274277

275278
async runTests(userReporter: AnyReporter, params: RunTestsParams): Promise<{ status: FullResultStatus }> {

‎tests/playwright-test/test-server.spec.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
// @ts-nocheck
1818

19+
import path from 'path';
20+
1921
import { test as baseTest, expect } from './ui-mode-fixtures';
2022
import { TestServerConnection } from '../../packages/playwright/lib/isomorphic';
2123
import ws from 'ws';
@@ -125,6 +127,35 @@ test('file watching', async ({ startTestServer, writeFiles }, testInfo) => {
125127
]);
126128
});
127129

130+
test('file watching with a directory', async ({ startTestServer, writeFiles }, testInfo) => {
131+
// The VS Code extension watches the test dir with a trailing separator when watching all tests,
132+
// and a directory without it when watching a folder.
133+
await writeFiles({
134+
'a.test.ts': `
135+
import { test } from '@playwright/test';
136+
test('foo', () => {});
137+
`,
138+
});
139+
140+
const testServerConnection = await startTestServer();
141+
expect(await testServerConnection.runTests({ locations: [] })).toEqual({ status: 'passed' });
142+
143+
await testServerConnection.watch({ fileNames: [testInfo.outputPath() + path.sep] });
144+
145+
await writeFiles({
146+
'a.test.ts': `
147+
import { test } from '@playwright/test';
148+
test('foo', () => {});
149+
test('bar', () => {});
150+
`,
151+
});
152+
153+
const changedEvents = () => testServerConnection.events.filter(e => e[0] === 'testFilesChanged');
154+
await expect.poll(changedEvents).toEqual([
155+
['testFilesChanged', { testFiles: [testInfo.outputPath('a.test.ts')] }]
156+
]);
157+
});
158+
128159
test('should list tests with testIdAttribute', async ({ startTestServer, writeFiles }) => {
129160
await writeFiles({
130161
'a.test.ts': `

0 commit comments

Comments
 (0)