diff --git a/index.js b/index.js index 71259c04..a14deeb1 100644 --- a/index.js +++ b/index.js @@ -864,6 +864,15 @@ _remove(directory, item, isDirectory) { const wasTracked = parent.has(item); parent.remove(item); + // Fixes issue #1042 -> Relative paths were detected and added as symlinks + // (https://github.com/paulmillr/chokidar/blob/e1753ddbc9571bdc33b4a4af172d52cb6e611c10/lib/nodefs-handler.js#L612), + // but never removed from the map in case the path was deleted. + // This leads to an incorrect state if the path was recreated: + // https://github.com/paulmillr/chokidar/blob/e1753ddbc9571bdc33b4a4af172d52cb6e611c10/lib/nodefs-handler.js#L553 + if (this._symlinkPaths.has(fullPath)) { + this._symlinkPaths.delete(fullPath); + } + // If we wait for this file to be fully written, cancel the wait. let relPath = path; if (this.options.cwd) relPath = sysPath.relative(this.options.cwd, path); diff --git a/test.js b/test.js index 100ef60d..b8661f66 100644 --- a/test.js +++ b/test.js @@ -2102,6 +2102,46 @@ const runTests = (baseopts) => { }); } }); + describe('reproduction of bug in issue #1024', () => { + it('should detect changes to folders, even if they were deleted before', async () => { + const id = subdirId.toString(); + const relativeWatcherDir = sysPath.join(FIXTURES_PATH_REL, id, 'test'); + const watcher = chokidar.watch(relativeWatcherDir, { + persistent: true, + }); + try { + const events = []; + watcher.on('all', (event, path) => + events.push(`[ALL] ${event}: ${path}`) + ); + const testSubDir = sysPath.join(relativeWatcherDir, 'dir'); + const testSubDirFile = sysPath.join(relativeWatcherDir, 'dir', 'file'); + + // Command sequence from https://github.com/paulmillr/chokidar/issues/1042. + await delay(); + await fs_mkdir(relativeWatcherDir); + await fs_mkdir(testSubDir); + // The following delay is essential otherwise the call of mkdir and rmdir will be equalize + await delay(300); + await fs_rmdir(testSubDir); + // The following delay is essential otherwise the call of rmdir and mkdir will be equalize + await delay(300); + await fs_mkdir(testSubDir); + await write(testSubDirFile, ''); + await delay(300); + + chai.assert.deepStrictEqual(events, [ + `[ALL] addDir: ${sysPath.join('test-fixtures', id, 'test')}`, + `[ALL] addDir: ${sysPath.join('test-fixtures', id, 'test', 'dir')}`, + `[ALL] unlinkDir: ${sysPath.join('test-fixtures', id, 'test', 'dir')}`, + `[ALL] addDir: ${sysPath.join('test-fixtures', id, 'test', 'dir')}`, + `[ALL] add: ${sysPath.join('test-fixtures', id, 'test', 'dir', 'file')}`, + ]); + } finally { + watcher.close(); + } + }); + }); }; describe('chokidar', () => {