From bb63e1ced4b2cdfe5a82cc9b4cdbc95c251543bf Mon Sep 17 00:00:00 2001 From: Nick Santos Date: Mon, 1 Jun 2020 21:33:53 -0400 Subject: [PATCH] handle promise rejection when a symlink's target does not exist. Fixes https://github.com/paulmillr/chokidar/issues/955 --- lib/nodefs-handler.js | 10 +++++++++- test.js | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/nodefs-handler.js b/lib/nodefs-handler.js index 3d324751..b4cb10f8 100644 --- a/lib/nodefs-handler.js +++ b/lib/nodefs-handler.js @@ -421,7 +421,15 @@ async _handleSymlink(entry, directory, path, item) { if (!this.fsw.options.followSymlinks) { // watch symlink directly (don't follow) and detect changes this.fsw._incrReadyCount(); - const linkPath = await fsrealpath(path); + + let linkPath; + try { + linkPath = await fsrealpath(path); + } catch (e) { + this.fsw._emitReady(); + return true; + } + if (this.fsw.closed) return; if (dir.has(item)) { if (this.fsw._symlinkPaths.get(full) !== linkPath) { diff --git a/test.js b/test.js index 003a6bca..4774d1da 100644 --- a/test.js +++ b/test.js @@ -1168,6 +1168,20 @@ const runTests = (baseopts) => { spy.should.have.been.calledWith(EV_ADD, linkedDir); spy.should.have.been.calledOnce; }); + it('should survive ENOENT for missing symlinks when followSymlinks:false', async () => { + options.followSymlinks = false; + const targetDir = getFixturePath('subdir/nonexistent'); + await fs_mkdir(targetDir); + await fs_symlink(targetDir, getFixturePath('subdir/broken')); + await fs_rmdir(targetDir); + + const watcher = chokidar_watch(getFixturePath('subdir'), options); + const spy = await aspy(watcher, EV_ALL); + + spy.should.have.been.calledTwice; + spy.should.have.been.calledWith(EV_ADD_DIR, getFixturePath('subdir')); + spy.should.have.been.calledWith(EV_ADD, getFixturePath('subdir/add.txt')); + }); it('should watch symlinks within a watched dir as files when followSymlinks:false', async () => { options.followSymlinks = false; // Create symlink in linkPath