grunt-contrib-watch: Not detecting newly added files

If I understand correctly, grunt-contrib-watch should detect any added, modified, or deleted file that matches the patters specified in the Grunt configuration file.

On the other hand, I noticed that in my case it doesn’t detect newly added files at all. Here’s an example of my Grunt configuration:

  grunt.initConfig({
    watch: {
      options: {
        livereload: true
      },
      styles: {
        files: ['<%= yeoman.app %>/styles/sections/*.styl'],
        tasks: ['stylus', 'autoprefixer'],
        options: {livereload: false}
      },
      iconfont: {
        files: ['<%= yeoman.app %>/images/icons/*.svg'],
        tasks: ['webfont', 'stylus'],
      }

grunt-contrib-watch perfectly detects modifications to existing files, but for example if I add a new .styl file in the specified folder nothing happens. I’m using Node 0.10.24 on OSX Mavericks.

Any advice on what might be causing the problem?

Thanks 😃

About this issue

  • Original URL
  • State: open
  • Created 10 years ago
  • Reactions: 11
  • Comments: 46 (4 by maintainers)

Commits related to this issue

Most upvoted comments

It seems that there is only a problem with new files, if the watcher is configured to look at specific file types. I now use 'src/js/**' instead of 'src/js/**/*.js' and the watcher works as expected. The watcher is triggered after adding new files and after changing them without to restart the watch task. Only a little workaround with some restrictions but maybe helpful for debugging.

In case it might help someone else, I had the same problem (OS: Windows 8.1 x64) until I set:

options: {
  event: ['changed', 'added', 'deleted']
}

After I set that under options it appears to fix my issue of not detecting new files / folders. Not sure why the default of event: 'all' didn’t work.

Still happening. OSX

Hmm that could be it. The cwd is always implied so a ./ never needs to be prepended. It could be related to this issue when ./ is prepended, it breaks some of the events: shama/gaze#66

Having the same issue where newly created less files aren’t watched.

I’ve managed to reproduce this with a minimal example Gruntfile.js:

module.exports = function (grunt) {
    grunt.loadNpmTasks('grunt-contrib-watch');

    var config = {
        watch: {
            less: {
                files: ['src/**/*.less'],
                tasks: ['listfiles:less']
            },
            js: {
                files: ['src/**/*.js'],
                tasks: ['listfiles:js']
            }
        },
        listfiles: {
            js: {
                src: ['src/**/*.js']
            },
            less: {
                src: ['src/**/*.less']
            }
        }
    };

    grunt.initConfig(config);

    grunt.registerMultiTask('listfiles', 'List files matched', function () {
        console.log(this.filesSrc);
    });
};

Now if we create a src/test.js and then launch grunt watch:

  • Creating a src/test2.js triggers the watch
  • Creating a src/test.less does not trigger the watch.

What seems to be happening is that the absence of at least one matching file in the directory prevents watch from triggering on subsequent changes in that directory (for a given pattern). This is why empty directories are completely ignored, but also non-empty directories that don’t have any matching files.

Hope this can help bring about a solution.

I think this may be related to Vagrant shared folders (due to lack of inotify?)

I see, thanks for pointing that out! Removing the <%= yeoman.app %> prefix (and consequently the .), together with the first / at the beginning of the path, fixed the problem 😃

On the other hand, as a general approach I actually liked having a variable defined once in the yeomanConfig object, and then referred to elsewhere in the Grunt config file. This way, if one needs to change the folder structure or location at any point, the value has to be updated in one place only.

So, I still think it would be cool to be able to use . and have grunt-contrib-watch detect newly added files. Is there any chance that the bug could be fixed in Gaze, or does this make any sense to you at all?

Cheers!

@seomantix I tried to remove the file extension and the “*” but it still doesn’t work. Perhaps when you tested it you had an old file where you didn’t expect?

I experienced the same problem when I had “./” at the beginning of the search path. I removed it and it now it does detect newly added files in non-empty directories. I can confirm that it still doesn’t detect new files in an empty directory. Applying @Denney’s suggestion didn’t seem to help. I also tried setting the cwd property in the options to the base search path and it didn’t help.

(node 0.12, grunt: 0.4.5, grunt-contrib-watch: 0.6.1 on Windows 7 64bit)

As this issue has been unresolved for more than a year now (and more than year and a half for related ones such as #166 and shama/gaze#177 and shama/gaze#167). Being the most popular (and one of the most useful) grunt plugins I find it strange no solution has been found yet and considering either fixing it myself or looking for an alternative plugin.

Edit: After further searching I’ve found this pull request for a related test: https://github.com/shama/gaze/pull/103 . It seems there is an alternative to gaze called https://github.com/shama/navelgazer that’s in development and may not have this problem. It may eventually be used in grunt-watch. I will look further into it.

Had this problem and removing ./ from the beginning of my files options fixed it for me. 👍