From 4a216e5725bc7e93bd4f7fef7c8d9054530ac30d Mon Sep 17 00:00:00 2001 From: Rob Hogan <2590098+robhogan@users.noreply.github.com> Date: Mon, 9 Dec 2024 18:41:34 +0000 Subject: [PATCH 1/2] Don't throw on module collision during watch --- packages/metro-file-map/src/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/metro-file-map/src/index.js b/packages/metro-file-map/src/index.js index 479ec9b91f..b3ea907142 100644 --- a/packages/metro-file-map/src/index.js +++ b/packages/metro-file-map/src/index.js @@ -872,6 +872,7 @@ export default class FileMap extends EventEmitter { // In watch mode, we'll only warn about module collisions and we'll retain // all files, even changes to node_modules. this._options.throwOnModuleCollision = false; + hasteMap.setThrowOnModuleCollision(false); this._options.retainAllFiles = true; const hasWatchedExtension = (filePath: string) => From 7d9ca1ffffef1ca2f1da98053c143771de02fca8 Mon Sep 17 00:00:00 2001 From: Rob Hogan <2590098+robhogan@users.noreply.github.com> Date: Mon, 9 Dec 2024 19:38:46 +0000 Subject: [PATCH 2/2] Add test --- .../src/__tests__/index-test.js | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/packages/metro-file-map/src/__tests__/index-test.js b/packages/metro-file-map/src/__tests__/index-test.js index f177e5238b..549cf6d7b0 100644 --- a/packages/metro-file-map/src/__tests__/index-test.js +++ b/packages/metro-file-map/src/__tests__/index-test.js @@ -2059,6 +2059,61 @@ describe('FileMap', () => { } } + fm_it( + 'does not throw on a duplicate created at runtime even if throwOnModuleCollision: true', + async hm => { + mockFs[path.join('/', 'project', 'fruits', 'Pear.js')] = ` + // Pear! + `; + mockFs[path.join('/', 'project', 'fruits', 'another', 'Pear.js')] = ` + // Pear too! + `; + const {fileSystem} = await hm.build(); + const e = mockEmitters[path.join('/', 'project', 'fruits')]; + e.emit( + 'all', + 'change', + 'Pear.js', + path.join('/', 'project', 'fruits'), + MOCK_CHANGE_FILE, + ); + e.emit( + 'all', + 'add', + 'Pear.js', + path.join('/', 'project', 'fruits', 'another'), + MOCK_CHANGE_FILE, + ); + await new Promise((resolve, reject) => { + console.error.mockImplementationOnce(() => { + reject(new Error('should not print error')); + }); + hm.once('change', resolve); + }); + // Expect a warning to be printed, but no error. + expect(console.warn).toHaveBeenCalledWith( + expect.stringContaining( + 'metro-file-map: Haste module naming collision: Pear', + ), + ); + // Both files should be added to the fileSystem, despite the Haste + // collision + expect( + fileSystem.exists(path.join('/', 'project', 'fruits', 'Pear.js')), + ).toBe(true); + expect( + fileSystem.exists( + path.join('/', 'project', 'fruits', 'another', 'Pear.js'), + ), + ).toBe(true); + }, + { + config: { + throwOnModuleCollision: true, + }, + }, + ); + fm_it( 'recovers when the oldest version of the duplicates is fixed', async hm => {