Skip to content

Commit 2e23de2

Browse files
committed
metro-file-map: Always include package.json, drop the extension recheck
`Watcher` always watches `package.json` files, whatever `extensions` says, because they're crucial for module resolution. Neither crawler does the same, so with `json` absent from `extensions` they're skipped at startup. `FileMap` then checks every regular file change against `extensions` again, which drops the `package.json` events the watcher backend let through. This passes the same basenames to both crawlers. The Node crawler checks them alongside extensions. The Watchman crawler adds a `name` term for any basename whose extension isn't in `extensions`, and widens the `suffix` generator to cover it. With crawling and watching agreeing on which files to include, the watcher backends' `included` filter from #2000 is the only one needed, so this removes `FileMap`'s `hasWatchedExtension` recheck. There's no difference for a default Metro config, where `json` is in `sourceExts`. Changelog: [Fix] Include `package.json` files in the file map even when `json` is not in `sourceExts`, `assetExts` or `watcher.additionalExts`. Test plan: New `planQuery` tests for basenames, including that they don't change the query when `extensions` already covers them. The crawler integration test runs both crawlers against a real fixture tree with a `package.json` and only `js` in `extensions`. The `FileMap` test for the removed recheck is deleted, since `common-test.js` covers the backends' filter.
1 parent a779246 commit 2e23de2

13 files changed

Lines changed: 163 additions & 58 deletions

File tree

‎packages/metro-file-map/src/Watcher.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ const debug = debugModule('Metro:Watcher');
3737

3838
const MAX_WAIT_TIME = 240000;
3939

40+
// Files crawled and watched regardless of `extensions`. package.json files are
41+
// crucial for module resolution.
42+
const INCLUDED_BASENAMES: ReadonlyArray<string> = ['package.json'];
43+
4044
type InternalCrawlOptions = Readonly<{
4145
previousState: CrawlerOptions['previousState'],
4246
roots: ReadonlyArray<string>,
@@ -148,6 +152,7 @@ export class Watcher extends EventEmitter {
148152
console: options.console,
149153
includeSymlinks: options.enableSymlinks,
150154
extensions: options.extensions,
155+
basenames: INCLUDED_BASENAMES,
151156
ignore: ignoreForCrawl,
152157
onStatus: status => {
153158
this.emit('status', status);
@@ -223,9 +228,7 @@ export class Watcher extends EventEmitter {
223228
const watcherOptions: WatcherBackendOptions = {
224229
included: {
225230
extensions: new Set(extensions),
226-
// Ensure we always include package.json files, which are crucial for
227-
// module resolution.
228-
basenames: new Set(['package.json']),
231+
basenames: new Set(INCLUDED_BASENAMES),
229232
// Ensure we always watch any health check files
230233
basenamePrefixes: [this.#options.healthCheckFilePrefix],
231234
},

‎packages/metro-file-map/src/__tests__/index-test.js‎

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,38 +2018,6 @@ describe('FileMap', () => {
20182018
},
20192019
);
20202020

2021-
fm_it(
2022-
'does not emit changes for regular files with unwatched extensions',
2023-
async ({fileMap}) => {
2024-
const {fileSystem} = await fileMap.build();
2025-
mockFs[p('/project/fruits/Banana.unwatched')] = '';
2026-
2027-
const e = mockEmitters[p('/project/fruits')];
2028-
e.emitFileEvent({
2029-
event: 'touch',
2030-
relativePath: 'Banana.js',
2031-
metadata: MOCK_CHANGE_FILE,
2032-
});
2033-
e.emitFileEvent({
2034-
event: 'touch',
2035-
relativePath: 'Banana.unwatched',
2036-
metadata: MOCK_CHANGE_FILE,
2037-
});
2038-
const {changes} = await waitForItToChange(fileMap);
2039-
const filePath = p('/project/fruits/Banana.js');
2040-
expect(countFileChanges(changes)).toBe(1);
2041-
expectChanges(changes, {
2042-
modifiedFiles: [
2043-
[
2044-
path.join('fruits', 'Banana.js'),
2045-
{isSymlink: false, modifiedTime: 45},
2046-
],
2047-
],
2048-
});
2049-
expect(fileSystem.exists(filePath)).toBe(true);
2050-
},
2051-
);
2052-
20532021
fm_it(
20542022
'does not emit delete events for unknown files',
20552023
async ({fileMap}) => {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

‎packages/metro-file-map/src/crawlers/__tests__/integration-test.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ const CASES = [
7878
new Map([
7979
['foo.js', [expect.any(Number), 245, 0, null, 0]],
8080
[join('directory', 'bar.js'), [expect.any(Number), 245, 0, null, 0]],
81+
[join('directory', 'package.json'), [expect.any(Number), 3, 0, null, 0]],
8182
[
8283
'link-to-directory',
8384
[expect.any(Number), 9, 0, null, expect.oneOf(1, 'directory')],
@@ -92,6 +93,7 @@ const CASES = [
9293
false,
9394
new Map([
9495
[join('directory', 'bar.js'), [expect.any(Number), 245, 0, null, 0]],
96+
[join('directory', 'package.json'), [expect.any(Number), 3, 0, null, 0]],
9597
['foo.js', [expect.any(Number), 245, 0, null, 0]],
9698
]),
9799
],
@@ -121,6 +123,7 @@ describe.each(Object.keys(CRAWLERS))(
121123
},
122124
includeSymlinks,
123125
extensions: ['js'],
126+
basenames: ['package.json'],
124127
ignore: path => path.includes('ignored'),
125128
roots: [FIXTURES_DIR],
126129
rootDir: FIXTURES_DIR,

‎packages/metro-file-map/src/crawlers/__tests__/watchman-test.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ describe('watchman watch', () => {
135135
fileSystem: getFS(new Map()),
136136
},
137137
extensions: ['js', 'json'],
138+
basenames: [],
138139
ignore: pearMatcher,
139140
rootDir: ROOT_MOCK,
140141
roots: ROOTS,
@@ -210,6 +211,7 @@ describe('watchman watch', () => {
210211
fileSystem: getFS(mockFiles),
211212
},
212213
extensions: ['js', 'json'],
214+
basenames: [],
213215
ignore: pearMatcher,
214216
rootDir: ROOT_MOCK,
215217
roots: ROOTS,
@@ -278,6 +280,7 @@ describe('watchman watch', () => {
278280
fileSystem: getFS(mockFiles),
279281
},
280282
extensions: ['js', 'json'],
283+
basenames: [],
281284
ignore: pearMatcher,
282285
rootDir: ROOT_MOCK,
283286
roots: ROOTS,
@@ -358,6 +361,7 @@ describe('watchman watch', () => {
358361
fileSystem: getFS(mockFiles),
359362
},
360363
extensions: ['js', 'json'],
364+
basenames: [],
361365
ignore: pearMatcher,
362366
rootDir: ROOT_MOCK,
363367
roots: ROOTS,
@@ -413,6 +417,7 @@ describe('watchman watch', () => {
413417
fileSystem: getFS(new Map()),
414418
},
415419
extensions: ['js', 'json'],
420+
basenames: [],
416421
ignore: pearMatcher,
417422
rootDir: ROOT_MOCK,
418423
roots: [...ROOTS, ROOT_MOCK],
@@ -476,6 +481,7 @@ describe('watchman watch', () => {
476481
fileSystem: getFS(new Map()),
477482
},
478483
extensions: ['js', 'json'],
484+
basenames: [],
479485
rootDir: ROOT_MOCK,
480486
roots: [ROOT_MOCK],
481487
});
@@ -528,6 +534,7 @@ describe('watchman watch', () => {
528534
files: mockFiles,
529535
},
530536
extensions: ['js', 'json'],
537+
basenames: [],
531538
ignore: pearMatcher,
532539
rootDir: ROOT_MOCK,
533540
roots: ROOTS,
@@ -559,6 +566,7 @@ describe('watchman watch', () => {
559566
files: new Map(),
560567
},
561568
extensions: ['js', 'json'],
569+
basenames: [],
562570
ignore: pearMatcher,
563571
rootDir: ROOT_MOCK,
564572
roots: ROOTS,
@@ -592,6 +600,7 @@ describe('watchman watch', () => {
592600
files: new Map(),
593601
},
594602
extensions: ['js', 'json'],
603+
basenames: [],
595604
ignore: pearMatcher,
596605
rootDir: ROOT_MOCK,
597606
roots: ROOTS,

‎packages/metro-file-map/src/crawlers/node/index.js‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@ import * as fs from 'graceful-fs';
1616
import * as path from 'node:path';
1717

1818
function find(options: CrawlerOptions): Promise<FileData> {
19-
const {console, extensions, ignore, includeSymlinks, rootDir, roots} =
20-
options;
19+
const {
20+
basenames,
21+
console,
22+
extensions,
23+
ignore,
24+
includeSymlinks,
25+
rootDir,
26+
roots,
27+
} = options;
2128
const result: FileData = new Map();
2229
const pathUtils = new RootPathUtils(rootDir);
2330
const exts = new Set(extensions);
31+
const includedBasenames = new Set(basenames);
2432

2533
return new Promise(resolve => {
2634
let activeCalls = 0;
@@ -76,7 +84,11 @@ function find(options: CrawlerOptions): Promise<FileData> {
7684
}
7785

7886
const ext = path.extname(name).substr(1);
79-
if (!isSymbolicLink && !exts.has(ext)) {
87+
if (
88+
!isSymbolicLink &&
89+
!exts.has(ext) &&
90+
!includedBasenames.has(name)
91+
) {
8092
continue;
8193
}
8294

‎packages/metro-file-map/src/crawlers/watchman/__tests__/index-test.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const watchmanPath: string => string = filePath =>
3737

3838
const DEFAULT_OPTIONS: CrawlerOptions = {
3939
abortSignal: null,
40+
basenames: [],
4041
computeSha1: true,
4142
console: global.console,
4243
extensions: ['js'],

‎packages/metro-file-map/src/crawlers/watchman/__tests__/planQuery-test.js‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe('planQuery with includeSymlinks: false', () => {
1717
since: 'clock',
1818
directoryFilters: ['/dir1', '/dir2'],
1919
extensions: ['js', 'ts'],
20+
basenames: [],
2021
includeSha1: true,
2122
includeSymlinks: false,
2223
});
@@ -38,6 +39,7 @@ describe('planQuery with includeSymlinks: false', () => {
3839
since: 'clock',
3940
directoryFilters: [],
4041
extensions: ['js', 'ts'],
42+
basenames: [],
4143
includeSha1: true,
4244
includeSymlinks: false,
4345
});
@@ -54,6 +56,7 @@ describe('planQuery with includeSymlinks: false', () => {
5456
since: null,
5557
directoryFilters: ['/dir1', '/dir2'],
5658
extensions: ['js', 'ts'],
59+
basenames: [],
5760
includeSha1: true,
5861
includeSymlinks: false,
5962
});
@@ -71,6 +74,7 @@ describe('planQuery with includeSymlinks: false', () => {
7174
since: null,
7275
directoryFilters: [],
7376
extensions: ['js', 'ts'],
77+
basenames: [],
7478
includeSha1: true,
7579
includeSymlinks: false,
7680
});
@@ -87,6 +91,7 @@ describe('planQuery with includeSymlinks: false', () => {
8791
since: null,
8892
directoryFilters: [],
8993
extensions: ['js', 'ts'],
94+
basenames: [],
9095
includeSha1: false,
9196
includeSymlinks: false,
9297
});
@@ -104,6 +109,7 @@ test('does not request type if includeSymlinks == false', () => {
104109
since: null,
105110
directoryFilters: [],
106111
extensions: ['js', 'ts'],
112+
basenames: [],
107113
includeSha1: false,
108114
includeSymlinks: false,
109115
});
@@ -121,6 +127,7 @@ describe('planQuery with includeSymlinks: true', () => {
121127
since: 'clock',
122128
directoryFilters: ['/dir1', '/dir2'],
123129
extensions: ['js', 'ts'],
130+
basenames: [],
124131
includeSha1: true,
125132
includeSymlinks: true,
126133
});
@@ -145,6 +152,7 @@ describe('planQuery with includeSymlinks: true', () => {
145152
since: 'clock',
146153
directoryFilters: [],
147154
extensions: ['js', 'ts'],
155+
basenames: [],
148156
includeSha1: true,
149157
includeSymlinks: true,
150158
});
@@ -165,6 +173,7 @@ describe('planQuery with includeSymlinks: true', () => {
165173
since: null,
166174
directoryFilters: ['/dir1', '/dir2'],
167175
extensions: ['js', 'ts'],
176+
basenames: [],
168177
includeSha1: true,
169178
includeSymlinks: true,
170179
});
@@ -186,6 +195,7 @@ describe('planQuery with includeSymlinks: true', () => {
186195
since: null,
187196
directoryFilters: [],
188197
extensions: ['js', 'ts'],
198+
basenames: [],
189199
includeSha1: true,
190200
includeSymlinks: true,
191201
});
@@ -205,6 +215,7 @@ describe('planQuery with includeSymlinks: true', () => {
205215
since: null,
206216
directoryFilters: [],
207217
extensions: ['js', 'ts'],
218+
basenames: [],
208219
includeSha1: false,
209220
includeSymlinks: true,
210221
});
@@ -219,3 +230,84 @@ describe('planQuery with includeSymlinks: true', () => {
219230
});
220231
});
221232
});
233+
234+
describe('planQuery with basenames', () => {
235+
test('does not change a query when extensions match every basename', () => {
236+
const options = {
237+
since: null,
238+
directoryFilters: [],
239+
extensions: ['js', 'json'],
240+
includeSha1: false,
241+
includeSymlinks: false,
242+
};
243+
expect(planQuery({...options, basenames: ['package.json']})).toEqual(
244+
planQuery({...options, basenames: []}),
245+
);
246+
});
247+
248+
test('plans a "suffix" query that also generates basename suffixes', () => {
249+
const {query, queryGenerator} = planQuery({
250+
since: null,
251+
directoryFilters: [],
252+
extensions: ['js', 'ts'],
253+
basenames: ['package.json'],
254+
includeSha1: false,
255+
includeSymlinks: false,
256+
});
257+
expect(queryGenerator).toBe('suffix');
258+
expect(query).toEqual({
259+
suffix: ['js', 'ts', 'json'],
260+
expression: [
261+
'allof',
262+
['type', 'f'],
263+
['anyof', ['suffix', ['js', 'ts']], ['name', ['package.json']]],
264+
],
265+
fields: ['name', 'exists', 'mtime_ms', 'size'],
266+
});
267+
});
268+
269+
test('plans an "all" query for a basename with no extension', () => {
270+
const {query, queryGenerator} = planQuery({
271+
since: null,
272+
directoryFilters: [],
273+
extensions: ['js', 'ts'],
274+
basenames: ['BUCK'],
275+
includeSha1: false,
276+
includeSymlinks: false,
277+
});
278+
expect(queryGenerator).toBe('all');
279+
expect(query).toEqual({
280+
expression: [
281+
'allof',
282+
['type', 'f'],
283+
['anyof', ['suffix', ['js', 'ts']], ['name', ['BUCK']]],
284+
],
285+
fields: ['name', 'exists', 'mtime_ms', 'size'],
286+
});
287+
});
288+
289+
test('includes basenames alongside symlinks', () => {
290+
const {query, queryGenerator} = planQuery({
291+
since: 'clock',
292+
directoryFilters: [],
293+
extensions: ['js', 'ts'],
294+
basenames: ['package.json'],
295+
includeSha1: false,
296+
includeSymlinks: true,
297+
});
298+
expect(queryGenerator).toBe('since');
299+
expect(query).toEqual({
300+
since: 'clock',
301+
expression: [
302+
'anyof',
303+
[
304+
'allof',
305+
['type', 'f'],
306+
['anyof', ['suffix', ['js', 'ts']], ['name', ['package.json']]],
307+
],
308+
['type', 'l'],
309+
],
310+
fields: ['name', 'exists', 'mtime_ms', 'size', 'type'],
311+
});
312+
});
313+
});

‎packages/metro-file-map/src/crawlers/watchman/index.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ function makeWatchmanError(error: Error): Error {
4848

4949
export default async function watchmanCrawl({
5050
abortSignal,
51+
basenames,
5152
computeSha1,
5253
extensions,
5354
ignore,
@@ -204,6 +205,7 @@ export default async function watchmanCrawl({
204205
const {query, queryGenerator} = planQuery({
205206
since,
206207
extensions,
208+
basenames,
207209
directoryFilters,
208210
includeSha1: computeSha1,
209211
includeSymlinks,

0 commit comments

Comments
 (0)