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 => { 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) =>