-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
watchman.ts
391 lines (345 loc) · 11.3 KB
/
watchman.ts
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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import fs from 'fs';
import path from 'path';
import {sync as realpath} from 'realpath-native';
import watchman from 'fb-watchman';
import {Config} from '@jest/types';
import * as fastPath from '../lib/fast_path';
import normalizePathSep from '../lib/normalizePathSep';
import H from '../constants';
import {
InternalHasteMap,
CrawlerOptions,
FileMetaData,
FileData,
LinkMetaData,
} from '../types';
type WatchmanRoots = Map<string, Array<string>>;
const watchmanURL =
'https://facebook.github.io/watchman/docs/troubleshooting.html';
// Matches symlinks in "node_modules" directories.
const nodeModulesExpression = [
'allof',
['type', 'l'],
[
'anyof',
...['**/node_modules/*', '**/node_modules/@*/*'].map(glob => [
'match',
glob,
'wholename',
{includedotfiles: true},
]),
],
];
const NODE_MODULES = path.sep + 'node_modules' + path.sep;
function WatchmanError(error: Error): Error {
error.message =
`Watchman error: ${error.message.trim()}. Make sure watchman ` +
`is running for this project. See ${watchmanURL}.`;
return error;
}
export = async function watchmanCrawl(
options: CrawlerOptions,
): Promise<{
changedFiles?: FileData;
removedFiles: FileData;
hasteMap: InternalHasteMap;
}> {
const {data, extensions, ignore, rootDir} = options;
const client = new watchman.Client();
let clientError;
client.on('error', error => (clientError = WatchmanError(error)));
// TODO: type better than `any`
const cmd = (...args: Array<any>): Promise<any> =>
new Promise((resolve, reject) =>
client.command(args, (error, result) =>
error ? reject(WatchmanError(error)) : resolve(result),
),
);
type WatchmanFile = {
name: string;
exists: boolean;
mtime_ms: number;
size: number;
type: string;
'content.sha1hex'?: string;
};
const fields = ['name', 'exists', 'mtime_ms', 'size', 'type'];
if (options.computeSha1) {
const {capabilities} = await cmd('list-capabilities');
if (capabilities.includes('field-content.sha1hex')) {
fields.push('content.sha1hex');
}
}
// Clone the clockspec cache to avoid mutations during the watch phase.
const clocks = new Map(data.clocks);
/**
* Fetch an array of files that match the given expression and are contained
* by the given `watchRoot` (with directory filters applied).
*
* When the `watchRoot` has a cached Watchman clockspec, only changed files
* are returned. The cloned clockspec cache is updated on every query.
*
* The given `watchRoot` must be absolute.
*/
const queryRequest = async (
watchRoot: Config.Path,
dirs: Array<string>,
expression: Array<any>,
) => {
if (dirs.length) {
expression = [
'allof',
['anyof', ...dirs.map(dir => ['dirname', dir])],
expression,
];
}
const relativeRoot = fastPath.relative(rootDir, watchRoot) || '.';
const response = await cmd('query', watchRoot, {
expression,
fields,
// Use the cached clockspec since `queryRequest` is called twice per root.
since: data.clocks.get(relativeRoot),
});
if ('warning' in response) {
console.warn('watchman warning:', response.warning);
}
clocks.set(relativeRoot, response.clock);
return response;
};
// The cached array of project roots.
const roots: Config.Path[] = [];
const isValidRoot = (root: Config.Path) =>
!root.includes(NODE_MODULES) &&
!roots.includes(root) &&
!roots.find(root => root.startsWith(root + path.sep));
// Ensure every configured root is valid.
options.roots
.map(root => realpath(root))
.sort((a, b) => a.length - b.length)
.forEach(root => {
if (isValidRoot(root)) {
roots.push(root);
}
});
// Ensure every linked root is searched if valid.
data.roots.forEach(root => {
root = path.resolve(rootDir, root);
if (isValidRoot(root)) {
try {
if (fs.statSync(root).isDirectory()) {
roots.push(root);
}
} catch (e) {}
}
});
// Track which directories share a "watch root" (a common ancestor identified
// by Watchman). Use an empty array to denote a project root that is its own
// watch root. These arrays are used as "directory filters" in the 2nd phase.
const watched = new Map<Config.Path, string[]>();
// Store newfound dependency links in a local variable until the crawl phase,
// which is when we can know if Watchman is a fresh instance.
const dependencyLinks = new Map<Config.Path, LinkMetaData>();
/**
* Look for linked dependencies in every "node_modules" directory within the
* given root. Repeat for every linked dependency found that resolves to a
* directory which exists outside of all known project roots.
*/
const findDependencyLinks = async (root: Config.Path) => {
const response = await cmd('watch-project', root);
const watchRoot = normalizePathSep(response.watch);
let dirs = watched.get(watchRoot);
if (!dirs) {
watched.set(watchRoot, (dirs = []));
} else if (!dirs.length) {
return; // Ensure no directory filters are used.
}
const dir = normalizePathSep(response.relative_path || '');
if (dir) {
if (dirs.includes(dir)) {
return; // Avoid crawling the same directory twice.
}
dirs.push(dir);
} else {
// Ensure no directory filters are used.
dirs.length = 0;
}
// Search every "node_modules" directory in the entire tree.
// It should be faster for Watchman to do this in one fell swoop.
const queryResponse = await queryRequest(
watchRoot,
dir ? [dir] : [],
nodeModulesExpression,
);
await Promise.all(
queryResponse.files.map(async (link: WatchmanFile) => {
const name = normalizePathSep(link.name);
const linkPath = path.join(watchRoot, name);
const relativePath = fastPath.relative(rootDir, linkPath);
if (!link.exists || ignore(linkPath)) {
data.links.delete(relativePath);
return;
}
let target;
try {
target = realpath(linkPath);
} catch (e) {
return; // Skip broken symlinks.
}
const metaData = data.links.get(relativePath);
const mtime = testModified(metaData, link.mtime_ms);
dependencyLinks.set(
relativePath,
mtime !== 0 ? [fastPath.relative(rootDir, target), mtime] : metaData!,
);
if (fs.statSync(target).isDirectory() && isValidRoot(target)) {
roots.push(target);
await findDependencyLinks(target);
}
}),
);
};
let isFresh = false;
let removedFiles = new Map();
const changedFiles = new Map();
try {
await Promise.all(roots.map(findDependencyLinks));
const crawlExpression = [
'anyof',
['type', 'l'],
[
'allof',
['type', 'f'],
['anyof', ...extensions.map(extension => ['suffix', extension])],
],
];
const watchRoots = Array.from(watched.keys());
const crawled = await Promise.all(
watchRoots.map(async watchRoot => {
const queryResponse = await queryRequest(
watchRoot,
watched.get(watchRoot)!,
crawlExpression,
);
if (!isFresh) {
isFresh = queryResponse.is_fresh_instance;
}
return queryResponse.files;
}),
);
// Reset the file map if Watchman refreshed.
if (isFresh) {
removedFiles = new Map(data.files);
data.files = new Map();
data.links = dependencyLinks;
} else {
dependencyLinks.forEach((link, linkPath) => {
data.links.set(linkPath, link);
});
}
// Update the file map and symlink map.
crawled.forEach((files: WatchmanFile[], i) => {
const watchRoot = watchRoots[i];
for (const file of files) {
const name = normalizePathSep(file.name);
const filePath = path.join(watchRoot, name);
const relativeFilePath = fastPath.relative(rootDir, filePath);
const isLinked = file.type === 'l';
const existingFiles: Map<string, any> = isLinked
? data.links
: data.files;
const existingFile = existingFiles.get(relativeFilePath);
if (isFresh && existingFile && file.exists) {
// If watchman is fresh, the removed files map starts with all files
// and we remove them as we verify they still exist.
removedFiles.delete(relativeFilePath);
}
if (!file.exists || ignore(filePath)) {
// No need to act on files that do not exist and were not tracked.
if (existingFile) {
existingFiles.delete(relativeFilePath);
// If watchman is not fresh, we will know what specific files were
// deleted since we last ran and can track only those files.
if (!isFresh) {
removedFiles.set(relativeFilePath, existingFile);
}
}
continue;
}
let sha1hex = file['content.sha1hex'] || null;
if (sha1hex && sha1hex.length !== 40) {
sha1hex = null;
}
const mtime = testModified(existingFile, file.mtime_ms);
if (mtime !== 0) {
if (isLinked) {
// See ../constants.ts
existingFiles.set(relativeFilePath, [undefined, mtime]);
continue;
}
let nextData: FileMetaData;
if (sha1hex && existingFile && existingFile[H.SHA1] === sha1hex) {
nextData = [
existingFile[0],
mtime,
existingFile[2],
existingFile[3],
existingFile[4],
existingFile[5],
];
} else {
// See ../constants.ts
nextData = ['', mtime, file.size, 0, '', sha1hex];
}
const mappings = options.mapper ? options.mapper(filePath) : null;
if (mappings) {
for (const absoluteVirtualFilePath of mappings) {
if (!ignore(absoluteVirtualFilePath)) {
const relativeVirtualFilePath = fastPath.relative(
rootDir,
absoluteVirtualFilePath,
);
data.files.set(relativeVirtualFilePath, nextData);
changedFiles.set(relativeVirtualFilePath, nextData);
}
}
} else {
data.files.set(relativeFilePath, nextData);
changedFiles.set(relativeFilePath, nextData);
}
}
}
});
} finally {
client.end();
}
if (clientError) {
throw clientError;
}
data.roots = roots.map(root => fastPath.relative(rootDir, root));
data.clocks = clocks;
return {
changedFiles: isFresh ? undefined : changedFiles,
hasteMap: data,
removedFiles,
};
};
/**
* Check if the file data has been modified since last cached.
*
* Returns the 2nd argument if modified, else zero.
*/
function testModified(
metaData: FileMetaData | LinkMetaData | undefined,
mtime: number | {toNumber(): number},
) {
if (typeof mtime !== 'number') {
mtime = mtime.toNumber();
}
return !metaData || metaData[H.MTIME] !== mtime ? mtime : 0;
}