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

does not watch files if directory is empty #177

Open
zevero opened this issue Jan 30, 2015 · 13 comments
Open

does not watch files if directory is empty #177

zevero opened this issue Jan 30, 2015 · 13 comments

Comments

@zevero
Copy link

zevero commented Jan 30, 2015

When watching path/*.txt all works fine as long as there is at least one *.txt file present. If there are none, then 'added' is not triggered!

@adammockor
Copy link

+1

2 similar comments
@litzebauer
Copy link

+1

@mschae
Copy link

mschae commented Mar 3, 2015

+1

@jisaacks
Copy link

+1

This is correct. Even if the directory is not empty but does not contain matching files it will not trigger the add.

It seems the root problem is if there is nothing that matches the glob at the time it starts watching it will not detect new files.

@mahdaen
Copy link

mahdaen commented Oct 29, 2015

+1

@bradchesney79
Copy link

Would an acceptable answer be to have a hotkey and/or timer based way of reloading the watch?

Much like automatic download messages, we could put a notification in the watch message if changes are unseen. (You have seen them on the web, "If the download doesn't start, click here." or "You are being redirected to the new xyz.com homepage, if not click here."

Well, people could be prompted from the command line, if your changes aren't being detected press Ctrl + Alt + g for (if (gaze running) { stop gaze }; start gaze)...

@mikeyhew
Copy link

Has anyone figured out the cause of this bug?

@ghost
Copy link

ghost commented Nov 14, 2016

Ran into this now. Still a PITA issue 1 year later.

@Xarksass
Copy link

Xarksass commented Feb 3, 2017

I've encountered the same issue and I've found kind of a trick to get the job done.

Being new in node and not a master in javascript, I've looked at the code and try to understand it's flow. Then I've noticed something strange here (in helper.js):

// Returns boolean whether filepath is dir terminated
helper.isDir = function isDir (dir) {
  if (typeof dir !== 'string') {
    return false;
  }

  return (dir.slice(-(path.sep.length)) === path.sep);
};

This function (called into the _addToWatched function among others), will always return false (into _addToWatched()) because the dir path that is passed to the function is resolved by the path module which never add a separator at the end of a directory path ...

Maybe it would be better this way?

var endpath = dir.split(path.sep).pop();
return endpath.split('.').length === 1;

I know that's not perfect but personnaly I never put a dot in my directories names.

Anyway, I tried some configurations to watch new files in empty dir and it would seem that if you ask to watch the dir and the dir files, it's ok. e.g: if you want to watch js source files in js/src but with src as an empty dir, doing that:

Gaze(['js/src/','js/src/**/*.js'], function(err, watcher) { // what you want to do });

And the new files in src will be watched.

@bradchesney79
Copy link

@Xarksass nice detective work. I do think the "." as a delimiter is not a complete solution. I have and run into directories with that character a few times a year owing to Linux people not being adverse to doing that it seems-- still would be an improvement as the subset would go from all empty directories --> empty directories that contain a '.' .

Would "return endpath.split('.').length > 0" work?

@Xarksass
Copy link

Xarksass commented Feb 16, 2017

Thanks. In my case, I don't think I'll encounter a dir with a dot but I'll keep thinking about a better solution.

However, no "return endpath.split('.').length > 0" wouldn't work because it will always return true if there is at least one character in your string.

@Xarksass
Copy link

Your reply makes me want to find a more effective solution so ...

Add "var fs = require('fs');" (just before "var path = ..." for example).

Then replace

var endpath = dir.split(path.sep).pop();
return endpath.split('.').length === 1;

By

var stats = fs.statSync(dir);
return stats.isDirectory();

The more I learn about Node, the more I love using it ;)

@axten
Copy link

axten commented Nov 1, 2018

this also causes a bug in grunt-contrib-watch: gruntjs/grunt-contrib-watch#282

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

10 participants