From 28a9ba4b73b140d00e44144799b05a84cf7d613d Mon Sep 17 00:00:00 2001 From: Rob Hogan <2590098+robhogan@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:47:57 +0000 Subject: [PATCH 1/2] metro-file-map: Remove TreeFS.getSerializableSnapshot https://github.com/react/metro/pull/2219 added `getSerializableState()`, which returns the file system's own data, and moved the cache write onto it. This removes `getSerializableSnapshot()`, the deep copy it replaced, which nothing in Metro calls now. Anyone calling `getSerializableSnapshot()` should migrate to `getSerializableState()`. Because it isn't a copy, it can drift as the file system changes unless it's consumed synchronously. `getSerializableState()` is on `MutableFileSystem` rather than `FileSystem`, but the file system that `FileMap.build()` returns is a `TreeFS`, which implements it. A caller that holds on to the result across an `await`, or while the file map is watching, can copy it with `structuredClone()`, which copies at least as deeply as `getSerializableSnapshot()` did. Though `metro-file-map`'s API is private, I had a search around GitHub to make sure this won't break anyone - nothing outside Metro calls it, including RN and Expo. Changelog: Internal Test plan: Needs a Meta import to check for internal callers. --- packages/metro-file-map/API.md | 1 - packages/metro-file-map/src/flow-types.js | 6 ++---- packages/metro-file-map/src/lib/TreeFS.js | 16 ---------------- .../src/lib/__tests__/TreeFS-test.js | 15 +++------------ 4 files changed, 5 insertions(+), 33 deletions(-) diff --git a/packages/metro-file-map/API.md b/packages/metro-file-map/API.md index f9415e41fe..b956aa0d17 100644 --- a/packages/metro-file-map/API.md +++ b/packages/metro-file-map/API.md @@ -182,7 +182,6 @@ interface FileSystem_2 { filesToStat: Map; }; getOrComputeSha1(file: Path): Promise; - getSerializableSnapshot(): CacheData['fileSystemData']; getSha1(file: Path): null | undefined | string; hierarchicalLookup( mixedStartPath: string, diff --git a/packages/metro-file-map/src/flow-types.js b/packages/metro-file-map/src/flow-types.js index 6dbb969016..695938e0cd 100644 --- a/packages/metro-file-map/src/flow-types.js +++ b/packages/metro-file-map/src/flow-types.js @@ -394,8 +394,6 @@ export interface FileSystem { removedFiles: Set, filesToStat: Map, }; - // A copy of the file system's data, which later changes don't affect. - getSerializableSnapshot(): CacheData['fileSystemData']; getSha1(file: Path): ?string; getOrComputeSha1(file: Path): Promise; @@ -610,8 +608,8 @@ export interface ReadonlyFileSystemChanges { export interface MutableFileSystem extends FileSystem { /** * The file system's own data, not a copy, so it changes with the file - * system. Serialize it synchronously (see `CacheManager.write`), or use - * `getSerializableSnapshot()` for a copy. + * system. Serialize it synchronously (see `CacheManager.write`), or copy it + * to keep it. */ getSerializableState(): CacheData['fileSystemData']; remove(filePath: Path, listener?: FileSystemListener): void; diff --git a/packages/metro-file-map/src/lib/TreeFS.js b/packages/metro-file-map/src/lib/TreeFS.js index c05a8088e8..0593f85fa5 100644 --- a/packages/metro-file-map/src/lib/TreeFS.js +++ b/packages/metro-file-map/src/lib/TreeFS.js @@ -206,10 +206,6 @@ export default class TreeFS implements MutableFileSystem { } } - getSerializableSnapshot(): CacheData['fileSystemData'] { - return this.#cloneTree(this.#rootNode); - } - getSerializableState(): CacheData['fileSystemData'] { return this.#rootNode; } @@ -1554,16 +1550,4 @@ export default class TreeFS implements MutableFileSystem { } return result.node; } - - #cloneTree(root: DirectoryNode): DirectoryNode { - const clone: DirectoryNode = new Map(); - for (const [name, node] of root) { - if (isDirectory(node)) { - clone.set(name, this.#cloneTree(node)); - } else { - clone.set(name, [...node]); - } - } - return clone; - } } diff --git a/packages/metro-file-map/src/lib/__tests__/TreeFS-test.js b/packages/metro-file-map/src/lib/__tests__/TreeFS-test.js index 243a62f584..a9e1b12c41 100644 --- a/packages/metro-file-map/src/lib/__tests__/TreeFS-test.js +++ b/packages/metro-file-map/src/lib/__tests__/TreeFS-test.js @@ -111,7 +111,7 @@ describe.each([['win32'], ['posix']])('TreeFS on %s', platform => { const {deserialize, serialize} = require('node:v8'); const deserialized = TreeFS.fromDeserializedSnapshot({ rootDir: p('/project'), - fileSystemData: deserialize(serialize(tfs.getSerializableSnapshot())), + fileSystemData: deserialize(serialize(tfs.getSerializableState())), processFile: () => { throw new Error('Not implemented'); }, @@ -119,15 +119,6 @@ describe.each([['win32'], ['posix']])('TreeFS on %s', platform => { expect(deserialized.exists(p('/project/foo/another.js'))).toBe(true); }); - test('getSerializableSnapshot returns a copy, getSerializableState does not', () => { - const snapshot = tfs.getSerializableSnapshot(); - const state = tfs.getSerializableState(); - expect(snapshot).toEqual(state); - tfs.addOrModify(p('/project/foo/new.js'), [0, 1, 0, null, 0, null]); - expect(snapshot).not.toEqual(tfs.getSerializableSnapshot()); - expect(state).toEqual(tfs.getSerializableSnapshot()); - }); - test('implements linkStats()', () => { expect(tfs.linkStats(p('/project/link-to-foo/another.js'))).toEqual({ fileType: 'f', @@ -2202,13 +2193,13 @@ describe('snapshot portability between operating systems', () => { processFile: () => { throw new Error('Not implemented'); }, - }).getSerializableSnapshot(); + }).getSerializableState(); const TreeFSTo = loadTreeFS(to); const loaded = TreeFSTo.fromDeserializedSnapshot({ rootDir: rootFor(to), // Snapshots are typed opaquely for the cache; this one came from - // getSerializableSnapshot, so it is a DirectoryNode. + // getSerializableState, so it is a DirectoryNode. // $FlowFixMe[incompatible-type] fileSystemData: snapshot, processFile: () => { From 67fbe6ebbebd576066c4557c86cc5bd6a4dd3b55 Mon Sep 17 00:00:00 2001 From: Rob Hogan <2590098+robhogan@users.noreply.github.com> Date: Thu, 19 Nov 2026 14:39:18 +0000 Subject: [PATCH 2/2] Test that getSerializableState returns the live tree --- packages/metro-file-map/src/lib/__tests__/TreeFS-test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/metro-file-map/src/lib/__tests__/TreeFS-test.js b/packages/metro-file-map/src/lib/__tests__/TreeFS-test.js index a9e1b12c41..1fe26c2ea1 100644 --- a/packages/metro-file-map/src/lib/__tests__/TreeFS-test.js +++ b/packages/metro-file-map/src/lib/__tests__/TreeFS-test.js @@ -119,6 +119,12 @@ describe.each([['win32'], ['posix']])('TreeFS on %s', platform => { expect(deserialized.exists(p('/project/foo/another.js'))).toBe(true); }); + test('getSerializableState returns the live tree, not a copy', () => { + const state = tfs.getSerializableState(); + tfs.addOrModify(p('/project/foo/new.js'), [0, 1, 0, null, 0, null]); + expect(state).toEqual(tfs.getSerializableState()); + }); + test('implements linkStats()', () => { expect(tfs.linkStats(p('/project/link-to-foo/another.js'))).toEqual({ fileType: 'f',