From 4fcb64a0a0eab720cd4fc66bca4222872e0ca8ed Mon Sep 17 00:00:00 2001 From: Rob Hogan Date: Mon, 16 Nov 2026 15:29:41 +0000 Subject: [PATCH] Resolver: Observe Haste names under unstable_incrementalResolution MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 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. --- .../DeltaBundler/__tests__/resolver-test.js | 108 ++++++++++++++++++ packages/metro/src/DeltaBundler/types.js | 5 +- .../DependencyGraph/ModuleResolution.js | 34 ++++-- .../DependencyGraph/createModuleResolver.js | 4 + 4 files changed, 143 insertions(+), 8 deletions(-) diff --git a/packages/metro/src/DeltaBundler/__tests__/resolver-test.js b/packages/metro/src/DeltaBundler/__tests__/resolver-test.js index 70c2f88c42..699c0e2fc1 100644 --- a/packages/metro/src/DeltaBundler/__tests__/resolver-test.js +++ b/packages/metro/src/DeltaBundler/__tests__/resolver-test.js @@ -3313,6 +3313,7 @@ function dep(name: string): TransformResultDependency { 'package.json', ), content: new Set(), + haste: null, }, }); }); @@ -3358,6 +3359,7 @@ function dep(name: string): TransformResultDependency { 'node_modules/pkg/main.js', ), content: new Set(), + haste: null, }, }); }); @@ -3380,6 +3382,7 @@ function dep(name: string): TransformResultDependency { ), // Retargeting the link changes what every path through it means content: canonical('link'), + haste: null, }, }); }); @@ -3405,6 +3408,7 @@ function dep(name: string): TransformResultDependency { 'package.json', ), content: new Set(), + haste: null, }); }); @@ -3444,6 +3448,7 @@ function dep(name: string): TransformResultDependency { 'package.json', ), content: new Set(), + haste: null, }, }); }); @@ -3456,6 +3461,109 @@ function dep(name: string): TransformResultDependency { expect(second).toBe(first); expect(second.unstable_observations?.existence.size).toBeGreaterThan(0); }); + describe('Haste names', () => { + const withHaste: InputConfigT = { + resolver: { + hasteImplModulePath: path.join( + __dirname, + '../__fixtures__/hasteImpl.js', + ), + unstable_incrementalResolution: true, + }, + }; + + test('are not observed when no file can be given one', async () => { + setMockFileSystem({ + 'index.js': '', + node_modules: {pkg: {'index.js': ''}}, + }); + resolver = await createResolver(incremental, 'ios'); + // Bare specifiers go to the Haste map, which stays empty + expect( + resolver.resolve(p('/root/index.js'), dep('pkg')) + .unstable_observations?.haste, + ).toBe(null); + }); + + test('records a name that is found', async () => { + setMockFileSystem({ + 'index.js': '', + 'hasteModule.js': '@providesModule hasteModule', + }); + resolver = await createResolver(withHaste, 'ios'); + const resolution = resolver.resolve( + p('/root/index.js'), + dep('hasteModule'), + ); + expect(resolution.filePath).toBe(p('/root/hasteModule.js')); + expect(resolution.unstable_observations?.haste).toEqual( + new Set(['hasteModule']), + ); + }); + + test('records a name that is not found, since it may appear later', async () => { + setMockFileSystem({ + 'index.js': '', + node_modules: {pkg: {'index.js': ''}}, + }); + resolver = await createResolver(withHaste, 'ios'); + const resolution = resolver.resolve(p('/root/index.js'), dep('pkg')); + expect(resolution.filePath).toBe( + p('/root/node_modules/pkg/index.js'), + ); + expect(resolution.unstable_observations?.haste).toEqual( + new Set(['pkg']), + ); + }); + + test('records only the package name of a deep import', async () => { + setMockFileSystem({ + 'index.js': '', + node_modules: {pkg: {lib: {'thing.js': ''}}}, + }); + resolver = await createResolver(withHaste, 'ios'); + expect( + resolver.resolve(p('/root/index.js'), dep('pkg/lib/thing')) + .unstable_observations?.haste, + ).toEqual(new Set(['pkg'])); + }); + + test('records nothing for a relative import, which does not consult Haste', async () => { + setMockFileSystem({'index.js': '', 'a.js': ''}); + resolver = await createResolver(withHaste, 'ios'); + expect( + resolver.resolve(p('/root/index.js'), dep('./a')) + .unstable_observations?.haste, + ).toBe(null); + }); + + test('records a global package name', async () => { + setMockFileSystem({ + 'index.js': '', + aPackage: { + 'package.json': JSON.stringify({name: 'aPackage'}), + 'index.js': '', + }, + }); + resolver = await createResolver( + { + resolver: { + enableGlobalPackages: true, + unstable_incrementalResolution: true, + }, + }, + 'ios', + ); + const resolution = resolver.resolve( + p('/root/index.js'), + dep('aPackage'), + ); + expect(resolution.filePath).toBe(p('/root/aPackage/index.js')); + expect(resolution.unstable_observations?.haste).toEqual( + new Set(['aPackage']), + ); + }); + }); }); describe('schemeResolvers', () => { diff --git a/packages/metro/src/DeltaBundler/types.js b/packages/metro/src/DeltaBundler/types.js index 0475f7700f..42aa532a41 100644 --- a/packages/metro/src/DeltaBundler/types.js +++ b/packages/metro/src/DeltaBundler/types.js @@ -174,10 +174,13 @@ export type AllowOptionalDependencies = boolean | AllowOptionalDependenciesWithOptions; // Canonical paths whose addition or removal (`existence`) or modification -// (`content`) can change a resolution's result. +// (`content`) can change a resolution's result, and every Haste name it looked +// up, found or not (`haste`). `haste` is null if there were none, which is +// always so when no file can have a Haste name. export type ResolutionObservations = Readonly<{ existence: Set, content: Set, + haste: ?Set, }>; export type BundlerResolution = diff --git a/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js b/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js index 35e21efd3b..be8133b52e 100644 --- a/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js +++ b/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js @@ -45,6 +45,8 @@ type Options = Readonly<{ ) => ReturnType, getHasteModulePath: (name: string, platform: ?string) => ?string, getHastePackagePath: (name: string, platform: ?string) => ?string, + // Whether any file can have a Haste name. If not, lookups are not observed. + isHasteEnabled: boolean, mainFields: ReadonlyArray, getPackage: (packageJsonPath: string) => ?PackageJson, getPackageForModule: ( @@ -76,8 +78,8 @@ type Options = Readonly<{ }>; // Same shape for every record, to keep lookups monomorphic. -function createObservations(): ResolutionObservations { - return {existence: new Set(), content: new Set()}; +function createObservations(): {...ResolutionObservations} { + return {existence: new Set(), content: new Set(), haste: null}; } export class ModuleResolver { @@ -224,7 +226,7 @@ export class ModuleResolver { allowHaste: boolean, platform: string | null, resolverOptions: ResolverInputOptions, - observations: ?ResolutionObservations, + observations: ?{...ResolutionObservations}, ): Resolution { const { assetExts, @@ -246,6 +248,18 @@ export class ModuleResolver { unstable_enablePackageExports, unstable_resolveExportsFromPackageRoot, } = this._options; + // Most resolutions never consult Haste, so the set is created on demand. + const observeHasteName = + observations != null && this._options.isHasteEnabled + ? (name: string) => { + let names = observations.haste; + if (names == null) { + names = new Set(); + observations.haste = names; + } + names.add(name); + } + : null; return Resolver.resolve( { @@ -275,10 +289,15 @@ export class ModuleResolver { ? resolveAsset : (dirPath, assetName, extension) => resolveAsset(dirPath, assetName, extension, observations), - resolveHasteModule: (name: string) => - this._options.getHasteModulePath(name, platform), - resolveHastePackage: (name: string) => - this._options.getHastePackagePath(name, platform), + // Recorded by name, found or not: lookups fall back across platforms + resolveHasteModule: (name: string) => { + observeHasteName?.(name); + return this._options.getHasteModulePath(name, platform); + }, + resolveHastePackage: (name: string) => { + observeHasteName?.(name); + return this._options.getHastePackagePath(name, platform); + }, resolveRequest, schemeResolvers, sourceExts, @@ -328,6 +347,7 @@ export class ModuleResolver { return emptyModule; } // The empty module's resolution is cached, so merge in what it observed. + // It is resolved without Haste, so has no names to merge. const emptyObservations = emptyModule.unstable_observations; if (emptyObservations != null) { for (const canonicalPath of emptyObservations.existence) { diff --git a/packages/metro/src/node-haste/DependencyGraph/createModuleResolver.js b/packages/metro/src/node-haste/DependencyGraph/createModuleResolver.js index 739aeecfee..62b776b419 100644 --- a/packages/metro/src/node-haste/DependencyGraph/createModuleResolver.js +++ b/packages/metro/src/node-haste/DependencyGraph/createModuleResolver.js @@ -53,6 +53,10 @@ export default function createModuleResolver({ emptyModulePath: config.resolver.emptyModulePath, extraNodeModules: config.resolver.extraNodeModules, fileSystemLookup, + // Mirrors which files `HastePlugin` gives a name + isHasteEnabled: + config.resolver.enableGlobalPackages || + config.resolver.hasteImplModulePath != null, getHasteModulePath: (name, platform) => hasteMap.getModule(name, platform, true), getHastePackagePath: (name, platform) =>