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 files in empty folder not watched #166

Open
markgoodyear opened this Issue Jul 21, 2013 · 117 comments

Comments

Projects
None yet

Hi,

I'm trying to set up watching Sass files and compiling them. Everything works great appart from when creating a new empty directory (or using an existing one which empty), then adding in a new file. Using grunt watch --verbose shows that the folder gets added, but any new files inside aren't recognised. If the folder already has a file inside, new files are recognised.

Is this a known issue?

A stripped down version of my Gruntfile.js for the two tasks involved is:

module.exports = function(grunt) {

  // Initialize Grunt
  grunt.initConfig({

    // Read package.json
    pkg: grunt.file.readJSON('package.json'),

    // Project meta
    meta: {

      // Filepaths
      dir: {
        js: 'js',
        css: 'css',
        sass: 'css/sass',
        img: 'img'
      }
    },

    // Sass
    sass: {
      dist: {
        options: {
          style: 'expanded'
        },
        files: {
          '<%= meta.dir.css %>/main.css': '<%= meta.dir.sass %>/main.scss'
        }
      }
    },

    // Watch
    watch: {
      sass: {
        files: '<%= meta.dir.sass %>/**/*',
        tasks: ['sass'],
      },
    }
  });

  // Load
  grunt.loadNpmTasks('grunt-contrib-sass');
  grunt.loadNpmTasks('grunt-contrib-watch');

  // Default task
  grunt.registerTask('default', ['sass']);

};

EDIT
Just thought I should mention I'm running version 0.5.1.

Owner

shama commented Jul 21, 2013

This issue should be fixed in >=0.5.0. Could you try npm cache clean && rm -rf node_modules/grunt-contrib-watch && npm install grunt-contrib-watch to ensure it's grabbing the latest version of gaze? Thanks!

Hey @shama, I just tried that and it's still doing the same unfortunately, it did show that gaze is 0.4.1.

Is there anything else I could try, or anything that could help find the issue?

Cheers!

Owner

shama commented Jul 22, 2013

Hmm which version of node.js and which os?

I'm running:

  • Node v0.10.13
  • npm 1.3.4
  • grunt-cli v0.1.9
  • grunt v0.4.1
  • Mac OSX 10.8.4

Same here. As far as I can see at least one file following the pattern in files must exist to trigger the watch event. So if target files is something like src/**/*.scss, adding a new .scss file to:

  • Empty dir doesn't work
  • dir with file dir/empty doesn't work
  • dir with file dir/file.scss works

I already updated to latest grunt-contrib-watch. Node v0.10.12 & Mac OSX 10.8.4.

I can confirm what @luissquall describes is exact behavior I'm seeing.

Just wondering if anyone's had chance to have a look at this? Would be awesome to automatically watch new files in empty folders.

@shama — If theres anything I can do to that could help find the issue just let me know.

mnoble01 commented Aug 7, 2013

+1, I can confirm the same behavior as described by @luissquall

  • Node v0.10.15
  • npm 1.3.5
  • grunt 0.4.1
  • grunt-contrib-watch 0.5.1 (gaze 0.4.1)
  • Mac OSX 10.8.4
Owner

shama commented Aug 7, 2013

Thanks everyone! I'll take a look when I get a chance. I have tests for this in gaze so I'm not sure why it isn't working here. We should add a test case for it here and work backwards to figure out why.

mnoble01 commented Aug 7, 2013

Update: the dir/empty case also occurs when dir doesn't have any files, but may contain folders

Any progress made on this one? I'm having the same issue on osx 10.8.4.

Owner

shama commented Aug 16, 2013

I looked into it briefly, no real progress yet. Will look into more soon.

@shama — If you need anything testing to help fix it I can try help out.

In gaze.js, how about replacing:

    // If file was added
    current.filter(function(file) {
      return previous.indexOf(file) < 0;
    }).forEach(function(file) {
      // Is it a matching pattern?
      var relFile = path.join(relDir, file);
      // Add to watch then emit event
      self._internalAdd(relFile, function() {
        self.emit('added', path.join(dir, file));
      });
    });

with

    // If file was added
    current.filter(function(file) {
      return previous.indexOf(file) < 0;
    }).forEach(function(file) {
      // Is it a matching pattern?
      var relFile = path.join(relDir, file);
      var absFile = fs.realpathSync(relFile); //ADDED
      // Add to watch then emit event
        self._internalAdd(absFile, function() { //MODIFIED
        self.emit('added', path.join(dir, file));
      });
    });

?

minimatch.match doesn't seem to be able to handle relative paths. I can submit this as a pull request if it makes sense.

I tried the above code change against the gaze project's tests. The following tests failed:

watchTest.js::addedEmitInSubFolders()
matchingTest.js::addedLater()

ideas?

Environment:
OSX 10.8.4
Node 0.10.15
Gaze 0.4.1
Grunt 0.4.1
npm 1.3.5

Owner

shama commented Aug 17, 2013

How I usually go about these things is to write a new test case that matches the desired behavior. Then adjust the code until the new test passes along with the existing tests. This way we're sure we're working towards the fix we want without breaking any previous fixes made.

I can jive with that. In the meantime, would you have any idea why those 2 particular test cases would be failing with the above code change?

Okay, I figured out what's causing my issue. I was doing the following:

gaze('/Users/brett/Desktop/gazetestfolder/**/*', function(err, watcher) { ... })

i.e. I was using an absolute path instead of a relative path. The above invocation would result in newly created files in /gazetestfolder not registered.

If I use a relative path: gaze('../gazetestfolder/**/*', function(err, watcher) { ... }), then the new file additions to /gazetestfolder are registered.

Is this the intended behavior of gaze?

NB: my case is different than the case that @markgoodyear reported. I tried out his case (making a new directory then adding a file to it) and I do observe the same behavior that he has observed (new files added to that directory are not registered).

Maybe I should open up a separate issue for the absolute path stuff?

Owner

shama commented Aug 18, 2013

Yes. There is an open issue for that here: shama/gaze#41 But it is low priority for me though as you can set the cwd option if you really need to pass absolute paths now.

Owner

shama commented Aug 18, 2013

@markgoodyear @luissquall @mnoble01 Out of curiosity, are you using absolute paths as well?

Hey @shama,

I've not had chance to test the above, however I do use relative paths in my Gruntfile, if that's what you're meaning?

I'll be able to check what @brett-shwom suggested later today (#166 (comment)).

Cheers

@markgoodyear, I don't think that my suggestion will work for you. When I added that suggestion, I was under the impression that you and I were having the same issue. It turns out that I conflated our issues: I was using absolute paths and so no file creation events were getting triggered, whereas your issue was that file creation events weren't getting triggered when files were created in a folder that was created after the grunt watch task was kicked off. The code change I suggested only seems to add support file creation events that do not occur in new folders when an absolute path is specified in gaze().

@shama, I'm also using relative paths in my Gruntfile...

+1 I'm also running into this issue with new files not being detected (although I hadn't linked it to empty directories)

+1 .. same issue with latest libraries

@pkieltyka what issue are you having?

@brett-shwom I'm still having the same issue as my original post. New files in empty folders do not get picked up when using grunt watch. New folders work, just not a new file in an empty folder.

Yea exactly.. I can see watch sees the new folder, but it should then watch the files in that directory as well.. if they match the glob pattern..

On 2013-09-28, at 1:18 PM, Mark Goodyear notifications@github.com wrote:

@brett-shwom I'm still having the same issue as my original post. New files in empty folders do not get picked up when using grunt watch. New folders work, just not a new file in an empty folder.


Reply to this email directly or view it on GitHub.

@brett-shwom thanks for the PR.. did you test it out btw? .. I did some debugging as well.. and one thing that came to mind is that perhaps it's actually a file glob matching issue.

For example, in my Gruntfile I have:

watch: {
  coffee: {
    files: ['src/**/*.coffee'],
    tasks: ['coffee:build']
  }
}

.. pretty standard. Now, if I create a new directory under src/, I can see the message ">> File 'src/x' added." where "x" is the new empty directory. That is followed by Grunt running my "coffee:build" task.. however, it shouldn't actually run that task because that doesn't match the pattern of *.coffee ... this is a definite problem in the pattern matching that is probably throwing things off as well.

I think it should actually do nothing when a new directory is added, other then perhaps add it to the list of directories to watch because of the ** match, and once it see's a .coffee file that matches the full pattern, then run the associated tasks.


Btw, I found another bug while I was fiddling around. When deleting an entire subfolder that contains watched files, the "watch" task completely misses that the folder and its files were deleted and doesn't even send the "deleted" target to grunt.event.on("watch",..). But, if you delete each watched file in a subfolder individually, it will send the "deleted" event for each file properly, as well execute the associated tasks.

@pkieltyka Hey man,

Yeah, I did test it.

In addition to modifying the mkdirThenAddFile nodeunit test (part of the https://github.com/shama/gaze repo), I also created a sample script that listens to a events on a folder named /test. I ran the sample script, then created a folder, then created a .js file within that folder, and finally observed that a file added event was triggered.

If you want to give that sample script a shot,

  1. git clone git@github.com:brett-shwom/gaze.git
  2. git checkout track-file-additions-in-newly-created-folders
  3. npm install
  4. place the below code in [your-file-name].js
  5. ensure that the line require('../lib/gaze') is pointing to the gaze script in my branch
  6. mkdir [wherever you want]/test
  7. ensure that the line gaze('test/**/*.js', is pointing to [wherever you want]/test
  8. node [your-file-name].js
  9. mkdir [wherever you want]/test/a
  10. touch [wherever you want]/test/a/r.js
  11. observe [wherever you want]/test/a/r.js was added
var gaze = require('../lib/gaze'); 

// Watch all .js files/dirs in process.cwd()
gaze('test/**/*.js', function(err, watcher) {
  // Files have all started watching
  // watcher === this

  // Get all watched files
  console.log(this.watched());

  // On file changed
  this.on('changed', function(filepath) {
    console.log(filepath + ' was changed');
  });

  // On file added
  this.on('added', function(filepath) {
    console.log(filepath + ' was added');
  });

  // On file deleted
  this.on('deleted', function(filepath) {
    console.log(filepath + ' was deleted');
  });

  // Get watched files with relative paths
  console.log(this.relative());
});

Also, you @pkieltyka wrote: ".. pretty standard. Now, if I create a new directory under src/, I can see the message ">> File 'src/x' added." where "x" is the new empty directory. That is followed by Grunt running my "coffee:build" task.. however, it shouldn't actually run that task because that doesn't match the pattern of *.coffee ... this is a definite problem in the pattern matching that is probably throwing things off as well."

Maybe file that as a separate bug?

@pkieltyka you wrote: "Btw, I found another bug while I was fiddling around. When deleting an entire subfolder that contains watched files, the "watch" tasks completely misses it and doesn't even send the "deleted" target to grunt.event.on("watch",..). This means, But, if you delete each watched file in a subfolder individually, it will send the "deleted" event for each file properly, as well execute the associated tasks."

Perhaps, create a new ticket for that as well :)

floz commented Oct 3, 2013

Same issue here, new files are not tracked.
Gaze detect the new file (Gaze.prototype._watchDir is triggered with file creation), but the forEach loop (in Gaze.prototype.relative) is executed with the values inside this._watched.
The problem comes from here.

The this._watched[dir] content should be refreshed by comparing the new folder content.

I have a pull request in to fix this: shama/gaze#53

We're waiting on @shama to approve/deny.

floz commented Oct 3, 2013

Hmmm... With your fix my new files are still not watched.
But it trigger the watch event when creating a new folder.
Is that what it's supposed to do?

How did you test the fix?

Did you check out my fork of gaze https://github.com/brett-shwom/gaze , switch to the track-file-additions-in-newly-created-folders branch, and run a test against that?

If so, could you provide me with the steps you took in your test?

floz commented Oct 3, 2013

I've took the gaze.js from your branch.

And, in a personal task, with that configuration of GruntFile:

watch: {
    coffee: {
        files: [ "./src/coffee/**/*.coffee" ],
        tasks: [ "coffee:compile" ]
    }
}

Then:
Adding a file in ./src/coffee => nothing appear in the terminal
Adding a folder named "test" in ./src/coffee => trigger the coffee:compile task
Adding a file in ./src/coffee/test => nothing appear in the terminal

@floz I looked into your case. The issue seems to be with the ./ in ./src/coffee/**/*.coffee

If you replace ./src/coffee/**/*.coffee with src/coffee/**/*.coffee

I think you'll see that the fix works. Not ideal, but at least you have a workaround.

floz commented Oct 3, 2013

Good catch... !
Thanks for the feedback, and interesting case :)

Hi! I have the same issue on Windows 7. When add new file to an empty folder(inside of watching dir), watch task does not launch.

fi5u commented Oct 17, 2013

Hi, I think I have the same issue. For me I am trying to get watch to watch for any changes to HTML files. This works if the file's directory existed at the time watch was called, but if the directory is created after watch was called, no HTML inside is watched.
My problem is explained in detail in my StackOverflow question:
Grunt globbing pattern not working as expected within grunt-contrib-watch
Gruntfile: Gruntfile.js
Node: 0.10.15
OS: OSX 10.8.5

I have a pending pull request to fix this issue. It's on @shama to approve :)

Owner

shama commented Oct 17, 2013

@brett-shwom Thanks again for the PR although Im not sure if it fixes this issue. It should handle grunt.file.write('new_dir/tmp.js', ''); the same as fs.mkdirSync('new_dir'); fs.writeFileSync('new_dir/tmp.js'); and doesn't should we should figure out why before changing the test.

Also to everyone reporting they have the same issue, could you please post your Gruntfile, node and operating system version and what you tried? Sometimes it may appear to be the same issue but after further reviewal; becomes an entirely different issue (and one that may have previously been solved). Thanks!

@shama Are you saying that the fix that I provided fails for grunt.file.write('new_dir/tmp.js', ''); but does not fail for fs.mkdirSync('new_dir'); fs.writeFileSync('new_dir/tmp.js'); ?

@shama That's easily tested :) If you revert the test in watch.js to use grunt.file.write('new_dir/tmp.js', ''); while keeping the gaze.js you'll see that the tests still pass.

@shama given my latest comment, what else might I be able to do to convince you to accept the PR?

Owner

shama commented Oct 18, 2013

@brett-shwom Add a test case that shows the error being fixed as opposed to editing the existing one. As we should support both of those use cases if they behave differently.

This line in your PR:

fs.mkdirSync('new_dir'); //fs.mkdirSync([folder]) seems to behave differently than grunt.file.write('[folder]/[file]')

tells me that I need to look into why you said they behave differently before merging. Which is why I left the PR open.

@shama the test case I wrote shows the error being fixed. I don't really understand what you mean.

@shama the original test case with grunt.file.write('new_dir/tmp.js', ''); was flawed.

Owner

shama commented Oct 18, 2013

@brett-shwom Why was it flawed? Red flags go up for me any time someone edits or deletes a test case.

I was under the assumption that grunt.file.write('new_dir/tmp.js', ''); did the exact same thing as fs.mkdirSync('new_dir'); fs.writeFileSync('new_dir/tmp.js');. Your PR says that isn't true. In which case we should add a test case that proves both scenarios rather than change the existing test.

Fair enough. Test added.

Owner

shama commented Oct 19, 2013

Thanks again @brett-shwom!

You're right, point me in the relevant direction, and I'll see what it
would take to implement this correctly.

In addition to the bug related to new files, the other piece that seems
broken is the chaining together of more than one watch tasks.

For example, a common flow is to transpile cs -> js, then browserify the js
files -> bundle.js, then -> live reload.

The current implementation seems to fire all of the tasks in parallel, such
that the live-reload occurs before the bundle is ready.

i.e.: it might be worth looking into some sort of data flow/reactive
dependency management system.

DM

On Wed, Nov 27, 2013 at 7:25 PM, brett-shwom notifications@github.comwrote:

@davidmoshal https://github.com/davidmoshal not to be a jerk or
anything, but grunt-contrib-watch isnt a "product" that we consume, it's an
organism that we all feed. I understand that you're frustrated that
grunt-contrib-watch isn't working but instead of broadcasting to everyone
that you're moving off of it, could you try instead to fix it?


Reply to this email directly or view it on GitHubhttps://github.com/gruntjs/grunt-contrib-watch/issues/166#issuecomment-29437098
.

Owner

shama commented Nov 28, 2013

Thanks for the info @luissquall. Although I'm having trouble reproducing the failing tests, I'm on OSX 10.9 and all tests pass (with the latest master branch): screen shot 2013-11-27 at 8 54 42 pm

I wrote a test case for this and it works if the pattern matches a single nested file: add/**/*.js and add/sub.js is added. Although it doesn't work if add/sub/sub.js is added. This should be working as we have tests for it upstream in gaze so not sure why it's still failing here.

Reopening and will try to look into this soon. Thanks!

@shama shama reopened this Nov 28, 2013

When creating a sub-folder and then adding a file to it, the new file doesn't get "watched".
When creating a file in an existing folder/sub-folder, the new file doesn't get "watched".

  • node v0.10.22
  • npm v1.3.14
  • grunt-cli v0.1.11
  • grunt v0.4.2
  • grunt-contrib-watch v0.5.3
  • Windows 7 (64-bit)

Gruntfile.js:

module.exports = function(grunt){
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        concat: {
            options: {
                process: function(src, filepath){
                    return '\n\n\n/* Source: ' + filepath + '*/\n\n' + src;
                }
            },
            js: {
                src: [
                    'static/storm-v01/js/app.js',
                    'static/storm-v01/js/**/*.js'
                ],
                dest: 'static/storm-v01/dist/storm.js'
            },
            scss: {
                src: [
                    'static/storm-v01/scss/app.scss',
                    'static/storm-v01/scss/**/*.scss'
                ],
                dest: 'static/storm-v01/dist/storm.scss'
            }
        },
        uglify: {
            build: {
                src: 'static/storm-v01/dist/storm.js',
                dest: 'static/storm-v01/dist/storm.min.js'
            }
        },
        sass: {
            uncompressed: {
                options: {
                    style: 'expanded' //'nested, compact, compressed, expanded'
                },
                files: {
                    'static/storm-v01/dist/storm.css': 'static/storm-v01/dist/storm.scss'
                }
            },
            dist: {
                options: {
                    style: 'compressed' //'nested, compact, compressed, expanded'
                },
                files: {
                    'static/storm-v01/dist/storm.min.css': 'static/storm-v01/dist/storm.scss'
                }
            }
        },
        watch: {
            js: {
                files: 'static/storm-v01/js/**/*.js',
                tasks: ['concat:js'], 
                options: {
                    atBegin: true,
                    event: ['added', 'deleted', 'changed']
                }
            },
            distjs: {
                files: 'static/storm-v01/dist/storm.js',
                tasks: ['uglify'],
                options: {
                    atBegin: true,
                    event: ['added', 'deleted', 'changed']
                }
            },
            scss: {
                files: 'static/storm-v01/scss/**/*.scss',
                tasks: ['concat:scss'],
                options: {
                    atBegin: true,
                    event: ['added', 'deleted', 'changed']
                }
            },
            distscss: {
                files: 'static/storm-v01/dist/storm.scss',
                tasks: ['sass'],
                options: {
                    atBegin: true,
                    event: ['added', 'deleted', 'changed']
                }
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-sass');
    grunt.loadNpmTasks('grunt-contrib-watch');

    grunt.registerTask('default', ['watch']);
};

I am having the same issue as @trevorhreed.

  • New folders are not watched.
  • New files in those new-er folders are not watched.
  • New files in existing folders are not watched.
  • New files in existing folders with existing watched files, are not watched.

I solved these issues by moving to chokidar btw.

Owner

shama commented Dec 19, 2013

@davidmoshal Nice! Give this one a try too: https://npmjs.org/package/watchr It's pretty good. Feel free to let us know if you find another watch library that will pass all our tests here. New issues are only solved by not breaking old ones. ;)

I was able to solve the issue of not detecting new files without moving away from gaze.
See the comment here: dbankier/TiShadow#166 (comment) and shama's response.

@dbankier I tried getting that to work, but I am having an issue that doesn't quite make sense. Perhaps someone will have an idea.

This currently works great except for the watching of new files :

watch: {
  less: {
    // Which files to watch (all .less files recursively)
    files: ['/space/vhosts/www.derp.com/codebase/*/*/_less/*.less'],
    tasks: ['newer:less'],
    options: {
      spawn: false
    }
  },
}

Changing to this as per your posting gives me a big error :

watch: {
  less: {
    // Which files to watch (all .less files recursively)
    files: [{
        cwd: '/space/vhosts/www.derp.com/codebase/',     // Src matches are relative to this path.
        src: ['*/*/_less/*.less']                              // Actual pattern(s) to match.
    }],
    tasks: ['newer:less'],
    options: {
      spawn: false
    }
  },
}

The errors are ...a bit nutter.

*note lots of these looping as if calling itself*
Running "watch" task
Waiting...Verifying property watch exists in config...OK
Verifying property watch.less.files exists in config...OK
Warning: Object #<Object> has no method 'indexOf'

Running "watch" task
Waiting...Fatal error: Maximum call stack size exceeded

Ideas?

Owner

shama commented Dec 19, 2013

@SpencerCarstens cwd is an option of the watch task. It is a bit confusing as the watch task isn't a multitask (so it doesn't take the same src/dest config as many of the other Grunt tasks). Try this instead:

watch: {
  less: {
    files: ['*/*/_less/*.less'],
    tasks: ['newer:less'],
    options: {
      spawn: false,
      cwd: '/space/vhosts/www.derp.com/codebase/',
    },
  },
},

That did it @shama! 👍

+1 ... My Grunt setup wasn't watching newly created files, and I'm up and running now thanks to options.cwd.

@tomusdrw tomusdrw referenced this issue in tomusdrw/grunt-sync Dec 21, 2013

Closed

Can't sync new image files from "src" directories #5

+1. Added options.cwd and now I get added/deleted events as I wanted!

Still experiencing that issue sporadically: usually when I create a new folder and add a new file, it -might- work, but then it just stops working for any new added file.

I tried with options.cwd, doesn't change anything

node v0.10.25
grunt-cli v0.1.12
grunt v0.4.2
grunt-contrib-watch 0.5.3

Same here. Also tried cwd. Just does not watch empty directories.

wilgert commented Feb 14, 2014

Is there a way to make this options.cwd property dynamically? In our project the gruntfile is under git so it is shared between developers.

Owner

shama commented Feb 14, 2014

options.cwd defaults to the process.cwd() which is set by Grunt to always be the folder where the Gruntfile lives. You're free to override by any means required by your project. Such as cwd: path.resolve(process.cwd(), 'subfolder') for a subfolder of the default current working directory.

It's helpful to describe the behavior you're experiencing versus your expected behavior. As an example, I am watching with the pattern **/*.js, when I create the folder ./lib/ and then add the file ./lib/test.js I expected a added event to fire but it does not. Here are my versions and specs and link to gist gruntfile (or paste into the issue). Thanks!

kronion commented Mar 13, 2014

I am also running into problems with files in (previously) empty folders not being watched. For example, I have the pattern */README. I can mkdir test and then touch test/README, and the file will not be watched. However, if my pattern is simply *, the command mkdir test triggers the task, so the empty directory itself is clearly watched. rmdir test does not trigger the task though, and an ENOENT error is thrown the next time the task is triggered, as watch doesn't know where the directory went.

Version numbers:
OSX 10.8.4 (could differences between 10.8 and 10.9 be the culprit? Seems unlikely)
node v0.10.12
grunt-cli v0.1.12
grunt v0.4.3
grunt-contrib-watch v0.5.3

@shama exactly like your example: I'm watching with the pattern src/**/*.js and if I create a new folder src/test and then create a file src/test/test.js I expected a added event to fire but it does not. Same with the pattern src/**/*.less, new folder src/test and then new file src/test/test.less.

Ubuntu 13.10
nodejs v0.10.26
npm 1.4.4
grunt-contrib-watch 0.6.0

        watch: {
            options: {
                livereload: true,
                spawn: false,
                interrupt: true
            },
            js: {
                files: ['src/**/*.js']
            },
            less: {
                files: ['src/**/*.less'],
                tasks: ['less:development']
            }
        },

That causes livereload to not see the change, or the less task in case of new less files not to be launched, too bad :(

Owner

shama commented Mar 17, 2014

@JSteunou Thanks for the perfect error report! :) I'll take a look when I can.

Also I can get new files in new empty folder triggering added event by watching src/**/* but I'm losing specific file tasks triggering and css nice reload.

@shama I'm very sorry my test case was not correct. I did some new tests this morning and it's a bit more tricky.

In fact if I create a new empty folder src/test and a new file in it src/test/test/js I have a added event.
But, if if I create a new empty folder src/modules/test in a my existing src/modules folder and a new file in it src/modules/test/test.js I do not have a added event while I'm expecting one.

So this could be an issue of depth with folder/**/*.ext pattern.

Confirmed: using a hard core coded files: ['src/*/*.js', 'src/*/*/*.js', 'src/*/*/*/*.js', 'src/*/*/*/*/*.js', 'src/*/*/*/*/*/*.js', 'src/*/*/*/*/*/*/*.js', 'src/*/*/*/*/*/*/*.js'], works :D

Ho boy it's even sillier! Even with my big list it does not work, but only in one folder: the src/modules folder. Whyyyyyyy :'(

Owner

shama commented Mar 18, 2014

@JSteunou Yep and that isn't even the real fun part. Once we fix it on Linux, we also have to make sure it works the same on Windows and OSX. FWIW, this will likely be fixed with the next version of gaze.

Hehe, nice to read @shama, keep on this good work!

I don't know if someone else has posted this solution yet as it is late for me and this is a long thread. If so, I apologize. But....

I found that you can put an image (maybe titled donotdeleteme.jpg perhaps?) in the folder that you are having the issue with and then exclude that image from your watch, the watch task will work correctly. Works even better if it's a jpg of a kitten.

The same issue seems to still be occurring. My test case is the following : I'm watching a simple client/**/*.jade pattern. If I delete the only jade file in the client folder (there are others but they are lower in the hierarchy), I will get a deleted event, all clear. If I save it again, nothing happens while I was expecting an added event to be emitted.

My watch configuration looks like:

{
    options: {
        livereload: true,
        spawn: false,
    },
    jsclient: {
        files: 'client/**/*.js',
        tasks: ['minify:js', 'jshint:client', 'jscs'],
    },
    jsserver: {
        files: 'server/**/*.js',
        tasks: ['jshint:server', 'jscs'],
    },
    jade: {
        files: 'client/**/*.jade',
        tasks: ['jade'],
    },
    config: {
        options: {
            reload: true,
        },
        files: ['Gruntfile.js', 'grunt/**/*.js'],
        tasks: ['jshint:server', 'jscs'],
    },
}

OS: KUbuntu 14.04LTS
node: 0.10.29
npm: 1.4.16
grunt-cli: 0.1.13
grunt: 0.4.5
grunt-contrib-watch: 0.6.1
-> gaze : 0.5.1

ZaLiTHkA commented Sep 1, 2014

Just out of curiosity, is anyone closer to finding a solution for this issue? I'm having the same trouble here, but the project I'm working on at the moment specifically needs to watch for files inside folders that are created and deleted dynamically by other processes..

I've tried with using both relative and absolute paths, with and without the cwd option, initially with '**/*.ext' then also with the ['*.ext','*/*.ext','*/*/*.ext', ... ] method suggested by JSteunou above.

Regardless of my Gruntfile setup, I always see the following behaviour:

  • created/deleted files in an existing populated folder are detected
  • created/deleted files in an existing empty folder are not detected
  • created/deleted files in a new folder are not detected

Note: folder depth doesn't seem to make any difference in my case.

My system is as follows:
OS: Win7 x64
Node: 0.10.30 (x86)
grunt-contrib-watch: 0.6.1
Consoles used: Windows cmd and Git bash

To be fair, I don't necessarily need to use Grunt for this project, I just thought it would make life easier. I see Gaze itself has had quite a few changes since the version used by grunt-contrib-watch, so perhaps I'll try with that directly and see how it goes.

@ZaLiTHkA Any luck with the new Gaze? I'm seeing the same issues as you (on the same platform). Thanks!

I see this on non-empty folders, when adding new files. On 10.9.4 running grunt to watch NFS volume mounted to Vagrant through a docker container. The project grunt-simple-watch is the closest i've found, but it can't do casacading/chain detection and lacks livereload.

mrhyde commented Sep 8, 2014

Have the same problem as ZaLiTHkA and others
If you create an empty folder, run grunt watch and after that put any file in it - it will not detect them.

OS: Win 8.1
node: 0.10.29
npm: 1.4.23
grunt-contrib-watch: 0.6.1

foxx commented Nov 6, 2014

Not sure if this needs a new issue or not but, I was able to reproduce this behaviour whilst using Vagrant with folder sharing, and Docker with volume sharing. It's able to detect changed files, but not new files.

@peterflynn peterflynn referenced this issue in adobe/brackets Nov 21, 2014

Closed

Grunt watch not working with Brackets #9884

@foxx I confirm this behavior.

lasekio commented Jan 28, 2015

@foxx Any fix for this behavior? I have same issue on Vagrant with NFS sharing.

foxx commented Jan 28, 2015

@XhmikosR Yeah, I stopped using grunt :(

gurdiga added a commit to gurdiga/xo-failed that referenced this issue Feb 18, 2015

gaupoit commented Feb 25, 2015

@foxx same issues. Which one are u using?

More than 2 years later, this problem still exists? I can't get any new files to be picked up whatsoever. Only existing files are picked up, no new files regardless as to whether the directory is empty or already contains files. cwd changes nothing, and there is no difference between /**/ in files or separate listings.

OS: Ubuntu 14.04 VirtualBox guest, Windows 7 host
nodejs: 0.10.39
grunt-contrib-watch: 0.6.1
gaze: 0.5.1

2+ years for one bug report makes me sad. Our team wanted to use SASS for our new project, but this single problem completely removes that possibility.

@frickenate Not picking up any new files is different than this issue. This issue is specifically about files added to empty directories and/or new directories being created. You could check #282 for possible solutions to your problem, as it's more generic. In general, prefixing paths with "./" and using absolute paths are issues that can make the watcher not pick up new files, so if you want to watch all .js in src, you want to use "src/**/*.js".

@markusfalk markusfalk referenced this issue in OpenSourceWorkflow/generator-kickstart Feb 10, 2016

Open

Task fails when saving a file of a deferred module #177

pigulla commented Feb 21, 2016

Just ran into this issue as well, it's ridiculous that this is still open after 2.5 years.

Yeah, seems like it will be never fixed ever...

I've found a dirty walk-through in gulp (3.9.1) but it should work in grunt since both use gaze. In empty folder there's no registered pollers and that's the reason why watcher doesn't trigger. We must make a ghost poller, basically a 'unnamed extension' like .sass and enable globs to accept dots.

Windows 10

run -> cmd.exe
cd __PATH__\src\styles
touch .sass

In gulp add dot option to gulp.watch like so:
gulp.watch(['src/styles/**/*.sass'], { dot: true }, ['styles']);

In grunt it probably will look like this:

sass: {
  options: { dot: true },
  files: 'src/styles/**/*.sass',
  tasks: ['styles'],
}

Pros: .sass doesn't compile to .css
Cons: useless files :suspect:

rohdef commented Oct 10, 2016

What the flying f*? This issue is so old and not fixed, seriously? Sure I can restart grunt when changing folder structure, so technically the workaround isn't a major issue per say, but it is kind of a fundamental feature to have. Come on, pull yourself together, will you?

I believe switching to chokidar will fix issue, but as far as I know switching isn't that easy.

STRML commented Jan 3, 2017

@rohdef This is open source. Nobody is making money and this is all volunteer effort. If this issue means so much to you, contribute something that will fix it, or fork and publish a forked version.

Other forked solutions I've found:

So sad that this has not been prioritized enough to fix. So many new developers to your tool will hit this hangup.

I wonder would maintainers fix it if we crowdfund for it?

Just drop it, grunt and gulp are unnecessary these days since npm gives you much more power (http://goo.gl/rcqrfV)

mrhyde commented Jul 15, 2017

@idbartosz Once you start dealing with something more complex you will realize that npm is missing plenty of things and managing run scripts will become a nightmare

Not to mention some things are still really difficult with npm if you must use Windows as your operating system. Command line args being one of them. Feel free to fix that and then you have a valid argument.

Everything got it pros and cons guys, but I get your point - let's go back to topic :-)

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