diff --git a/packages/metro/src/DeltaBundler/__tests__/resolver-test.js b/packages/metro/src/DeltaBundler/__tests__/resolver-test.js index 72778a92ac..70c2f88c42 100644 --- a/packages/metro/src/DeltaBundler/__tests__/resolver-test.js +++ b/packages/metro/src/DeltaBundler/__tests__/resolver-test.js @@ -3281,6 +3281,183 @@ function dep(name: string): TransformResultDependency { }); }); + describe('unstable_incrementalResolution', () => { + const incremental: InputConfigT = { + resolver: {unstable_incrementalResolution: true}, + }; + // Canonical paths: root-relative, with system separators. + const canonical = (...posixPaths: Array) => + new Set(posixPaths.map(posixPath => joinPath(...posixPath.split('/')))); + + test('resolutions carry no observations by default', async () => { + setMockFileSystem({'index.js': '', 'a.js': ''}); + resolver = await createResolver(); + expect( + resolver.resolve(p('/root/index.js'), dep('./a')), + ).not.toHaveProperty('unstable_observations'); + }); + + test('records every candidate probed for a relative import', async () => { + setMockFileSystem({'index.js': '', 'a.js': ''}); + resolver = await createResolver(incremental, 'ios'); + expect(resolver.resolve(p('/root/index.js'), dep('./a'))).toEqual({ + type: 'sourceFile', + filePath: p('/root/a.js'), + unstable_observations: { + // A more specific candidate appearing changes the result + existence: canonical( + 'a', + 'a.ios.js', + 'a.native.js', + 'a.js', + 'package.json', + ), + content: new Set(), + }, + }); + }); + + test('records each level probed for a package', async () => { + setMockFileSystem({ + deep: {dir: {'index.js': ''}}, + node_modules: { + pkg: { + 'package.json': JSON.stringify({name: 'pkg', main: 'main.js'}), + 'main.js': '', + }, + }, + }); + resolver = await createResolver(incremental, 'ios'); + expect( + resolver.resolve(p('/root/deep/dir/index.js'), dep('pkg')), + ).toEqual({ + type: 'sourceFile', + filePath: p('/root/node_modules/pkg/main.js'), + unstable_observations: { + existence: canonical( + // The closest package to the origin + 'deep/dir/index.js', + 'deep/dir/package.json', + 'deep/package.json', + 'package.json', + // Each node_modules on the way up, recorded once + 'deep/dir/node_modules', + 'deep/node_modules', + 'node_modules', + // The package scope of the files directly in node_modules + 'node_modules/package.json', + // A file is preferred to a package of the same name + 'node_modules/pkg.ios.js', + 'node_modules/pkg.native.js', + 'node_modules/pkg.js', + 'node_modules/pkg.ios.json', + 'node_modules/pkg.native.json', + 'node_modules/pkg.json', + 'node_modules/pkg', + 'node_modules/pkg/package.json', + 'node_modules/pkg/main.js', + ), + content: new Set(), + }, + }); + }); + + test('records a traversed symlink as content, and lookups by real path', async () => { + setMockFileSystem({'index.js': '', real: {'x.js': ''}}); + fs.symlinkSync(p('/root/real'), p('/root/link')); + resolver = await createResolver(incremental, 'ios'); + expect(resolver.resolve(p('/root/index.js'), dep('./link/x'))).toEqual({ + type: 'sourceFile', + filePath: p('/root/real/x.js'), + unstable_observations: { + existence: canonical( + 'real/x', + 'real/x.ios.js', + 'real/x.native.js', + 'real/x.js', + 'real/package.json', + 'package.json', + ), + // Retargeting the link changes what every path through it means + content: canonical('link'), + }, + }); + }); + + test('records every asset variant probed', async () => { + setMockFileSystem({ + 'index.js': '', + 'asset.png': '', + 'asset@2x.png': '', + }); + resolver = await createResolver(incremental, 'ios'); + expect( + resolver.resolve(p('/root/index.js'), dep('./asset.png')) + .unstable_observations, + ).toEqual({ + existence: canonical( + 'asset.png', + 'asset@1x.png', + 'asset@1.5x.png', + 'asset@2x.png', + 'asset@3x.png', + 'asset@4x.png', + 'package.json', + ), + content: new Set(), + }); + }); + + test('a resolution landing on the empty module includes what resolving the empty module observed', async () => { + setMockFileSystem({ + 'empty.js': '', + withBrowser: { + 'package.json': JSON.stringify({ + name: 'with-browser', + browser: {'./gone.js': false}, + }), + 'index.js': '', + 'gone.js': '', + }, + }); + resolver = await createResolver( + { + resolver: { + emptyModulePath: p('/root/empty.js'), + unstable_incrementalResolution: true, + }, + }, + 'ios', + ); + expect( + resolver.resolve(p('/root/withBrowser/index.js'), dep('./gone')), + ).toEqual({ + type: 'sourceFile', + filePath: p('/root/empty.js'), + unstable_observations: { + existence: canonical( + // What led to the empty module + 'withBrowser/gone', + 'withBrowser/package.json', + // What resolving the empty module observed + 'empty.js', + 'package.json', + ), + content: new Set(), + }, + }); + }); + + test('origins sharing a cached resolution share its observations', async () => { + setMockFileSystem({'index.js': '', 'other.js': '', 'a.js': ''}); + resolver = await createResolver(incremental, 'ios'); + const first = resolver.resolve(p('/root/index.js'), dep('./a')); + const second = resolver.resolve(p('/root/other.js'), dep('./a')); + expect(second).toBe(first); + expect(second.unstable_observations?.existence.size).toBeGreaterThan(0); + }); + }); + describe('schemeResolvers', () => { test('config schemeResolvers are applied to scheme-prefixed specifiers', async () => { setMockFileSystem({'index.js': '', 'a.js': ''}); diff --git a/packages/metro/src/DeltaBundler/types.js b/packages/metro/src/DeltaBundler/types.js index c0be48676e..0475f7700f 100644 --- a/packages/metro/src/DeltaBundler/types.js +++ b/packages/metro/src/DeltaBundler/types.js @@ -173,10 +173,19 @@ export type AllowOptionalDependenciesWithOptions = { export type AllowOptionalDependencies = boolean | AllowOptionalDependenciesWithOptions; +// Canonical paths whose addition or removal (`existence`) or modification +// (`content`) can change a resolution's result. +export type ResolutionObservations = Readonly<{ + existence: Set, + content: Set, +}>; + export type BundlerResolution = | Readonly<{ type: 'sourceFile', filePath: string, + // Set if `resolver.unstable_incrementalResolution` is enabled. + unstable_observations?: ResolutionObservations, }> | Readonly<{ type: 'virtualModule', @@ -189,6 +198,8 @@ export type BundlerResolution = source: Buffer, /** SHA-1 of `source`. */ sha1: string, + // Set if `resolver.unstable_incrementalResolution` is enabled. + unstable_observations?: ResolutionObservations, }>; export type Options = Readonly<{ diff --git a/packages/metro/src/node-haste/DependencyGraph.js b/packages/metro/src/node-haste/DependencyGraph.js index e4d2c559a4..3bdeedd3bc 100644 --- a/packages/metro/src/node-haste/DependencyGraph.js +++ b/packages/metro/src/node-haste/DependencyGraph.js @@ -11,6 +11,7 @@ import type { BundlerResolution, + ResolutionObservations, TransformResultDependency, } from '../DeltaBundler/types'; import type {DynamicRoot} from '../lib/dynamicRoots'; @@ -138,8 +139,8 @@ export default class DependencyGraph extends EventEmitter { ); this._resolutionCache = new Map(); this.#packageCache = new PackageCache({ - getClosestPackage: absoluteModulePath => - this._getClosestPackage(absoluteModulePath), + getClosestPackage: (absoluteModulePath, observations) => + this._getClosestPackage(absoluteModulePath, observations), }); this._createModuleResolver(); }); @@ -219,6 +220,7 @@ export default class DependencyGraph extends EventEmitter { _getClosestPackage( absoluteModulePath: string, + observations?: ?ResolutionObservations, ): ?{packageJsonPath: string, packageRelativePath: string} { const result = this._fileSystem.hierarchicalLookup( absoluteModulePath, @@ -227,6 +229,7 @@ export default class DependencyGraph extends EventEmitter { breakOnSegment: 'node_modules', subpathType: 'f', }, + observations, ); return result ? { diff --git a/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js b/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js index 12e279d46b..35e21efd3b 100644 --- a/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js +++ b/packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js @@ -11,6 +11,7 @@ import type { BundlerResolution, + ResolutionObservations, TransformResultDependency, } from '../../DeltaBundler/types'; import type {Reporter} from '../../lib/reporting'; @@ -38,17 +39,28 @@ type Options = Readonly<{ disableHierarchicalLookup: boolean, emptyModulePath: string, extraNodeModules: ?any, - fileSystemLookup: FileSystemLookup, + fileSystemLookup: ( + filePath: string, + observations?: ?ResolutionObservations, + ) => ReturnType, getHasteModulePath: (name: string, platform: ?string) => ?string, getHastePackagePath: (name: string, platform: ?string) => ?string, mainFields: ReadonlyArray, getPackage: (packageJsonPath: string) => ?PackageJson, - getPackageForModule: (absolutePath: string) => ?PackageForModule, + getPackageForModule: ( + absolutePath: string, + observations?: ?ResolutionObservations, + ) => ?PackageForModule, nodeModulesPaths: ReadonlyArray, preferNativePlatform: boolean, projectRoot: string, reporter: Reporter, - resolveAsset: ResolveAsset, + resolveAsset: ( + dirPath: string, + assetName: string, + extension: string, + observations?: ?ResolutionObservations, + ) => ReturnType, resolveRequest: ?CustomResolver, schemeResolvers: Readonly<{[scheme: string]: CustomResolver}>, sourceExts: ReadonlyArray, @@ -63,6 +75,11 @@ type Options = Readonly<{ unstable_resolveExportsFromPackageRoot: boolean, }>; +// Same shape for every record, to keep lookups monomorphic. +function createObservations(): ResolutionObservations { + return {existence: new Set(), content: new Set()}; +} + export class ModuleResolver { _options: Options; // A module representing the project root, used as the origin when resolving `emptyModulePath`. @@ -120,64 +137,20 @@ export class ModuleResolver { platform: string | null, resolverOptions: ResolverInputOptions, ): BundlerResolution { - const { - assetExts, - disableHierarchicalLookup, - extraNodeModules, - fileSystemLookup, - getPackage, - getPackageForModule, - mainFields, - nodeModulesPaths, - preferNativePlatform, - resolveAsset, - resolveRequest, - schemeResolvers, - sourceExts, - unstable_applyBrowserSpecAfterExports, - unstable_conditionNames, - unstable_conditionsByPlatform, - unstable_enablePackageExports, - unstable_resolveExportsFromPackageRoot, - } = this._options; + const observations = this._options.unstable_incrementalResolution + ? createObservations() + : null; try { - const result = Resolver.resolve( - { - allowHaste, - assetExts, - customResolverOptions: resolverOptions.customResolverOptions ?? {}, - dependency, - dev: resolverOptions.dev, - disableHierarchicalLookup, - extraNodeModules, - fileSystemLookup, - getPackage, - getPackageForModule, - isESMImport: dependency.data.isESMImport, - mainFields, - nodeModulesPaths, - originModulePath, - preferNativePlatform, - resolveAsset, - resolveHasteModule: (name: string) => - this._options.getHasteModulePath(name, platform), - resolveHastePackage: (name: string) => - this._options.getHastePackagePath(name, platform), - resolveRequest, - schemeResolvers, - sourceExts, - unstable_applyBrowserSpecAfterExports, - unstable_conditionNames, - unstable_conditionsByPlatform, - unstable_enablePackageExports, - unstable_resolveExportsFromPackageRoot, - unstable_logWarning: this._logWarning, - }, - dependency.name, + const result = this._resolve( + originModulePath, + dependency, + allowHaste, platform, + resolverOptions, + observations, ); - return this._getFileResolvedModule(result); + return this._getFileResolvedModule(result, observations); } catch (error) { if (error instanceof Resolver.FailedToResolvePathError) { const {candidates} = error; @@ -240,21 +213,131 @@ export class ModuleResolver { } } + /** + * Resolves a dependency, recording what it observes into `observations` if + * given. Capabilities are bound to the record, so a custom resolver's + * lookups are recorded too. + */ + _resolve( + originModulePath: string, + dependency: TransformResultDependency, + allowHaste: boolean, + platform: string | null, + resolverOptions: ResolverInputOptions, + observations: ?ResolutionObservations, + ): Resolution { + const { + assetExts, + disableHierarchicalLookup, + extraNodeModules, + fileSystemLookup, + getPackage, + getPackageForModule, + mainFields, + nodeModulesPaths, + preferNativePlatform, + resolveAsset, + resolveRequest, + schemeResolvers, + sourceExts, + unstable_applyBrowserSpecAfterExports, + unstable_conditionNames, + unstable_conditionsByPlatform, + unstable_enablePackageExports, + unstable_resolveExportsFromPackageRoot, + } = this._options; + + return Resolver.resolve( + { + allowHaste, + assetExts, + customResolverOptions: resolverOptions.customResolverOptions ?? {}, + dependency, + dev: resolverOptions.dev, + disableHierarchicalLookup, + extraNodeModules, + fileSystemLookup: + observations == null + ? fileSystemLookup + : filePath => fileSystemLookup(filePath, observations), + getPackage, + getPackageForModule: + observations == null + ? getPackageForModule + : absolutePath => getPackageForModule(absolutePath, observations), + isESMImport: dependency.data.isESMImport, + mainFields, + nodeModulesPaths, + originModulePath, + preferNativePlatform, + resolveAsset: + observations == null + ? 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), + resolveRequest, + schemeResolvers, + sourceExts, + unstable_applyBrowserSpecAfterExports, + unstable_conditionNames, + unstable_conditionsByPlatform, + unstable_enablePackageExports, + unstable_resolveExportsFromPackageRoot, + unstable_logWarning: this._logWarning, + }, + dependency.name, + platform, + ); + } + /** * TODO: Return Resolution instead of coercing to BundlerResolution here */ - _getFileResolvedModule(resolution: Resolution): BundlerResolution { + _getFileResolvedModule( + resolution: Resolution, + observations: ?ResolutionObservations, + ): BundlerResolution { switch (resolution.type) { case 'sourceFile': - return resolution; + return observations == null + ? resolution + : { + filePath: resolution.filePath, + type: 'sourceFile', + unstable_observations: observations, + }; case 'assetFiles': // FIXME: we should forward ALL the paths/metadata, // not just an arbitrary item! const arbitrary = getArrayLowestItem(resolution.filePaths); invariant(arbitrary != null, 'invalid asset resolution'); - return {filePath: arbitrary, type: 'sourceFile'}; + return observations == null + ? {filePath: arbitrary, type: 'sourceFile'} + : { + filePath: arbitrary, + type: 'sourceFile', + unstable_observations: observations, + }; case 'empty': - return this._getEmptyModule(); + const emptyModule = this._getEmptyModule(); + if (observations == null) { + return emptyModule; + } + // The empty module's resolution is cached, so merge in what it observed. + const emptyObservations = emptyModule.unstable_observations; + if (emptyObservations != null) { + for (const canonicalPath of emptyObservations.existence) { + observations.existence.add(canonicalPath); + } + for (const canonicalPath of emptyObservations.content) { + observations.content.add(canonicalPath); + } + } + return {...emptyModule, unstable_observations: observations}; case 'virtualModule': if (!this._options.unstable_allowVirtualModules) { throw new Error( @@ -275,12 +358,15 @@ export class ModuleResolver { typeof resolution.source === 'string' ? Buffer.from(resolution.source, 'utf8') : resolution.source; - return { - type: 'virtualModule', + const virtualModule = { + type: 'virtualModule' as const, filePath: deriveVirtualModulePath(virtualPath), source, sha1: crypto.createHash('sha1').update(source).digest('hex'), }; + return observations == null + ? virtualModule + : {...virtualModule, unstable_observations: observations}; default: resolution.type as empty; throw new Error('invalid type'); diff --git a/packages/metro/src/node-haste/DependencyGraph/createModuleResolver.js b/packages/metro/src/node-haste/DependencyGraph/createModuleResolver.js index 1d521aaf1f..739aeecfee 100644 --- a/packages/metro/src/node-haste/DependencyGraph/createModuleResolver.js +++ b/packages/metro/src/node-haste/DependencyGraph/createModuleResolver.js @@ -9,6 +9,7 @@ * @oncall react_native */ +import type {ResolutionObservations} from '../../DeltaBundler/types'; import type {PackageCache} from '../PackageCache'; import type {ConfigT} from 'metro-config'; import type {FileSystem, HasteMap} from 'metro-file-map'; @@ -31,8 +32,11 @@ export default function createModuleResolver({ hasteMap, packageCache, }: CreateModuleResolverOptions): ModuleResolver { - const fileSystemLookup = (filePath: string): ReturnType => { - const result = fileSystem.lookup(filePath); + const fileSystemLookup = ( + filePath: string, + observations?: ?ResolutionObservations, + ): ReturnType => { + const result = fileSystem.lookup(filePath, observations); if (result.exists) { return { exists: true, @@ -61,14 +65,21 @@ export default function createModuleResolver({ return null; } }, - getPackageForModule: (absolutePath: string) => - packageCache.getPackageForModule(absolutePath), + getPackageForModule: ( + absolutePath: string, + observations?: ?ResolutionObservations, + ) => packageCache.getPackageForModule(absolutePath, observations), mainFields: config.resolver.resolverMainFields, nodeModulesPaths: config.resolver.nodeModulesPaths, preferNativePlatform: true, projectRoot: config.projectRoot, reporter: config.reporter, - resolveAsset: (dirPath: string, assetName: string, extension: string) => { + resolveAsset: ( + dirPath: string, + assetName: string, + extension: string, + observations?: ?ResolutionObservations, + ) => { const basePath = dirPath + path.sep + assetName; const assets = [ basePath + extension, @@ -77,7 +88,7 @@ export default function createModuleResolver({ ), ] .map(assetPath => { - const result = fileSystemLookup(assetPath); + const result = fileSystemLookup(assetPath, observations); // A directory such as `node_modules/lodash.zip` is not an asset, and // must fall through to directory (package) resolution. return result.exists && result.type === 'f' ? result.realPath : null; diff --git a/packages/metro/src/node-haste/PackageCache.js b/packages/metro/src/node-haste/PackageCache.js index 58840da46e..c4fcf806a9 100644 --- a/packages/metro/src/node-haste/PackageCache.js +++ b/packages/metro/src/node-haste/PackageCache.js @@ -9,12 +9,16 @@ * @oncall react_native */ +import type {ResolutionObservations} from '../DeltaBundler/types'; import type {PackageJson} from 'metro-resolver/private/types'; import {readFileSync} from 'node:fs'; import {dirname} from 'node:path'; -type GetClosestPackageFn = (absoluteFilePath: string) => ?{ +type GetClosestPackageFn = ( + absoluteFilePath: string, + observations?: ?ResolutionObservations, +) => ?{ packageJsonPath: string, packageRelativePath: string, }; @@ -70,8 +74,11 @@ export class PackageCache { * The closest package is looked up on every call rather than remembered per * module path. Only the parsed contents of each `package.json` are cached. */ - getPackageForModule(absoluteModulePath: string): ?PackageForModule { - const closest = this.#getClosestPackage(absoluteModulePath); + getPackageForModule( + absoluteModulePath: string, + observations?: ?ResolutionObservations, + ): ?PackageForModule { + const closest = this.#getClosestPackage(absoluteModulePath, observations); if (closest == null) { return null; }