Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/metro-file-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"graceful-fs": "^4.2.4",
"invariant": "^2.2.4",
"jest-worker": "^29.7.0",
"micromatch": "^4.0.4",
"nullthrows": "^1.1.1",
"walker": "^1.0.7"
},
Expand Down
13 changes: 6 additions & 7 deletions packages/metro-file-map/src/Watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,14 @@ export class Watcher extends EventEmitter {

const createWatcherBackend = (root: Path): Promise<WatcherBackend> => {
const watcherOptions: WatcherBackendOptions = {
dot: true,
globs: [
included: {
extensions: new Set(extensions),
// Ensure we always include package.json files, which are crucial for
/// module resolution.
'**/package.json',
// module resolution.
basenames: new Set(['package.json']),
// Ensure we always watch any health check files
'**/' + this.#options.healthCheckFilePrefix + '*',
...extensions.map(extension => '**/*.' + extension),
],
basenamePrefixes: [this.#options.healthCheckFilePrefix],
},
ignored: ignorePatternForWatch,
watchmanDeferStates: this.#options.watchmanDeferStates,
};
Expand Down
15 changes: 11 additions & 4 deletions packages/metro-file-map/src/flow-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,6 @@ export interface FileSystem {
}): Iterable<Path>;
}

export type Glob = string;

export type JsonData =
| string
| number
Expand Down Expand Up @@ -546,11 +544,20 @@ export type WatcherBackendChangeEvent =

export type WatcherBackendOptions = Readonly<{
ignored: ?RegExp,
globs: ReadonlyArray<string>,
dot: boolean,
included: ?WatcherIncludedFiles,
...
}>;

/**
* Regular files a watcher reports changes to: those matching any of the
* extensions (without a leading dot), basenames, or basename prefixes.
*/
export type WatcherIncludedFiles = Readonly<{
extensions: ReadonlySet<string>,
basenames: ReadonlySet<string>,
basenamePrefixes: ReadonlyArray<string>,
}>;

export type WatchmanClockSpec =
| string
| Readonly<{scm: Readonly<{'mergebase-with': string}>}>;
Expand Down
9 changes: 4 additions & 5 deletions packages/metro-file-map/src/watchers/AbstractWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
WatcherBackend,
WatcherBackendChangeEvent,
WatcherBackendOptions,
WatcherIncludedFiles,
} from '../flow-types';

import {posixPathMatchesPattern} from './common';
Expand All @@ -26,17 +27,15 @@ export type Listeners = Readonly<{
export class AbstractWatcher implements WatcherBackend {
+root: string;
+ignored: ?RegExp;
+globs: ReadonlyArray<string>;
+dot: boolean;
+included: ?WatcherIncludedFiles;
+doIgnore: (path: string) => boolean;

#emitter: EventEmitter = new EventEmitter();

constructor(dir: string, opts: WatcherBackendOptions) {
const {ignored, globs, dot} = opts;
this.dot = dot || false;
const {ignored, included} = opts;
this.ignored = ignored;
this.globs = globs;
this.included = included;
this.doIgnore = ignored
? (filePath: string) => posixPathMatchesPattern(ignored, filePath)
: () => false;
Expand Down
5 changes: 2 additions & 3 deletions packages/metro-file-map/src/watchers/FallbackWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ export default class FallbackWatcher extends AbstractWatcher {
const relativePath = path.relative(this.root, filepath);
if (
this.doIgnore(relativePath) ||
(type === 'f' &&
!common.includedByGlob('f', this.globs, this.dot, relativePath))
(type === 'f' && !common.isIncluded('f', this.included, relativePath))
) {
return false;
}
Expand Down Expand Up @@ -292,7 +291,7 @@ export default class FallbackWatcher extends AbstractWatcher {

if (
this.doIgnore(relativePath) ||
!common.includedByGlob('d', this.globs, this.dot, relativePath)
!common.isIncluded('d', this.included, relativePath)
) {
return;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/metro-file-map/src/watchers/NativeWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
* @format
*/

import type {WatcherIncludedFiles} from '../flow-types';
import type {FSWatcher} from 'fs';

import {AbstractWatcher} from './AbstractWatcher';
import {includedByGlob, typeFromStat} from './common';
import {isIncluded, typeFromStat} from './common';
import {promises as fsPromises, watch} from 'fs';
import {platform} from 'os';
import * as path from 'path';
Expand Down Expand Up @@ -54,8 +55,7 @@ export default class NativeWatcher extends AbstractWatcher {
dir: string,
opts: Readonly<{
ignored: ?RegExp,
globs: ReadonlyArray<string>,
dot: boolean,
included: ?WatcherIncludedFiles,
...
}>,
) {
Expand Down Expand Up @@ -122,7 +122,7 @@ export default class NativeWatcher extends AbstractWatcher {
return;
}

if (!includedByGlob(type, this.globs, this.dot, relativePath)) {
if (!isIncluded(type, this.included, relativePath)) {
return;
}

Expand Down
14 changes: 1 addition & 13 deletions packages/metro-file-map/src/watchers/WatchmanWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,6 @@ export default class WatchmanWatcher extends AbstractWatcher {
relative_root: watchProjectInfo.relativePath,
};

// Make sure we honor the dot option if even we're not using globs.
if (self.globs.length === 0 && !self.dot) {
options.expression = [
'match',
'**',
'wholename',
{
includedotfiles: false,
},
];
}

self.#client.command(
['subscribe', getWatchRoot(), self.subscriptionName, options],
onSubscribe,
Expand Down Expand Up @@ -269,7 +257,7 @@ export default class WatchmanWatcher extends AbstractWatcher {

if (
this.doIgnore(relativePath) ||
!common.includedByGlob(type, this.globs, this.dot, relativePath)
!common.isIncluded(type, this.included, relativePath)
) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ jest.mock('fb-watchman', () => ({
describe('WatchmanWatcher', () => {
test('initializes with watch-project, clock, subscribe', () => {
const watchmanWatcher = new WatchmanWatcher(p('/project/subdir/js'), {
dot: true,
ignored: null,
globs: ['**/*.js'],
included: {
extensions: new Set(['js']),
basenames: new Set(),
basenamePrefixes: [],
},
watchmanDeferStates: ['busy'],
});
let isSettled = false;
Expand Down Expand Up @@ -104,9 +107,12 @@ describe('WatchmanWatcher', () => {
let watchmanWatcher: WatchmanWatcher;
beforeEach(async () => {
watchmanWatcher = new WatchmanWatcher(p('/project/subdir/js'), {
dot: true,
ignored: null,
globs: ['**/*.js'],
included: {
extensions: new Set(['js']),
basenames: new Set(),
basenamePrefixes: [],
},
watchmanDeferStates: ['busy'],
});
const startPromise = watchmanWatcher.startWatching();
Expand Down Expand Up @@ -160,9 +166,12 @@ describe('WatchmanWatcher', () => {

beforeEach(async () => {
watchmanWatcher = new WatchmanWatcher(p('/project/subdir/js'), {
dot: true,
ignored: null,
globs: ['**/*.js'],
included: {
extensions: new Set(['js']),
basenames: new Set(),
basenamePrefixes: [],
},
watchmanDeferStates: ['busy'],
});
startPromise = watchmanWatcher.startWatching();
Expand Down
56 changes: 56 additions & 0 deletions packages/metro-file-map/src/watchers/__tests__/common-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall react_native
*/

import type {WatcherIncludedFiles} from '../../flow-types';

import {isIncluded} from '../common';
import {join} from 'path';

describe('isIncluded', () => {
const included: WatcherIncludedFiles = {
extensions: new Set(['js', 'json', 'env', 'local', 'd.ts']),
basenames: new Set(['package.json', 'BUCK']),
basenamePrefixes: ['.metro-health-check'],
};

test.each([
[join('src', 'index.js'), true],
[join('src', 'index.test.js'), true],
[join('src', 'data.json'), true],
['BUCK', true],
[join('node_modules', 'foo', 'BUCK'), true],
[join('src', '.metro-health-check-abc123'), true],
[join('src', 'index.ts'), false],
[join('src', 'js'), false],
[join('src', 'BUCK.v2'), false],
[join('src', 'x-.metro-health-check'), false],
[join('.hidden', 'index.js'), true],
['.env', true],
['.env.local', true],
[join('src', '.js'), true],
[join('src', 'types.d.ts'), true],
['.envrc', false],
[join('src', 'index.js.map'), false],
])('regular file %s -> %s', (relativePath, expected) => {
expect(isIncluded('f', included, relativePath)).toBe(expected);
});

test.each([['d'], ['l'], [null]])(
'type %s is not checked against included files',
type => {
expect(isIncluded(type, included, 'foo.ts')).toBe(true);
},
);

test('null included files include every regular file', () => {
expect(isIncluded('f', null, join('src', 'foo.ts'))).toBe(true);
});
});
20 changes: 15 additions & 5 deletions packages/metro-file-map/src/watchers/__tests__/integration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ describe.each(Object.keys(WATCHERS))(
await new Promise(resolve => setTimeout(resolve, 100));

const opts: WatcherOptions = {
dot: true,
globs: ['**/package.json', '**/*.js', '**/cookie-*'],
included: {
extensions: new Set(['js', 'env', 'local', 'd.ts']),
basenames: new Set(['package.json']),
basenamePrefixes: ['cookie-'],
},
// We need to ignore `.watchmanconfig` to keep these tests stable.
// Even though we write it before initialising watchers, OS-level
// delays/debouncing(?) can mean the write is *sometimes* reported by
Expand Down Expand Up @@ -122,9 +125,16 @@ describe.each(Object.keys(WATCHERS))(
await rm(watchRoot, {recursive: true});
});

maybeTest('detects a new, changed, deleted file', async () => {
const testFile = join(appRoot, 'test.js');
const relativePath = join('app', 'test.js');
maybeTest.each([
'test.js',
'.env',
'.env.local',
'types.d.ts',
'package.json',
'.hidden.js',
])('detects a new, changed, deleted %s file', async filename => {
const testFile = join(appRoot, filename);
const relativePath = join('app', filename);
expect(
await eventHelpers.nextEvent(() => writeFile(testFile, 'hello world')),
).toStrictEqual({
Expand Down
48 changes: 34 additions & 14 deletions packages/metro-file-map/src/watchers/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
* https://github.com/amasad/sane/blob/64ff3a870c42e84f744086884bf55a4f9c22d376/src/common.js
*/

import type {ChangeEventMetadata} from '../flow-types';
import type {ChangeEventMetadata, WatcherIncludedFiles} from '../flow-types';
import type {Stats} from 'fs';

// $FlowFixMe[untyped-import] - Write libdefs for `micromatch`
import micromatch from 'micromatch';
import path from 'path';

/**
Expand All @@ -30,29 +28,51 @@ export const RECRAWL_EVENT = 'recrawl';
export const ALL_EVENT = 'all';

export type WatcherOptions = Readonly<{
globs: ReadonlyArray<string>,
dot: boolean,
included: ?WatcherIncludedFiles,
ignored: ?RegExp,
watchmanDeferStates: ReadonlyArray<string>,
watchman?: unknown,
watchmanPath?: string,
}>;

/**
* Checks a file relative path against the globs array.
* Whether a watcher should report a change at the given relative path. Only
* regular files are checked against `included`, and every file is included
* when it is null.
*
* A file matches an extension when its basename ends with `.` followed by
* that extension, so `.env` matches `env` and `foo.d.ts` matches `d.ts`.
*/
export function includedByGlob(
export function isIncluded(
type: ?('f' | 'l' | 'd'),
globs: ReadonlyArray<string>,
dot: boolean,
included: ?WatcherIncludedFiles,
relativePath: string,
): boolean {
// For non-regular files or if there are no glob matchers, just respect the
// `dot` option to filter dotfiles if dot === false.
if (globs.length === 0 || type !== 'f') {
return dot || micromatch.some(relativePath, '**/*');
if (included == null || type !== 'f') {
return true;
}
const basename = path.basename(relativePath);
return (
hasIncludedExtension(included.extensions, basename) ||
included.basenames.has(basename) ||
included.basenamePrefixes.some(prefix => basename.startsWith(prefix))
);
}

function hasIncludedExtension(
extensions: ReadonlySet<string>,
basename: string,
): boolean {
for (
let i = basename.indexOf('.');
i !== -1;
i = basename.indexOf('.', i + 1)
) {
if (extensions.has(basename.slice(i + 1))) {
return true;
}
}
return micromatch.some(relativePath, globs, {dot});
return false;
}

/**
Expand Down
Loading