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(