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

Unwatch is not unwatching folders #273

Closed
rfossella opened this issue Apr 1, 2015 · 13 comments
Closed

Unwatch is not unwatching folders #273

rfossella opened this issue Apr 1, 2015 · 13 comments

Comments

@rfossella
Copy link

I'm using chokidar on Windows 7. Installed latest version today 1.3.11

I have a long-running node process that needs to watch and unwatch different folders daily. I successfully watch a collection of folders with all proper events. However, when I try to UNWATCH folders and then WATCH new folders, the old folders (now, presumably UNWATCHED) still generate notifications. The test I currently use is to UNWATCH/WATCH after 30 seconds. When I use watch.close() I (obviously) receive NO notifications. Can someone tell what I may be missing?
Thanks.

var chokidar = require('chokidar');
var folders = ['tmp','tmp2','tmp3'];        

var watcher = chokidar.watch(folders, {
  ignored: /[\/\\]\./, persistent: true
});

watcher
    .on('add', function(path, stats) 
        { 
            console.log({type: 'File',  action: 'create', path: path , stats:stats });
        })
    .on('addDir', function(path, stats) 
        { 
            console.log({type: 'Directory',  action: 'create', path: path , stats:stats });
        })
    .on('change', function(path, stats) 
        { 
            console.log({type: 'File',  action: 'change', path: path , stats:stats });
        })
    .on('unlink', function(path, stats) 
        { 
            console.log({type: 'File',  action: 'delete', path: path , stats:stats });
        })
    .on('unlinkDir', function(path, stats) 
        { 
            console.log({type: 'Directory',  action: 'delete', path: path , stats:stats });
        })
    .on('error', function(error) 
        { 
            console.log('Error occurred ' + error);
        })
    .on('ready', function() 
        { 
            console.log('Initial scan complete. Ready for changes.', "blue");
        })

 // Unwatch/Add watch after 30 seconds 
setTimeout(function(){
    console.log('About to watch/unwatch our paths');
    watcher.add(['tmp4','tmp5']);
    watcher.unwatch(['tmp','tmp2','tmp3']);

    //watcher.close()

},30000); 

Any activity on folders ['tmp','tmp2','tmp3'] is still tracked, which I do not want nor expect.

@es128
Copy link
Contributor

es128 commented Apr 1, 2015

What is different about your situation compared with what's being done in the passing tests at https://github.com/paulmillr/chokidar/blob/master/test.js#L1171-1215 ? If you run the tests in your environment, do they pass?

@rfossella
Copy link
Author

Quite honestly, I was following the examples on github as it appeared straight-forward enough. I currently cannot run the test you directed me to since there are many other modules that I need to install. Thought this was going to be a "quickie" to get this working. I looked through the test, and with the exception of the getFixturePath function it appears the same. Still looking, but unsure why unwatch wouldn't do it.

Thanks for your input.

@rfossella
Copy link
Author

Seems like the only way I could get this to work was creating multiple watchers - e.g.

currentWatcher= chokidar.watch(['tmp','tmp2','tmp3'], 
                    {ignored: /[\/\\]\./, persistent: true});
....
....
//after timeout...
newWatcher = chokidar.watch(['tmp4','tmp5'], 
                    {ignored: /[\/\\]\./, persistent: true});
 currentWatcher.unwatch(['tmp','tmp2','tmp3']);
 currentWatcher.close();

 currentWatcher = newWatcher;    // keeps node server/watcher up

Just doesn't seem right. If anyone else can help it is appreciated.

@es128
Copy link
Contributor

es128 commented Apr 2, 2015

with the exception of the getFixturePath function it appears the same

Actually that's a good point and it may be relevant - the difference between relative and absolute paths. I'll try it out when I get a chance.

@rfossella
Copy link
Author

Thanks. I appreciate anything you can offer. Your Library is great and we'll written

Robert

On Apr 1, 2015, at 8:39 PM, Elan Shanker notifications@github.com wrote:

with the exception of the getFixturePath function it appears the same

Actually that's a good point and it may be relevant - the difference between relative and absolute paths. I'll try it out when I get a chance.


Reply to this email directly or view it on GitHub.

@es128
Copy link
Contributor

es128 commented Apr 2, 2015

I added a test using unwatch with relative paths, but it passes.

@rfossella
Copy link
Author

I've tried this in several different ways but cannot get the unwatch to happen unless I use the "two-watcher" approach I described above. Currently I'm watching local directories but will eventually need to watch network directories so I'm obviously concerned I'm missing something. Do you think it's a timing thing? Seems like the watched dependency is never broken on unwatch.

@paulmillr paulmillr added the bug label Apr 12, 2015
@rdner
Copy link

rdner commented Apr 27, 2015

It seems like I have the same issue on OS X 10.10.3, io.js v1.5.1.
I use the config below:

var CHOKIDAR_OPTIONS = {
    ignoreInitial: true,
    cwd: process.cwd(),
    ignorePermissionErrors: true
};

Then I use code like this to watch a directory.

var watcher = chokidar.watch(
  'local/relative/directory', CHOKIDAR_OPTIONS
);

After some events I "unwatch" the directory like this:

watcher.unwatch('local/relative/directory');

But when I remove the directory after I have called unwatch, I have unlink events for every file in the directory.

@skyrpex
Copy link

skyrpex commented Jul 30, 2015

I pass by to say that unwatch doesn't work to me in windows 7 neither...

@KevinOrfas
Copy link

Unwatch is not functioning for me either.

@flasaracina
Copy link

flasaracina commented Mar 4, 2020

Hello, is this still an issue?
I needed a file watcher and I found this library. I am concerned with memory leaks so I looked up for and unwatch API and I found this isssue.

Just by reading the source code it appears that the unwatch function calls other functions that uses promises making it an async method, but at the same time the close method is sync (what I mean here is that it sets this.closed = true early in a syncronous way).

so if you write

currentWatcher.unwatch(['tmp','tmp2','tmp3']);
currentWatcher.close();

the actual order of execution is

currentWatcher.close(); // the part that sets this.closed = true at least
currentWatcher.unwatch(['tmp','tmp2','tmp3']);

hence the unwatch method doesn't fire off.

Am I right? I want to use this library in a project but I am concerned about this possible bug

It has been resolved? It is still a thing?

Thanks

@paulmillr
Copy link
Owner

@flasaracina use await watcher.unwatch(), obviously. No bugs.

@flasaracina
Copy link

flasaracina commented Mar 5, 2020

@paulmillr Thanks for your quick response!
Ok, I'll try it ASAP. I know I can await an async function in JS but the thing is that looking at the source code the unwatch method doesn't return a promise that could be await-ed.

Anyway, I was concerned with the bug the OP experienced concerning the unwatch method. If it is not a thing anymore I will be more than happy to start using chokidar right now.

Keep up the good work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants