From dffa25a9eef4aaf1b21675fb9691e12611441493 Mon Sep 17 00:00:00 2001 From: Rob Hogan <2590098+robhogan@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:11:29 +0000 Subject: [PATCH] metro-file-map: Walk to the parent directory once in addOrModify `addOrModify` walks to the file's parent directory, then hands the file to `bulkAddOrModify`, which walks to the same directory again. This is paid on every watched file change. This adds the file to the directory node from the first walk instead, which makes `addOrModify` about 45% faster. It also fixes the file's path when its parent is a symlink to the project root. The parent's canonical path is then `''`, and joining it to the basename gave an absolute path (`/new.js`), so the file was added outside the project, at `../../new.js` from a project at `/project/root`. @kitten spotted this in https://github.com/expo/expo/pull/52449#discussion_r4403892247. ## Benchmark AI-driven. Paired against `main`, 16 interleaved rounds, with a byte-identical copy of `main` as a noise control. | | `main` | This PR [95% CI] | Control [95% CI] | |---|---|---|---| | `addOrModify`, existing file | 1207 ns | -47.1% [-48.0, -46.4] | -0.3% [-1.2, +1.4] | | `addOrModify`, new file | 1323 ns | -44.5% [-44.8, -43.7] | +0.6% [-0.2, +1.3] | | `remove` | 710 ns | -0.9% [-2.3, -0.5] | +0.9% [-0.3, +1.8] | | Cold build | 137 ms | +0.5% [-1.0, +1.2] | +0.1% [-1.1, +0.6] | | Warm start | 133 ms | +0.6% [-1.1, +2.2] | +1.0% [-2.2, +3.1] | `bulkAddOrModify` is unchanged (0.455 ms on `main`, 0.456 ms with this PR, 200 calls in a fresh process each). ## Changelog ``` - **[Fix]**: Fix the path of a file added through a symlink to the project root ``` ## Test plan New unit test, failing on `main`. Same benchmark app and method as https://github.com/react/metro/pull/2204. Fuzzed against `main` on 20,000 random symlinked trees, comparing results, errors and change events. 16 trees differ, each adding through a symlink to the project root, where `main` adds the file outside the project. --- packages/metro-file-map/src/lib/TreeFS.js | 45 ++++++++++++++----- .../src/lib/__tests__/TreeFS-test.js | 22 +++++++++ 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/packages/metro-file-map/src/lib/TreeFS.js b/packages/metro-file-map/src/lib/TreeFS.js index 41b495029a..a3245f12f9 100644 --- a/packages/metro-file-map/src/lib/TreeFS.js +++ b/packages/metro-file-map/src/lib/TreeFS.js @@ -566,23 +566,48 @@ export default class TreeFS implements MutableFileSystem { changeListener?: FileSystemListener, ): void { const normalPath = this.#normalizePath(mixedPath); + const normalDirname = path.dirname(normalPath); // Walk the tree to find the *real* path of the parent node, creating // directories as we need. - const parentDirNode = this.#lookupByNormalPath(path.dirname(normalPath), { - ...LOOKUP_OPTIONS, - changeListener, - makeDirectories: true, - }); + const parentDirNode = this.#lookupByNormalPath( + normalDirname === '.' ? '' : normalDirname, + { + ...LOOKUP_OPTIONS, + changeListener, + makeDirectories: true, + }, + ); if (!parentDirNode.exists) { throw new Error( `TreeFS: Failed to make parent directory entry for ${mixedPath}`, ); } - // Normalize the resulting path to account for the parent node being root. - const canonicalPath = this.#normalizePath( - parentDirNode.canonicalPath + path.sep + path.basename(normalPath), - ); - this.bulkAddOrModify(new Map([[canonicalPath, metadata]]), changeListener); + // As bulkAddOrModify does for each file, without walking to the parent + // directory a second time. + const {canonicalPath: dirname, node: directoryNode} = parentDirNode; + const basename = path.basename(normalPath); + const canonicalPath = + dirname === '' ? basename : dirname + path.sep + basename; + if (!isDirectory(directoryNode)) { + throw new Error( + `TreeFS: Could not add directory ${dirname}, adding ${canonicalPath}. ` + + `${dirname} already exists in the file map as a file.`, + ); + } + if (changeListener != null) { + const existingNode = directoryNode.get(basename); + if (existingNode != null) { + invariant( + !isDirectory(existingNode), + 'Detected addition or modification of file %s, but it is tracked as a non-empty directory', + canonicalPath, + ); + changeListener.fileModified(canonicalPath, existingNode, metadata); + } else { + changeListener.fileAdded(canonicalPath, metadata); + } + } + directoryNode.set(basename, metadata); } bulkAddOrModify( 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 34fa5a91f4..db491e57a6 100644 --- a/packages/metro-file-map/src/lib/__tests__/TreeFS-test.js +++ b/packages/metro-file-map/src/lib/__tests__/TreeFS-test.js @@ -1137,6 +1137,28 @@ describe.each([['win32'], ['posix']])('TreeFS on %s', platform => { }); }); + test('adds a file through a symlink to the root at its canonical path', () => { + tfs.addOrModify(p('link-to-root'), [0, 0, 0, null, '', null]); + const fileAdded = jest.fn(); + tfs.addOrModify(p('link-to-root/new.js'), [1, 1, 0, null, 0, null], { + directoryAdded: jest.fn(), + directoryRemoved: jest.fn(), + fileAdded, + fileModified: jest.fn(), + fileRemoved: jest.fn(), + }); + expect(fileAdded).toHaveBeenCalledWith(p('new.js'), [ + 1, + 1, + 0, + null, + 0, + null, + ]); + expect(tfs.getAllFiles()).toContain(p('/project/new.js')); + expect(tfs.getAllFiles()).not.toContain(p('/new.js')); + }); + describe('bulkAddOrModify', () => { test('adds new files and modifies existing, new symlinks work', () => { tfs.bulkAddOrModify(