Skip to content

Commit 2d5e1a4

Browse files
committed
Release shared transform results after failed updates
With `transformer.unstable_shareTransformResults`, `Transformer` drops a shared result once no graph holds it, but only paths that a graph commits get released. A live graph's failed update rolls back, so the graph never holds the modules that update transformed for the first time, and nothing releases them. Eg an edit imports a new file whose own import fails to resolve, then the next edit drops the import: the new file's result stays in memory until something transforms that file again. This PR has `DeltaBundler` record the paths each live graph transforms. A failed update still releases nothing, so what it transformed stays shared with the graph's retries (and with other graphs hitting the same error) while the error persists. Once the graph next commits an update, or ends, it releases the recorded paths it doesn't hold, which drops their results unless another graph holds them. The cost is one `Set` insertion per module that an update transforms, and nothing with sharing off. Graphs that `DeltaBundler` never registers, ie from `getDependencies` or a failed first build, still don't release what they add, which keeps prepended scripts shared. Changelog: Internal Test plan: New unit tests. E2E (AI-driven): an `IncrementalBundler` with sharing on, watching a fixture. An edit imported a new file that failed to resolve its own import, so the update failed. A second edit dropped the import and the update succeeded. Building a graph of the new file then read its result from the cache store once with this PR, and not at all before it, because the shared result was still held.
1 parent 2e76f50 commit 2d5e1a4

3 files changed

Lines changed: 120 additions & 14 deletions

File tree

‎packages/metro/src/DeltaBundler.js‎

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
Options,
1818
ReadOnlyGraph,
1919
ReleasedModules,
20+
TransformFn,
2021
} from './DeltaBundler/types';
2122
import type EventEmitter from 'node:events';
2223

@@ -47,14 +48,18 @@ export type {
4748
*/
4849
export type DeltaBundlerOptions<T> = Readonly<{
4950
// Called after a graph releases modules or ends, with the outputs that live
50-
// graphs still hold at the released paths.
51+
// graphs still hold at the released paths. A graph releases the modules that
52+
// its failed updates transformed once it commits an update or ends.
5153
onModulesReleased?: ?(released: ReleasedModules<T>) => void,
5254
}>;
5355

5456
export default class DeltaBundler<T = MixedOutput> {
5557
_changeEventSource: EventEmitter;
5658
_deltaCalculators: Map<Graph<T>, DeltaCalculator<T>> = new Map();
5759
readonly #onModulesReleased: ?(released: ReleasedModules<T>) => void;
60+
// The paths each live graph has transformed since it last committed an
61+
// update, recorded only if `onModulesReleased` is set.
62+
#transformedPaths: Map<Graph<T>, Set<string>> = new Map();
5863

5964
constructor(
6065
changeEventSource: EventEmitter,
@@ -69,6 +74,7 @@ export default class DeltaBundler<T = MixedOutput> {
6974
deltaCalculator.end(),
7075
);
7176
this._deltaCalculators = new Map();
77+
this.#transformedPaths = new Map();
7278
}
7379

7480
async getDependencies(
@@ -95,16 +101,28 @@ export default class DeltaBundler<T = MixedOutput> {
95101
entryPoints: ReadonlyArray<string>,
96102
options: Options<T>,
97103
): Promise<Graph<T>> {
104+
const transformedPaths = new Set<string>();
105+
const recordingTransform: TransformFn<T> = (modulePath, virtualSource) => {
106+
transformedPaths.add(modulePath);
107+
return options.transform(modulePath, virtualSource);
108+
};
98109
const deltaCalculator = new DeltaCalculator(
99110
new Set(entryPoints),
100111
this._changeEventSource,
101-
options,
112+
this.#onModulesReleased == null
113+
? options
114+
: {...options, transform: recordingTransform},
102115
);
103116

104117
await deltaCalculator.getDelta({reset: true, shallow: options.shallow});
105118
const graph = deltaCalculator.getGraph();
106119

107120
this._deltaCalculators.set(graph, deltaCalculator);
121+
if (this.#onModulesReleased != null) {
122+
// The graph holds every module that its first build transformed.
123+
transformedPaths.clear();
124+
this.#transformedPaths.set(graph, transformedPaths);
125+
}
108126
return graph;
109127
}
110128

@@ -125,12 +143,17 @@ export default class DeltaBundler<T = MixedOutput> {
125143
throw new Error('Graph not found');
126144
}
127145

146+
// A failed update throws here and releases nothing, so what it transformed
147+
// stays shared until the graph commits an update or ends.
128148
const delta = await deltaCalculator.getDelta({reset, shallow});
149+
const uncommittedPaths = this.#takeUncommittedPaths(graph).filter(
150+
modulePath => !delta.deleted.has(modulePath),
151+
);
129152
if (delta.reset) {
130153
// A reset delta doesn't say which modules the build released.
131154
this.#notifyModulesReleased(null);
132-
} else if (delta.deleted.size > 0) {
133-
this.#notifyModulesReleased(Array.from(delta.deleted));
155+
} else if (delta.deleted.size > 0 || uncommittedPaths.length > 0) {
156+
this.#notifyModulesReleased([...delta.deleted, ...uncommittedPaths]);
134157
}
135158
return delta;
136159
}
@@ -159,7 +182,27 @@ export default class DeltaBundler<T = MixedOutput> {
159182
deltaCalculator.end();
160183

161184
this._deltaCalculators.delete(graph);
162-
this.#notifyModulesReleased(Array.from(graph.dependencies.keys()));
185+
const uncommittedPaths = this.#takeUncommittedPaths(graph);
186+
this.#transformedPaths.delete(graph);
187+
this.#notifyModulesReleased([
188+
...graph.dependencies.keys(),
189+
...uncommittedPaths,
190+
]);
191+
}
192+
193+
// Returns the paths that `graph` has transformed since it last committed an
194+
// update and doesn't hold, such as what its failed updates transformed, and
195+
// starts recording afresh.
196+
#takeUncommittedPaths(graph: Graph<T>): Array<string> {
197+
const transformedPaths = this.#transformedPaths.get(graph);
198+
if (transformedPaths == null || transformedPaths.size === 0) {
199+
return [];
200+
}
201+
const uncommittedPaths = Array.from(transformedPaths).filter(
202+
modulePath => !graph.dependencies.has(modulePath),
203+
);
204+
transformedPaths.clear();
205+
return uncommittedPaths;
163206
}
164207

165208
// Collects the outputs that live graphs hold at `paths`, or anywhere if

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ type Entry<T> = Readonly<{
4141
* about to hold. Later graphs then get a second copy of that module, until the
4242
* module changes or the building graph ends.
4343
*
44-
* Only changes that a registered graph commits release paths. Entries for
45-
* modules that no graph commits, i.e. those added by graphs that `DeltaBundler`
46-
* never registers (`getDependencies` and a first build that fails) and by a
47-
* failed update of a live graph, stay until a later result for the same module
48-
* replaces them or a live graph releases their path. This keeps prepended
49-
* scripts, which every graph's revision holds, shared between graphs.
44+
* Only registered graphs release paths. A live graph's failed update releases
45+
* nothing, so what it transformed stays shared while the error persists, and
46+
* the graph releases those paths once it commits an update or ends. Entries
47+
* that only graphs `DeltaBundler` never registers add, i.e. those of
48+
* `getDependencies` and of a first build that fails, stay until a later result
49+
* for the same module replaces them or a live graph releases their path. This
50+
* keeps prepended scripts, which every graph's revision holds, shared between
51+
* graphs.
5052
*
5153
* Callers must treat `output` and `dependencies` as read-only, as their types
5254
* declare, since every graph holding a result sees a write to it.

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

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
* @oncall react_native
1010
*/
1111

12-
import type {MixedOutput, Options, TransformResultDependency} from '../types';
12+
import type {
13+
DeltaResult,
14+
MixedOutput,
15+
Options,
16+
TransformResultDependency,
17+
} from '../types';
1318

1419
import DeltaBundler from '../../DeltaBundler';
1520
import DeltaCalculator from '../DeltaCalculator';
@@ -150,12 +155,32 @@ describe('DeltaBundler', () => {
150155
DeltaCalculator.prototype.getGraph
151156
.mockReturnValueOnce(graphA)
152157
.mockReturnValueOnce(graphB);
158+
const transformingOptions = {...options, transform: jest.fn()};
153159
return [
154-
await deltaBundler.buildGraph(graphA.entryPoints, options),
155-
await deltaBundler.buildGraph(graphB.entryPoints, options),
160+
await deltaBundler.buildGraph(graphA.entryPoints, transformingOptions),
161+
await deltaBundler.buildGraph(graphB.entryPoints, transformingOptions),
156162
];
157163
}
158164

165+
// Fails the next update of the graph built last, after it transforms
166+
// `paths`.
167+
function failNextUpdate(paths: ReadonlyArray<string>) {
168+
const {transform} = jest
169+
.requireMock('../DeltaCalculator')
170+
.default.mock.calls.at(-1)[2];
171+
DeltaCalculator.prototype.getDelta.mockImplementationOnce(async () => {
172+
await Promise.all(paths.map(modulePath => transform(modulePath, null)));
173+
throw new Error('Update failed');
174+
});
175+
}
176+
177+
const emptyDelta: DeltaResult<MixedOutput> = {
178+
added: new Map(),
179+
modified: new Map(),
180+
deleted: new Set(),
181+
reset: false,
182+
};
183+
159184
function releasedArgs() {
160185
expect(onModulesReleased).toHaveBeenCalledTimes(1);
161186
const [{paths, heldOutputs}] = onModulesReleased.mock.calls[0];
@@ -232,6 +257,42 @@ describe('DeltaBundler', () => {
232257
expect(heldOutputs[1]).toBe(sharedOutputInB);
233258
});
234259

260+
test('is called with what failed updates transformed once the graph commits an update', async () => {
261+
const [, b] = await buildGraphs();
262+
failNextUpdate(['/new', '/shared']);
263+
264+
await expect(
265+
deltaBundler.getDelta(b, {reset: false, shallow: false}),
266+
).rejects.toThrow('Update failed');
267+
expect(onModulesReleased).not.toHaveBeenCalled();
268+
269+
DeltaCalculator.prototype.getDelta.mockResolvedValueOnce(emptyDelta);
270+
await deltaBundler.getDelta(b, {reset: false, shallow: false});
271+
272+
// The graph holds `/shared`, so only `/new` is released.
273+
const {paths, heldOutputs} = releasedArgs();
274+
expect(paths).toEqual(['/new']);
275+
expect(heldOutputs).toHaveLength(0);
276+
277+
// It's released once.
278+
onModulesReleased.mockClear();
279+
DeltaCalculator.prototype.getDelta.mockResolvedValueOnce(emptyDelta);
280+
await deltaBundler.getDelta(b, {reset: false, shallow: false});
281+
expect(onModulesReleased).not.toHaveBeenCalled();
282+
});
283+
284+
test('is called with what failed updates transformed when the graph ends', async () => {
285+
const [, b] = await buildGraphs();
286+
failNextUpdate(['/new']);
287+
await expect(
288+
deltaBundler.getDelta(b, {reset: false, shallow: false}),
289+
).rejects.toThrow('Update failed');
290+
291+
deltaBundler.endGraph(b);
292+
293+
expect(releasedArgs().paths).toEqual(['/b', '/shared', '/new']);
294+
});
295+
235296
test('is called with every held output after a reset delta', async () => {
236297
const [a] = await buildGraphs();
237298
DeltaCalculator.prototype.getDelta.mockResolvedValueOnce({

0 commit comments

Comments
 (0)