diff --git a/index.js b/index.js index 1a2261de..8785eeca 100644 --- a/index.js +++ b/index.js @@ -65,7 +65,7 @@ const toUnix = (string) => { // TODO: this is not equal to path-normalize module - investigate why const normalizePathToUnix = (path) => toUnix(sysPath.normalize(toUnix(path))); -const normalizeIgnored = (cwd) => (path) => { +const normalizeIgnored = (cwd = '') => (path) => { if (typeof path !== 'string') return path; return normalizePathToUnix(sysPath.isAbsolute(path) ? path : sysPath.join(cwd, path)); }; @@ -608,13 +608,15 @@ _isIgnored(path, stats) { const cwd = this.options.cwd; const ign = this.options.ignored; - let ignored = ign; - if (cwd && ign) ignored = ign.map(normalizeIgnored(cwd)); + const ignored = ign && ign.map(normalizeIgnored(cwd)); const paths = arrify(ignored) .filter((path) => typeof path === 'string' && !isGlob(path)) .map((path) => path + '/**'); this._userIgnored = anymatch( - this._getGlobIgnored().concat(ignored).concat(paths) + this._getGlobIgnored() + .map(normalizeIgnored(cwd)) + .concat(ignored) + .concat(paths) ); } diff --git a/lib/nodefs-handler.js b/lib/nodefs-handler.js index 31140b67..88b52c3f 100644 --- a/lib/nodefs-handler.js +++ b/lib/nodefs-handler.js @@ -325,7 +325,7 @@ _handleFile(file, stats, initialAdd, callback) { }); // emit an add event if we're supposed to - if (!(initialAdd && this.fsw.options.ignoreInitial)) { + if (!(initialAdd && this.fsw.options.ignoreInitial) && this.fsw._isntIgnored(file)) { if (!this.fsw._throttle('add', file, 0)) return; this.fsw._emit('add', file, stats); } diff --git a/test.js b/test.js index 5cbad8c5..bba4d59e 100644 --- a/test.js +++ b/test.js @@ -1818,24 +1818,26 @@ const runTests = function(baseopts) { if (!macosFswatch) spy.should.have.been.calledOnce; }); it('should ignore unwatched paths that are a subset of watched paths', async () => { + const subdirRel = upath.relative(process.cwd(), getFixturePath('subdir')); + const unlinkFile = getFixturePath('unlink.txt'); + const addFile = getFixturePath('subdir/add.txt'); + const changedFile = getFixturePath('change.txt'); let watcher = chokidar_watch(currentDir, options); const spy = await aspy(watcher, 'all'); - await delay(); // test with both relative and absolute paths - const subdirRel = upath.relative(process.cwd(), getFixturePath('subdir')); watcher.unwatch([subdirRel, getGlobPath('unl*')]); await delay(); - await fs_unlink(getFixturePath('unlink.txt')); - await write(getFixturePath('subdir/add.txt'), Date.now()); - await write(getFixturePath('change.txt'), Date.now()); + await fs_unlink(unlinkFile); + await write(addFile, Date.now()); + await write(changedFile, Date.now()); await waitFor([spy.withArgs('change')]); await delay(300); - spy.should.have.been.calledWith('change', getFixturePath('change.txt')); - spy.should.not.have.been.calledWith('add', getFixturePath('subdir/add.txt')); - spy.should.not.have.been.calledWith('unlink'); + spy.should.have.been.calledWith('change', changedFile); + spy.should.not.have.been.calledWith('add', addFile); + spy.should.not.have.been.calledWith('unlink', unlinkFile); if (!macosFswatch) spy.should.have.been.calledOnce; }); it('should unwatch relative paths', async () => {