Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recursively close paths on unwatch #1388

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {
path = sysPath.resolve(path);
}

this._closePath(path);
this._closePath(path, true);

this._addIgnoredPath(path);
if (this._watched.has(path)) {
Expand Down Expand Up @@ -924,7 +924,16 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {
/**
* Closes all watchers for a path
*/
_closePath(path: Path) {
_closePath(path: Path, recursive = false) {
if (recursive) {
// Close all watchers that are descendants of the path
const descendants = this._getWatchedDir(path).getChildren();
descendants.forEach((entry) => {
const p = sysPath.join(path, entry);
this._closePath(p, recursive);
});
}

this._closeFile(path);
const dir = sysPath.dirname(path);
this._getWatchedDir(dir).remove(sysPath.basename(path));
Expand Down
17 changes: 17 additions & 0 deletions test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1829,6 +1829,23 @@ const runTests = (baseopts) => {
spy.should.not.have.been.calledWith(EV.UNLINK);
if (!macosFswatch) spy.should.have.been.calledOnce;
});
it('should emit add events for new paths if they were previously unwatched', async () => {
const spy = sinon.spy();
const watchPaths = [getFixturePath('subdir')];
const watcher = chokidar_watch(watchPaths, { ...options, ignoreInitial: false });
await waitForWatcher(watcher);
await write(getFixturePath('subdir/add.txt'), dateNow());

await delay();
watcher.unwatch(getFixturePath('subdir'));

await delay();
watcher.on(EV.ALL, spy).add(getFixturePath('subdir'));

await waitFor([spy]);
spy.should.have.been.calledWith(EV.ADD);
if (!macosFswatch) spy.should.have.been.calledTwice;
});
});
describe('env variable option override', () => {
describe('CHOKIDAR_USEPOLLING', () => {
Expand Down