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)
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:
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#66Having the same issue where newly created less files aren’t watched.
I’ve managed to reproduce this with a minimal example
Gruntfile.js:Now if we create a
src/test.jsand then launchgrunt watch:src/test2.jstriggers the watchsrc/test.lessdoes not trigger the watch.What seems to be happening is that the absence of at least one matching file in the directory prevents
watchfrom 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
yeomanConfigobject, 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 havegrunt-contrib-watchdetect 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
cwdproperty 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.1on 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 myfilesoptions fixed it for me. 👍