diff --git a/packages/metro-file-map/src/__tests__/index-test.js b/packages/metro-file-map/src/__tests__/index-test.js index d74a651906..1e73a86c05 100644 --- a/packages/metro-file-map/src/__tests__/index-test.js +++ b/packages/metro-file-map/src/__tests__/index-test.js @@ -1552,6 +1552,74 @@ describe('HasteMap', () => { expect(eventsQueue).toHaveLength(1); }); + hm_it( + 'suppresses backend symlink events if enableSymlinks: false', + async hm => { + const {fileSystem} = await hm.build(); + const fruitsRoot = path.join('/', 'project', 'fruits'); + const e = mockEmitters[fruitsRoot]; + mockFs[path.join(fruitsRoot, 'Tomato.js')] = ` + // Tomato! + `; + e.emit('all', 'change', 'Tomato.js', fruitsRoot, MOCK_CHANGE_FILE); + e.emit( + 'all', + 'change', + 'LinkToStrawberry.js', + fruitsRoot, + MOCK_CHANGE_LINK, + ); + const {eventsQueue} = await waitForItToChange(hm); + expect(eventsQueue).toEqual([ + { + filePath: path.join(fruitsRoot, 'Tomato.js'), + metadata: MOCK_CHANGE_FILE, + type: 'change', + }, + ]); + expect( + fileSystem.linkStats(path.join(fruitsRoot, 'LinkToStrawberry.js')), + ).toBeNull(); + }, + ); + + hm_it( + 'emits symlink events if enableSymlinks: true', + async hm => { + const {fileSystem} = await hm.build(); + const fruitsRoot = path.join('/', 'project', 'fruits'); + const e = mockEmitters[fruitsRoot]; + mockFs[path.join(fruitsRoot, 'Tomato.js')] = ` + // Tomato! + `; + e.emit('all', 'change', 'Tomato.js', fruitsRoot, MOCK_CHANGE_FILE); + e.emit( + 'all', + 'change', + 'LinkToStrawberry.js', + fruitsRoot, + MOCK_CHANGE_LINK, + ); + const {eventsQueue} = await waitForItToChange(hm); + expect(eventsQueue).toEqual([ + { + filePath: path.join(fruitsRoot, 'Tomato.js'), + metadata: MOCK_CHANGE_FILE, + type: 'change', + }, + { + filePath: path.join(fruitsRoot, 'LinkToStrawberry.js'), + metadata: MOCK_CHANGE_LINK, + type: 'change', + }, + ]); + expect( + fileSystem.linkStats(path.join(fruitsRoot, 'LinkToStrawberry.js')), + ).toEqual({fileType: 'l', modifiedTime: 46}); + }, + {config: {enableSymlinks: true}}, + ); + hm_it( 'emits a change even if a file in node_modules has changed', async hm => { diff --git a/packages/metro-file-map/src/index.js b/packages/metro-file-map/src/index.js index 7787dd8be4..42925b4e2a 100644 --- a/packages/metro-file-map/src/index.js +++ b/packages/metro-file-map/src/index.js @@ -943,7 +943,9 @@ export default class HasteMap extends EventEmitter { // Ignore all directory events (metadata.type === 'd' || // Ignore regular files with unwatched extensions - (metadata.type === 'f' && !hasWatchedExtension(filePath))) + (metadata.type === 'f' && !hasWatchedExtension(filePath)) || + // Don't emit events relating to symlinks if enableSymlinks: false + (!this._options.enableSymlinks && metadata?.type === 'l')) ) { return; } @@ -993,11 +995,6 @@ export default class HasteMap extends EventEmitter { const linkStats = fileSystem.linkStats(relativeFilePath); const enqueueEvent = (metadata: ChangeEventMetadata) => { - // Don't emit events relating to symlinks if enableSymlinks: false - if (!this._options.enableSymlinks && type === 'l') { - return null; - } - eventsQueue.push({ filePath: absoluteFilePath, metadata,