diff --git a/packages/metro/src/DeltaBundler.js b/packages/metro/src/DeltaBundler.js index fe28307cd2..9bd1030617 100644 --- a/packages/metro/src/DeltaBundler.js +++ b/packages/metro/src/DeltaBundler.js @@ -17,11 +17,12 @@ import type { Options, ReadOnlyGraph, ReleasedModules, + TransformFn, } from './DeltaBundler/types'; import type EventEmitter from 'node:events'; import DeltaCalculator from './DeltaBundler/DeltaCalculator'; -import {getModuleSourcePath} from './lib/modulePath'; +import {deriveVirtualModulePath, getModuleSourcePath} from './lib/modulePath'; import debugModule from 'debug'; const debug = debugModule('Metro:DeltaBundler'); @@ -47,7 +48,8 @@ export type { */ export type DeltaBundlerOptions = Readonly<{ // Called after a graph releases modules or ends, with the outputs that live - // graphs still hold at the released paths. + // graphs still hold at the released paths. A graph releases the modules that + // its failed updates transformed once it commits an update or ends. onModulesReleased?: ?(released: ReleasedModules) => void, }>; @@ -55,6 +57,9 @@ export default class DeltaBundler { _changeEventSource: EventEmitter; _deltaCalculators: Map, DeltaCalculator> = new Map(); readonly #onModulesReleased: ?(released: ReleasedModules) => void; + // The paths each live graph has transformed since it last committed an + // update, recorded only if `onModulesReleased` is set. + #transformedPaths: Map, Set> = new Map(); constructor( changeEventSource: EventEmitter, @@ -69,6 +74,7 @@ export default class DeltaBundler { deltaCalculator.end(), ); this._deltaCalculators = new Map(); + this.#transformedPaths = new Map(); } async getDependencies( @@ -95,16 +101,33 @@ export default class DeltaBundler { entryPoints: ReadonlyArray, options: Options, ): Promise> { + const transformedPaths = new Set(); + const recordingTransform: TransformFn = (modulePath, virtualSource) => { + // Record the graph path, which for a virtual module is namespaced. + transformedPaths.add( + virtualSource?.type === 'buffer' + ? deriveVirtualModulePath(modulePath) + : modulePath, + ); + return options.transform(modulePath, virtualSource); + }; const deltaCalculator = new DeltaCalculator( new Set(entryPoints), this._changeEventSource, - options, + this.#onModulesReleased == null + ? options + : {...options, transform: recordingTransform}, ); await deltaCalculator.getDelta({reset: true, shallow: options.shallow}); const graph = deltaCalculator.getGraph(); this._deltaCalculators.set(graph, deltaCalculator); + if (this.#onModulesReleased != null) { + // The graph holds every module that its first build transformed. + transformedPaths.clear(); + this.#transformedPaths.set(graph, transformedPaths); + } return graph; } @@ -125,12 +148,17 @@ export default class DeltaBundler { throw new Error('Graph not found'); } + // A failed update throws here and releases nothing, so what it transformed + // stays shared until the graph commits an update or ends. const delta = await deltaCalculator.getDelta({reset, shallow}); + const uncommittedPaths = this.#takeUncommittedPaths(graph).filter( + modulePath => !delta.deleted.has(modulePath), + ); if (delta.reset) { // A reset delta doesn't say which modules the build released. this.#notifyModulesReleased(null); - } else if (delta.deleted.size > 0) { - this.#notifyModulesReleased(Array.from(delta.deleted)); + } else if (delta.deleted.size > 0 || uncommittedPaths.length > 0) { + this.#notifyModulesReleased([...delta.deleted, ...uncommittedPaths]); } return delta; } @@ -159,7 +187,27 @@ export default class DeltaBundler { deltaCalculator.end(); this._deltaCalculators.delete(graph); - this.#notifyModulesReleased(Array.from(graph.dependencies.keys())); + const uncommittedPaths = this.#takeUncommittedPaths(graph); + this.#transformedPaths.delete(graph); + this.#notifyModulesReleased([ + ...graph.dependencies.keys(), + ...uncommittedPaths, + ]); + } + + // Returns the paths that `graph` has transformed since it last committed an + // update and doesn't hold, such as what its failed updates transformed, and + // starts recording afresh. + #takeUncommittedPaths(graph: Graph): Array { + const transformedPaths = this.#transformedPaths.get(graph); + if (transformedPaths == null || transformedPaths.size === 0) { + return []; + } + const uncommittedPaths = Array.from(transformedPaths).filter( + modulePath => !graph.dependencies.has(modulePath), + ); + transformedPaths.clear(); + return uncommittedPaths; } // Collects the outputs that live graphs hold at `paths`, or anywhere if diff --git a/packages/metro/src/DeltaBundler/SharedTransformResults.js b/packages/metro/src/DeltaBundler/SharedTransformResults.js index 64c6cd954a..07337591f2 100644 --- a/packages/metro/src/DeltaBundler/SharedTransformResults.js +++ b/packages/metro/src/DeltaBundler/SharedTransformResults.js @@ -41,12 +41,14 @@ type Entry = Readonly<{ * about to hold. Later graphs then get a second copy of that module, until the * module changes or the building graph ends. * - * Only changes that a registered graph commits release paths. Entries for - * modules that no graph commits, i.e. those added by graphs that `DeltaBundler` - * never registers (`getDependencies` and a first build that fails) and by a - * failed update of a live graph, stay until a later result for the same module - * replaces them or a live graph releases their path. This keeps prepended - * scripts, which every graph's revision holds, shared between graphs. + * Only registered graphs release paths. A live graph's failed update releases + * nothing, so what it transformed stays shared while the error persists, and + * the graph releases those paths once it commits an update or ends. Entries + * added only by graphs that `DeltaBundler` never registers, i.e. by + * `getDependencies` and by a first build that fails, stay until a later result + * for the same module replaces them or a live graph releases their path. This + * keeps prepended scripts, which every graph's revision holds, shared between + * graphs. * * Callers must treat `output` and `dependencies` as read-only, as their types * declare, since every graph holding a result sees a write to it. diff --git a/packages/metro/src/DeltaBundler/__tests__/DeltaBundler-test.js b/packages/metro/src/DeltaBundler/__tests__/DeltaBundler-test.js index 8ecf344634..4ced122182 100644 --- a/packages/metro/src/DeltaBundler/__tests__/DeltaBundler-test.js +++ b/packages/metro/src/DeltaBundler/__tests__/DeltaBundler-test.js @@ -9,7 +9,12 @@ * @oncall react_native */ -import type {MixedOutput, Options, TransformResultDependency} from '../types'; +import type { + DeltaResult, + MixedOutput, + Options, + TransformResultDependency, +} from '../types'; import DeltaBundler from '../../DeltaBundler'; import DeltaCalculator from '../DeltaCalculator'; @@ -150,12 +155,32 @@ describe('DeltaBundler', () => { DeltaCalculator.prototype.getGraph .mockReturnValueOnce(graphA) .mockReturnValueOnce(graphB); + const transformingOptions = {...options, transform: jest.fn()}; return [ - await deltaBundler.buildGraph(graphA.entryPoints, options), - await deltaBundler.buildGraph(graphB.entryPoints, options), + await deltaBundler.buildGraph(graphA.entryPoints, transformingOptions), + await deltaBundler.buildGraph(graphB.entryPoints, transformingOptions), ]; } + // Fails the next update of the graph built last, after it transforms + // `paths`. + function failNextUpdate(paths: ReadonlyArray) { + const {transform} = jest + .requireMock('../DeltaCalculator') + .default.mock.calls.at(-1)[2]; + DeltaCalculator.prototype.getDelta.mockImplementationOnce(async () => { + await Promise.all(paths.map(modulePath => transform(modulePath, null))); + throw new Error('Update failed'); + }); + } + + const emptyDelta: DeltaResult = { + added: new Map(), + modified: new Map(), + deleted: new Set(), + reset: false, + }; + function releasedArgs() { expect(onModulesReleased).toHaveBeenCalledTimes(1); const [{paths, heldOutputs}] = onModulesReleased.mock.calls[0]; @@ -202,12 +227,7 @@ describe('DeltaBundler', () => { test('is not called after a delta that releases nothing', async () => { const [a] = await buildGraphs(); - DeltaCalculator.prototype.getDelta.mockResolvedValueOnce({ - added: new Map(), - modified: new Map(), - deleted: new Set(), - reset: false, - }); + DeltaCalculator.prototype.getDelta.mockResolvedValueOnce(emptyDelta); await deltaBundler.getDelta(a, {reset: false, shallow: false}); @@ -232,6 +252,73 @@ describe('DeltaBundler', () => { expect(heldOutputs[1]).toBe(sharedOutputInB); }); + test('is called with what failed updates transformed once the graph commits an update', async () => { + const [, b] = await buildGraphs(); + failNextUpdate(['/new', '/shared']); + + await expect( + deltaBundler.getDelta(b, {reset: false, shallow: false}), + ).rejects.toThrow('Update failed'); + expect(onModulesReleased).not.toHaveBeenCalled(); + + DeltaCalculator.prototype.getDelta.mockResolvedValueOnce(emptyDelta); + await deltaBundler.getDelta(b, {reset: false, shallow: false}); + + // The graph holds `/shared`, so only `/new` is released. + const {paths, heldOutputs} = releasedArgs(); + expect(paths).toEqual(['/new']); + expect(heldOutputs).toHaveLength(0); + + // It's released once. + onModulesReleased.mockClear(); + DeltaCalculator.prototype.getDelta.mockResolvedValueOnce(emptyDelta); + await deltaBundler.getDelta(b, {reset: false, shallow: false}); + expect(onModulesReleased).not.toHaveBeenCalled(); + }); + + test('is not called for a virtual module that the graph holds after a committed update transforms it', async () => { + const graph = { + dependencies: new Map([ + ['/c', {output: outputA}], + ['virtual:/c-virtual', {output: sharedOutputInA}], + ]), + entryPoints: ['/c'], + }; + DeltaCalculator.prototype.getGraph.mockReturnValueOnce(graph); + const c = await deltaBundler.buildGraph(graph.entryPoints, { + ...options, + transform: jest.fn(), + }); + const {transform} = jest + .requireMock('../DeltaCalculator') + .default.mock.calls.at(-1)[2]; + DeltaCalculator.prototype.getDelta.mockImplementationOnce(async () => { + // A virtual module is transformed as its source path. + await transform('/c-virtual', { + type: 'buffer', + source: Buffer.from(''), + sha1: 'sha1', + }); + return emptyDelta; + }); + + await deltaBundler.getDelta(c, {reset: false, shallow: false}); + + expect(onModulesReleased).not.toHaveBeenCalled(); + }); + + test('is called with what failed updates transformed when the graph ends', async () => { + const [, b] = await buildGraphs(); + failNextUpdate(['/new']); + await expect( + deltaBundler.getDelta(b, {reset: false, shallow: false}), + ).rejects.toThrow('Update failed'); + + deltaBundler.endGraph(b); + + expect(releasedArgs().paths).toEqual(['/b', '/shared', '/new']); + }); + test('is called with every held output after a reset delta', async () => { const [a] = await buildGraphs(); DeltaCalculator.prototype.getDelta.mockResolvedValueOnce({