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

Already on GitHub? Sign in to your account

New subfolders aren't watched until watch task is restarted #70

Fauntleroy opened this Issue Apr 12, 2013 · 21 comments


None yet

If you watch a directory like so:

module.exports = function( grunt ){
    var pkg = grunt.file.readJSON( __dirname +'/package.json');
        watch: {
            styles: {
                files: ['assets/**/*.css'],
                tasks: []
    // The cool way to load Grunt tasks
    Object.keys( pkg.devDependencies ).forEach( function( dep ){
        if( dep.substring( 0, 6 ) === 'grunt-' ) grunt.loadNpmTasks( dep );
    grunt.event.on( 'watch', function( action, filepath ){
        console.log( 'WATCH EVENT', action, filepath );

The task will do nothing if you add a new folder, then add a new file inside of it:

  • Create assets/test/
  • Create assets/test/test_file.css
  • Nothing happens 😭

If you restart the task, you'll be able to track the changes to files within that folder (but if you create any new folders, they will still be ignored until starting it again).

OSX 10.8.2, Node 0.8.21, Grunt 0.4.1, Grunt-CLI 0.1.6


shama commented Apr 12, 2013

This is a known issue shama/gaze#14. I have a fix in the works but I was cowardly waiting for this module to make a better fix. It was just released today \o/ so it's on my radar will get fixed in gaze soon.

and checkout matchdep for an even cooler way: https://github.com/tkellen/node-matchdep ;)

+1 for this - reeeeealy need it for next version of uRequire / grunt-urequire, the Javascript module converter. Tried with version 0.4.3


steida commented May 25, 2013

Until grunt-contrib-watch got fix you can use https://github.com/Steida/grunt-este-watch.


shama commented May 26, 2013

@steida Rather than shamelessly advertising your own module on this issue tracker why don't you help the Grunt community and submit a fix for this issue? I took a quick glance at your task. It doesn't have any unit tests and is completely ignoring the Grunt API. I don't see how that is a solution to this issue.


steida commented May 26, 2013

Sorry for shameless advertising.

ad unit tests
I experimented with several approaches to have working grunt-este-watch implementation. grunt-este-watch is proof of concept, therefore no unit tests. It works well for my use case
https://github.com/Steida/este/blob/master/Gruntfile.coffee#L190 and from reasons mentioned in readme I wasn't able to use official grunt-contrib-watch.

ad completely ignoring the Grunt API
No, I am not ignoring Grunt API. I just chose different path. You probably mean that I should use arbitrary files instead of only file extensions based tasks. Again, I had to fix real issue and I succeeded. Done is better than perfect. I used file.watch api only, because it's faster. Polling of thousands files was an issue for me and my MBA battery too.

ad I don't see how that is a solution to this issue.
I have seen a lot of people with same grunt-contrib-watch issues as I had and grunt-este-watch handle files in new directories well, so that why.

ad ... why don't you help the Grunt community and submit a fix for this issue?
Because I would have to completely rewrite your task to detect Node version where fs.watch is safe with failback in fs.fileWatch. I don't have time to that, sorry.

@shama shama closed this in 757c947 Jul 11, 2013

kronion commented Mar 17, 2014

I am still having this problem today.

  jshint: {
    files: ['*.js', 'models/*.js', 'routes/*.js'],
  watch: {
    scripts: {
      files: ['<%= jshint.files %>'],
      tasks: ['jshint']
    modules: {
      files: ['node_modules/*/README*', 'node_modules/*/Readme*'],
      tasks: ['shrinkwrap']

When I install new modules with npm, nothing happens. If I restart grunt watch and change the README, the task kicks off.

OSX 10.8.4
Node.js v0.10.12
Grunt v0.4.3
Grunt-cli v0.1.12
Grunt-contrib-watch 0.5.3
with Gaze 0.4.3

jscti commented Apr 3, 2014


thardy commented Apr 5, 2014

This is a frustrating problem. Why is this issue closed? It's not working.


vladikoff commented Apr 5, 2014

Grunt-contrib-watch 0.5.3

@kronion @bixibu @thardy Are you guys using 0.5.3 or 0.6.1?


shama commented Apr 5, 2014

This works for me using this pattern 'tasks/**/*.js':

@kronion Your patterns don't appear to match folders. That is probably why it's not working for you.

@bixibu and @thardy Could you please supply more information about what you have tried? and more information about your OS? Thanks!

thardy commented Apr 6, 2014

I'm using grunt-contrib-watch 0.6.1 on Windows 7x64. I'm not having problems with my js files triggering it, but I am with html files. The grunt task in question is something like the following...

watch: {
tpls: {
files: [
tasks: [ 'html2js' ]

It's actually quite a bit more complicated. If I can get the time, I will try to reproduce with a smaller sample.

thardy commented Apr 6, 2014

I ran a couple simple scenarios. If I create a "new.tpl.html" in an existing folder (one existing at the time grunt watch was run), everything works great. I see a "File "src\app\home\new.tpl.html" added", and my html2js task gets fired off.

If I create a new folder, let's say "stuff", I see a "File 'src\app\stuff' added", and my jshint:src task is run, followed by karma:unit:run, copy:build_appjs, jshint:test, and finally karma:unit:run. This is the sequence associated with my watching of the following - files: [ 'src//*.js', '!src//.spec.js', '!src/assets/__/.js' ]. Not sure why it gets kicked off on a folder creation.

If I then create a "new.tpl.html" in the stuff folder, I see "File 'src\app\stuff' deleted". What? The jshint:src task is run, followed by karma:unit:run, copy:build_appjs, jshint:test, and finally karma:unit:run (same sequence as above). Again, what? Why is that sequence being run? It's a sequence monitoring js files.

If I create a "new.js" file in the stuff folder, I get the same "File 'src\app\stuff' deleted", followed by the exact same sequence, which is great this time around because I just added a js file.

After this, trying to create more js and tpl.html files in the stuff folder produces no response whatsoever. grunt watch is still running, but it doesn't budge when I create more files in that folder. I can go back and create a file in a pre-existing folder though and it does what it's supposed to.

I try the same thing with a "stuff2" folder. I see a File "src\app\stuff2" added. Same task sequence gets fired off

I add a "new.js", first and this time I see "File "src\app\stuff2" deleted" followed immediately by "File "src\app\stuff2\new.js" renamed". That's new. Same task sequence is run.

I accidentally shut this session down at this point and have to start a new one.

I create a stuff3 folder, seeing "File 'src\app\stuff3' added", and the same sequence as above gets executed.

I create a new.js file, same results as above. "File "src\app\stuff3" deleted" followed immediately by "File "src\app\stuff3\new.js" renamed". Same task sequence is run.

I add a "new.tpl.html" file and see "File "src\app\stuff3" deleted", then same sequence as above executed (not what's supposed to be executed).

I add a "new2.js" and I see "File "src\app\stuff3\new2.js" added" (correct message this time), followed by correct task sequence (same as above).

I add a "new2.tpl.html" and get no response at all, but I can add a new3.js, and new4.js and have it work perfectly, telling me I added new js files and executing the appropriate sequence. Adding anything ending in html continues to produce no response.

Very odd.


shama commented Apr 7, 2014

@thardy If you can write a test that demonstrates the issue I'd be happy to look into a fix. I am unfortunately not able to reproduce any of the issues you're describing here, sorry. Even an example repo that demonstrates the issues would be helpful as well. Thanks!

thardy commented Apr 7, 2014

If you have the time, you can take a look at https://github.com/thardy/generator-ngbp. After installing the yeoman generator, you can "yo ngbp" and reproduce easily. Unfortunately, the gruntfile is anything but a simple example.

If I get time, I will try to pair it down to a sample that demonstrates the core issue as well.

This issue seems to exist on grunt-contrib-watch 0.6.1, works well with grunt-contrib-watch 0.5.3

rhyek commented Sep 8, 2014

Just tried switching to 0.5.3 and I still get the issue.

@thardy I was using https://github.com/thardy/generator-ngbp, I was able to fix the bug by adding the 'index:build' task on the tasks of delta:jsrrc.

jssrc: {
files: [
'<%= app_files.js %>'
tasks: [ 'jshint:src', 'karma:unit:run', 'copy:build_appjs', 'index:build' ]

I'll go send you a merge request.

@dinevillar Your solution worked for me. Thank you!

statyan commented Nov 26, 2015

This issue still exists. Can be reproduced if 'events' in options are set to added,deleted.
Tested for 0.6.1
with config:

'watch': {
            'controllers': {
                files: ['controllers/**/*Controller.js'],
                tasks: ['gen-include-controllers'],
                options: {
                    //event: ['added', 'deleted'],

If uncomment 'event' option, watcher would not watch newly created directories.

statyan commented Nov 26, 2015

Hmm, seems to me it's not so simple. Only once i accidentally make watcher watch newly created directories. But for now it doesn't. First guess was increment Fedora fs.inotify.max_user_watches = 524288 but this does not help. Keep searching...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment