Skip to content

Commit b51ffd3

Browse files
authored
Resolver: Observe Haste names under unstable_incrementalResolution
## Context #1949 records what a resolution looks up in the file map, but Haste lookups aren't paths. With `allowHaste`, every bare specifier is looked up by name in the Haste map, and a module of that name appearing later changes the result. So the name needs observing whether or not it was found. ## This change This adds `haste` to `ResolutionObservations`. It's the bare name, not the platform, because `HastePlugin` falls back from platform to `native` to generic and a change to any of those can change the answer: https://github.com/react/metro/blob/6dbe97956af0f66f7a1d88014ba0b784a4a4d893/packages/metro-file-map/src/plugins/HastePlugin.js#L194-L213 It's free in the default config, where nothing can have a Haste name and the map is empty. Otherwise the set is created on the first name recorded, since most resolutions are relative imports that never consult Haste. No measurable cost either way. Invalidating from these needs `HastePlugin` to report which names a change rebound, which is #1953. ## Benchmark AI-driven, flag on in all columns, 7 interleaved rounds. Everything is within half a percent, inside the run-to-run spread. | scenario | #1949 | this PR | #1949, Haste enabled | this PR, Haste enabled | |---|---|---|---|---| | first build (ms) | 112.4 | 111.8 | 111.2 | 110.6 | | rebuild, nothing changed (ms) | 7.75 | 7.78 | 7.81 | 7.79 | | after a source edit, the edited module's dependencies (µs) | 177 | 168 | 170 | 172 | | after a `package.json` change, re-resolving everything (ms) | 93.2 | 93.2 | 93.6 | 94.1 | | heap retained after resolving (MB) | 18.8 | 18.8 | | 19.1 | Changelog: Internal ## Test plan New tests cover Haste inert, a name found and not found, a deep import, and a relative import. Benchmark app: the synthetic RN 0.87 app described in #1945 (2.8k-module bundle, 34k-file file map). "Haste enabled" sets `enableGlobalPackages: true`, which gives its first-party packages Haste names.
1 parent 6b94381 commit b51ffd3

4 files changed

Lines changed: 143 additions & 8 deletions

File tree

‎packages/metro/src/DeltaBundler/__tests__/resolver-test.js‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3313,6 +3313,7 @@ function dep(name: string): TransformResultDependency {
33133313
'package.json',
33143314
),
33153315
content: new Set(),
3316+
haste: null,
33163317
},
33173318
});
33183319
});
@@ -3358,6 +3359,7 @@ function dep(name: string): TransformResultDependency {
33583359
'node_modules/pkg/main.js',
33593360
),
33603361
content: new Set(),
3362+
haste: null,
33613363
},
33623364
});
33633365
});
@@ -3380,6 +3382,7 @@ function dep(name: string): TransformResultDependency {
33803382
),
33813383
// Retargeting the link changes what every path through it means
33823384
content: canonical('link'),
3385+
haste: null,
33833386
},
33843387
});
33853388
});
@@ -3405,6 +3408,7 @@ function dep(name: string): TransformResultDependency {
34053408
'package.json',
34063409
),
34073410
content: new Set(),
3411+
haste: null,
34083412
});
34093413
});
34103414

@@ -3444,6 +3448,7 @@ function dep(name: string): TransformResultDependency {
34443448
'package.json',
34453449
),
34463450
content: new Set(),
3451+
haste: null,
34473452
},
34483453
});
34493454
});
@@ -3456,6 +3461,109 @@ function dep(name: string): TransformResultDependency {
34563461
expect(second).toBe(first);
34573462
expect(second.unstable_observations?.existence.size).toBeGreaterThan(0);
34583463
});
3464+
describe('Haste names', () => {
3465+
const withHaste: InputConfigT = {
3466+
resolver: {
3467+
hasteImplModulePath: path.join(
3468+
__dirname,
3469+
'../__fixtures__/hasteImpl.js',
3470+
),
3471+
unstable_incrementalResolution: true,
3472+
},
3473+
};
3474+
3475+
test('are not observed when no file can be given one', async () => {
3476+
setMockFileSystem({
3477+
'index.js': '',
3478+
node_modules: {pkg: {'index.js': ''}},
3479+
});
3480+
resolver = await createResolver(incremental, 'ios');
3481+
// Bare specifiers go to the Haste map, which stays empty
3482+
expect(
3483+
resolver.resolve(p('/root/index.js'), dep('pkg'))
3484+
.unstable_observations?.haste,
3485+
).toBe(null);
3486+
});
3487+
3488+
test('records a name that is found', async () => {
3489+
setMockFileSystem({
3490+
'index.js': '',
3491+
'hasteModule.js': '@providesModule hasteModule',
3492+
});
3493+
resolver = await createResolver(withHaste, 'ios');
3494+
const resolution = resolver.resolve(
3495+
p('/root/index.js'),
3496+
dep('hasteModule'),
3497+
);
3498+
expect(resolution.filePath).toBe(p('/root/hasteModule.js'));
3499+
expect(resolution.unstable_observations?.haste).toEqual(
3500+
new Set(['hasteModule']),
3501+
);
3502+
});
3503+
3504+
test('records a name that is not found, since it may appear later', async () => {
3505+
setMockFileSystem({
3506+
'index.js': '',
3507+
node_modules: {pkg: {'index.js': ''}},
3508+
});
3509+
resolver = await createResolver(withHaste, 'ios');
3510+
const resolution = resolver.resolve(p('/root/index.js'), dep('pkg'));
3511+
expect(resolution.filePath).toBe(
3512+
p('/root/node_modules/pkg/index.js'),
3513+
);
3514+
expect(resolution.unstable_observations?.haste).toEqual(
3515+
new Set(['pkg']),
3516+
);
3517+
});
3518+
3519+
test('records only the package name of a deep import', async () => {
3520+
setMockFileSystem({
3521+
'index.js': '',
3522+
node_modules: {pkg: {lib: {'thing.js': ''}}},
3523+
});
3524+
resolver = await createResolver(withHaste, 'ios');
3525+
expect(
3526+
resolver.resolve(p('/root/index.js'), dep('pkg/lib/thing'))
3527+
.unstable_observations?.haste,
3528+
).toEqual(new Set(['pkg']));
3529+
});
3530+
3531+
test('records nothing for a relative import, which does not consult Haste', async () => {
3532+
setMockFileSystem({'index.js': '', 'a.js': ''});
3533+
resolver = await createResolver(withHaste, 'ios');
3534+
expect(
3535+
resolver.resolve(p('/root/index.js'), dep('./a'))
3536+
.unstable_observations?.haste,
3537+
).toBe(null);
3538+
});
3539+
3540+
test('records a global package name', async () => {
3541+
setMockFileSystem({
3542+
'index.js': '',
3543+
aPackage: {
3544+
'package.json': JSON.stringify({name: 'aPackage'}),
3545+
'index.js': '',
3546+
},
3547+
});
3548+
resolver = await createResolver(
3549+
{
3550+
resolver: {
3551+
enableGlobalPackages: true,
3552+
unstable_incrementalResolution: true,
3553+
},
3554+
},
3555+
'ios',
3556+
);
3557+
const resolution = resolver.resolve(
3558+
p('/root/index.js'),
3559+
dep('aPackage'),
3560+
);
3561+
expect(resolution.filePath).toBe(p('/root/aPackage/index.js'));
3562+
expect(resolution.unstable_observations?.haste).toEqual(
3563+
new Set(['aPackage']),
3564+
);
3565+
});
3566+
});
34593567
});
34603568

34613569
describe('schemeResolvers', () => {

‎packages/metro/src/DeltaBundler/types.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,13 @@ export type AllowOptionalDependencies =
174174
boolean | AllowOptionalDependenciesWithOptions;
175175

176176
// Canonical paths whose addition or removal (`existence`) or modification
177-
// (`content`) can change a resolution's result.
177+
// (`content`) can change a resolution's result, and every Haste name it looked
178+
// up, found or not (`haste`). `haste` is null if there were none, which is
179+
// always so when no file can have a Haste name.
178180
export type ResolutionObservations = Readonly<{
179181
existence: Set<string>,
180182
content: Set<string>,
183+
haste: ?Set<string>,
181184
}>;
182185

183186
export type BundlerResolution =

‎packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ type Options = Readonly<{
4545
) => ReturnType<FileSystemLookup>,
4646
getHasteModulePath: (name: string, platform: ?string) => ?string,
4747
getHastePackagePath: (name: string, platform: ?string) => ?string,
48+
// Whether any file can have a Haste name. If not, lookups are not observed.
49+
isHasteEnabled: boolean,
4850
mainFields: ReadonlyArray<string>,
4951
getPackage: (packageJsonPath: string) => ?PackageJson,
5052
getPackageForModule: (
@@ -76,8 +78,8 @@ type Options = Readonly<{
7678
}>;
7779

7880
// Same shape for every record, to keep lookups monomorphic.
79-
function createObservations(): ResolutionObservations {
80-
return {existence: new Set(), content: new Set()};
81+
function createObservations(): {...ResolutionObservations} {
82+
return {existence: new Set(), content: new Set(), haste: null};
8183
}
8284

8385
export class ModuleResolver {
@@ -224,7 +226,7 @@ export class ModuleResolver {
224226
allowHaste: boolean,
225227
platform: string | null,
226228
resolverOptions: ResolverInputOptions,
227-
observations: ?ResolutionObservations,
229+
observations: ?{...ResolutionObservations},
228230
): Resolution {
229231
const {
230232
assetExts,
@@ -246,6 +248,18 @@ export class ModuleResolver {
246248
unstable_enablePackageExports,
247249
unstable_resolveExportsFromPackageRoot,
248250
} = this._options;
251+
// Most resolutions never consult Haste, so the set is created on demand.
252+
const observeHasteName =
253+
observations != null && this._options.isHasteEnabled
254+
? (name: string) => {
255+
let names = observations.haste;
256+
if (names == null) {
257+
names = new Set<string>();
258+
observations.haste = names;
259+
}
260+
names.add(name);
261+
}
262+
: null;
249263

250264
return Resolver.resolve(
251265
{
@@ -275,10 +289,15 @@ export class ModuleResolver {
275289
? resolveAsset
276290
: (dirPath, assetName, extension) =>
277291
resolveAsset(dirPath, assetName, extension, observations),
278-
resolveHasteModule: (name: string) =>
279-
this._options.getHasteModulePath(name, platform),
280-
resolveHastePackage: (name: string) =>
281-
this._options.getHastePackagePath(name, platform),
292+
// Recorded by name, found or not: lookups fall back across platforms
293+
resolveHasteModule: (name: string) => {
294+
observeHasteName?.(name);
295+
return this._options.getHasteModulePath(name, platform);
296+
},
297+
resolveHastePackage: (name: string) => {
298+
observeHasteName?.(name);
299+
return this._options.getHastePackagePath(name, platform);
300+
},
282301
resolveRequest,
283302
schemeResolvers,
284303
sourceExts,
@@ -328,6 +347,7 @@ export class ModuleResolver {
328347
return emptyModule;
329348
}
330349
// The empty module's resolution is cached, so merge in what it observed.
350+
// It is resolved without Haste, so has no names to merge.
331351
const emptyObservations = emptyModule.unstable_observations;
332352
if (emptyObservations != null) {
333353
for (const canonicalPath of emptyObservations.existence) {

‎packages/metro/src/node-haste/DependencyGraph/createModuleResolver.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ export default function createModuleResolver({
5353
emptyModulePath: config.resolver.emptyModulePath,
5454
extraNodeModules: config.resolver.extraNodeModules,
5555
fileSystemLookup,
56+
// Mirrors which files `HastePlugin` gives a name
57+
isHasteEnabled:
58+
config.resolver.enableGlobalPackages ||
59+
config.resolver.hasteImplModulePath != null,
5660
getHasteModulePath: (name, platform) =>
5761
hasteMap.getModule(name, platform, true),
5862
getHastePackagePath: (name, platform) =>

0 commit comments

Comments
 (0)