Skip to content

Not detecting added files on OSX #20

Closed
coen-hyde opened this Issue Dec 7, 2012 · 28 comments
@coen-hyde

Grunt-contrib-watch isn't detecting files new file additions for me on osx. I assume this is related to issue #4 (the test provided in issue #4 confirms the problem). File changes and file deletions are handled correctly.

OSX: 10.8.2
node: 0.8.15
grunt: 0.3.17
grunt-contrib-watch: 0.1.4

@shama
grunt member
shama commented Dec 7, 2012

I'm worried this might be related to #13, where I believe it's an issue with OSX >= 10.7 and this plugin. I'm still on OSX 10.6 and this test passes for me. On my todo list to upgrade and see if I can find a solution.

If you get some time, could you download and run the test suite npm test on gaze, then post your results? Thanks!

As a temporary solution, you can turn on the option: forceWatchMethod: 'old' to get around it.

@usharf
usharf commented Dec 8, 2012

It's the same for me, on OS X 10.8.2. It works with forceWatchMethod: old, as suggested, but it is very slow.

@philips
philips commented Dec 29, 2012

I had to install gaze "0.3.1" to get the forceWatchMethod to work reliably with the 0.2.x branch of grunt-contrib-watch on OSX 10.8 w/ macvim.

@presstube

Quick heads-up that I'm having the same problem on 10.8.2 with watch 0.2x.

@hojberg
hojberg commented Jan 15, 2013

also seeing this problem on the newest version

@shama
grunt member
shama commented Jan 15, 2013

Thanks! If anyone has the time to look into this on OSX 10.7+ it would be very much appreciated :)

@philips
philips commented Jan 15, 2013

@shama All I had to do was bump the gaze dep to 0.3.1 and grunt-contrib-watch worked fine

@itsjamie

I can confirm this issue on..
Mac OSX 10.8.2
Node v0.8.16
Grunt v0.4.0rc7
grunt-contrib-watch 0.2.0rc7

I noticed this also on a Windows 7 machine running the same versions as above.

New files that are added to a watched folder are not picked up, however if you restart the watch task everything functions as per normal.

I tried dropping the Gaze dependencies down to 0.3.1 from 0.3.2 incase it had a regression error, no dice. As well, I ran the tests on Gaze and they all passed.

I should have some time tomorrow to test out Gaze 0.3.2 by itself to narrow down where the error exists. Will report back my findings.

Running "nodeunit:files" (nodeunit) task
Testing add_test.js..OK
Testing api_test.js...OK
Testing matching_test.js....OK
Testing rename_test.js.OK
Testing relative_test.js.OK
Testing watch_test.js...........OK
Testing safewrite_test.js.OK
>> 39 assertions passed (17907ms)
@shama
grunt member
shama commented Jan 30, 2013

@philips glad to hear it's not an issue for you!

@jamie-stackhouse I would very much appreciate the help. Thanks!

@itsjamie

An aside... how do you do negation? I thought something like..
['!node_modules//*.js', '/*.js'] would match js files outside node_modules. I tried both ways, with the negation first or second, both times it came back with all the javascript files in node_modules.

So, I only have access to the Windows 7 machine right now, however..

var gaze = require('gaze');

gaze(['js/**/*.js'], function(err, watcher) {
    console.log(this.watched());

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

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

    this.on('all', function(event, filepath) {
        console.log(filepath + ' was ' + event);
    });

    this.on('error', function(err) {
        console.log(err);
    })

})

Works as currently intended where new files in old directories are detected. However, in this task, that is not the case. Somewhere from gaze to task the add event or all event is getting lost. Investigating further...

@itsjamie

So, maybe you can answer this and I'm missing something but at https://github.com/gruntjs/grunt-contrib-watch/blob/master/tasks/watch.js#L105, the code glob is expanded to match files, and then you create watchers on each file. Gaze is no longer watching a directory for new files, it's just watching those files to see them be changed and deleted. This line was changed in this commit.. c36fea1

I see that it was changed so that exclusions would be respected.. Maybe this is related to the issue that I ran into trying to create an exclusion for node_modules in gaze? Personally, I feel that Gaze should deal with the excluding, not the watch task.

@itsjamie

So, changing this to

var gaze = new Gaze(target.files...

and passing the globs directly to Gaze gets the added files working on Windows 7, however I'm getting some weird behaviour. Most likely because it doesn't do the exclusions right. I don't know enough about the libraries to know if this kind of thing is expected. But to explain what is being shown in the image. I changed that new.html file and you can see some console logs showing the four "glob arrays?" this should be watching. The change event is picked up on new.html, which causes a JST task to run, which recreates a new templates.js file. This fires off a change event for that, which for some reason fires added events on the vendor folder (this has existed the whole time). But most importantly, it fires off the backend_js tasks, for a change on templates.js which lives in the public folder, which is explicitly excluded.

I'd say, there needs to be some work done on Gaze regarding minimatch exclusions. I'll try, but I can promise no pull request as this is outside my area of expertises :(

I'm not sure how people are getting added events in this task right now, because as far as I can tell, it's impossible. My suggestion would be to get Gaze working nicely with exclusions then just pop the globs directly through.

Example

Here is my gruntfile in case it helps figure this out.. I removed one extraneous glob from backend_js.

module.exports = function(grunt) {
    'use strict';

    //Project Configuration
    grunt.initConfig({
        watch: {
            less: {
                files: 'less/**/*.less',
                tasks: ['less:development'],
                options: {
                    interrupt : true
                }
            },
            frontend_js: {
                files: ['public/js/**/*.js', '!public/js/vendor/*.js', '!public/js/templates.js'],
                tasks: ['exec:test_frontend'], //add requirejs task
                options: {
                    interrupt: false
                }
            },
            backend_js: {
                files: ['**/*.js', '!public/**/*.js', '!node_modules/**/*.js'],
                tasks: ['exec:test_backend'],
                options: {
                    interrupt: false
                }
            },
            templates: {
                files: ['templates/**/*.html'],
                tasks: ['jst:development'],
                options: {
                    interrupt: true
                }
            }
        },
        exec: {
            test_all: {
                cmd: "mocha --colors test" 
            },
            test_frontend: {
                cmd: "mocha --colors --recursive test/frontend" 
            },
            test_backend: {
                cmd: "mocha --colors --recursive test/backend"
            }
        },
        jst: {
            development: {
                options: {
                    amdWrapper: true,
                    prettify: true,
                    processContent: function(src) {
                        return src.replace(/(^\s+|\s+$)/gm, '');
                    }
                },
                files: {
                    "public/js/templates.js": ["templates/**/*.html"]
                }
            },
            production: {
                options: {
                    amdWrapper: true,
                    processContent: function(src) {
                        return src.replace(/(^\s+|\s+$)/gm, '');
                    }
                },
                files: {
                    "public/js/templates.js": ["templates/**/*.html"]
                }
            }
        },
        less: {
            development: {
                options: {
                    paths: "less/include"
                },
                files: [
                    {
                        expand: true,
                        //flatten: true, //will make dest folder flat
                        cwd: 'less/',
                        src: ['**/*.less', '!include/*.less'],
                        dest: 'public/css/',
                        ext: '.css'
                    }
                ]
            },
            production: {

            }
        }

    });
    grunt.loadNpmTasks('grunt-exec');
    grunt.loadNpmTasks('grunt-contrib-jst');
    grunt.loadNpmTasks('grunt-contrib-less');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.registerTask('test', ['exec:test_all']);
    grunt.registerTask('build_development', 'less:development');
}
@itsjamie itsjamie referenced this issue in shama/gaze Jan 30, 2013
Closed

Add Lodash #18

@itsjamie

Progress! Async console logging makes it difficult to show. However...

I've got some exclusions working in gaze, you can see it process down the last rule at the bottom.. now to just clean up the soup and figure out why it isn't actually watching the files.. :P

Progress!

@itsjamie

Some code inside gaze is way to greedy and trying to add every file from a folder that it is watching regardless of whether or not it matches an existing pattern :( (It's also of course possible I messed something up :P)

The *** FINAL *** messages and the pattern are added to the Gaze.prototype.add method to aid debugging of adding the exclusions. I thought I was done, and getting ready to submit a pull request, when I went and saved my gruntfile, it worked, but Gaze went ahead and added all the files and folders that were in the same directory as the gruntfile.

Waiting...************FINAL***********
[ '**/*.js', '!public/**/*.js', '!node_modules/**/*.js' ]
Status: changed
Filepath: C:\Users\jstackhouse\Development\lnl\Gruntfile.js
************FINAL***********
[ '.git\\' ]
************FINAL***********
[ '.gitignore' ]
************FINAL***********
[ '.travis.yml' ]
************FINAL***********
[ 'less\\' ]
************FINAL***********
[ 'node_modules\\' ]
************FINAL***********
[ 'package.json' ]
************FINAL***********
[ 'public\\' ]
************FINAL***********
[ 'README.md' ]
************FINAL***********
[ 'routes\\' ]
************FINAL***********
[ 'templates\\' ]
************FINAL***********
[ 'test\\' ]
************FINAL***********
[ '.git\\' ]
************FINAL***********
[ '.gitignore' ]
************FINAL***********
[ '.travis.yml' ]
************FINAL***********
[ 'less\\' ]
************FINAL***********
[ 'node_modules\\' ]
************FINAL***********
[ 'package.json' ]
************FINAL***********
[ 'public\\' ]
************FINAL***********
[ 'README.md' ]
************FINAL***********
[ 'routes\\' ]
************FINAL***********
[ 'templates\\' ]
************FINAL***********
[ 'test\\' ]
Status: added
Filepath: C:\Users\jstackhouse\Development\lnl\.git\
Status: added
Filepath: C:\Users\jstackhouse\Development\lnl\.gitignore
Status: added
Filepath: C:\Users\jstackhouse\Development\lnl\.travis.yml
Status: added
Filepath: C:\Users\jstackhouse\Development\lnl\less\
Status: added
Filepath: C:\Users\jstackhouse\Development\lnl\node_modules\
Status: added
Filepath: C:\Users\jstackhouse\Development\lnl\package.json
Status: added
Filepath: C:\Users\jstackhouse\Development\lnl\public\
Status: added
Filepath: C:\Users\jstackhouse\Development\lnl\README.md
Status: added
Filepath: C:\Users\jstackhouse\Development\lnl\routes\
Status: added
Filepath: C:\Users\jstackhouse\Development\lnl\templates\
Status: added
Filepath: C:\Users\jstackhouse\Development\lnl\test\
OK
>> File "Gruntfile.js" changed.
>> File ".git" added.
>> File ".gitignore" added.
>> File ".travis.yml" added.
>> File "less" added.
>> File "node_modules" added.
>> File "package.json" added.
>> File "public" added.
>> File "README.md" added.
>> File "routes" added.
>> File "templates" added.
>> File "test" added.
@shama
grunt member
shama commented Jan 30, 2013

@jamie-stackhouse thanks for all your work on this! Just a suggestion. How I usually tackle these things is to first create tests that show the exact behavior I'm working towards. Then work on the code until those tests pass.

This issue is difficult for me because I believe I have passing tests that demonstrates the issue reported. Ideally I'd like to have a failing test case that reveals the issue reported so we can work towards a fix.

I really appreciate your time on this and am looking forward to see what you come up with. Thanks!

@itsjamie

@shama, Gaze did have testcases that passed, because the actual functionality in Gaze for adding files was great :+1: The issue was that it wasn't a glob being passed into Gaze from this Task anymore, it was an already expanded set of files from the globs passed in, this changed I believe with the merge from #30, so Gaze never touched the initial glob. I hope I'm getting the terminology right :)

After changing that, the behaviour was not ideal though, so I've kinda ported the Grunt expand method into Gaze and asyncimified it, and now trying to figure out why it's adding files that don't match the initial patterns, I'm assuming I've done something incorrect with the Gaze internals and forgot to register something somewhere :)

@donaldpipowitch

I have the same problem:
Mac OS X 10.8.2
Node.js v.0.8.19
grunt 0.4.0rc7
grunt-contrib-watch v0.2.0rc7

:(

EDIT: This seems to work: https://github.com/gruntjs/grunt-contrib-watch/pull/43/files#r2843299

@itsjamie

donaldpipowitch, if you do not need exclusion patterns that will be enough to pass the patterns through, if you want to exclude certain files, you are going to need to use the referenced pull request in Gaze.

@shama
grunt member
shama commented Feb 11, 2013

A fix is coming from upstream and will be here soon.

@itsjamie

@shama awesome!

I took a look at the branch, looks much nicer than mine :P

Thanks!

@donaldpipowitch

Waiting for the fix :+1:

@shama
grunt member
shama commented Feb 12, 2013

Thank you @jamie-stackhouse for identifying the underlying problem!

@shama shama closed this in f857f2b Feb 14, 2013
@dennisharrison

I didn't know whether you would want a new issue or for this one to be re-opened. As it's the same problem and the same test fails I'm hoping re-opening this one is correct.

I'm seeing this issue with the following versions:
OSX: 10.8.4
node: 0.10.18
grunt: 0.4.1
grunt-contrib-watch: 0.5.3

This problem is also affecting Ubuntu 13.04 with the same versions of node, grunt, and grunt-contrib-watch. New files only, modified and deleted are working.

@JonDum
JonDum commented Sep 17, 2013

Found this thread via Google. Having the same issue.

OS X 10.8.4
node v0.10.17
grunt-cli v0.1.9
grunt v0.4.1
grunt-contrib-watch@0.5.3

I have a simple watch task:

watch: {
  all: {
    files: ['pages/**', 'less/**', 'js/**'],
    tasks: ['default']
  }
}

And saving to any file in the cwd causes grunt watch to trigger. Even if I save the Gruntfile, which is in the root directory and not even listed in the watch:

Running "watch" task
Waiting...OK
>> File ".git" added.
>> File "build" added.
>> File "node_modules" added.
>> File "static" added.
>> File "templates" added.

All of those files are already in the cwd, so why it's picking up that they've been added again is beyond me.

@snrbrnjna

Having the same issue on

Mac OS 10.6.8
node v0.10.13
grunt-cli v0.1.11
grunt v0.4.2
grunt-contrib-watch 0.5.3 & 0.5.2

And btw the tests are not running. I'm not sure if i'm doing them right, but if i go to the grunt-contrib-watch folder and call npm install and then grunt nodeunit the events_test.js gives me one red F. and then nothing happens, the test task doesn't exit. If i call the tests one by one, all of them pass green minus the events test. same behaviour.

@shama
grunt member
shama commented Nov 27, 2013

@snrbrnjna Would you mind running the tests in verbose mode? npm test and post the results. That should give you more info on where it is failing.

@jakswa
jakswa commented Dec 9, 2013

I just ran the tests, hoping to solve this for me on osx 10.9, and the tests hang on events - onlyAdded..., and I have to ctrl-C out to stop the testing. Not sure what's up, just thought I'd chime in with what's happening with me.

Just to confirm, I'm here because I'm seeing this issue as well. New files aren't getting picked up by watch.

edit: fresh reinstall of the package has it hanging on events - onlyDeleted... now. Feels like this is either my environment, or something strange...

Watch 0.5.3
Grunt 0.4.1
Node 0.10.21

@jakswa
jakswa commented Dec 10, 2013

After reading a bit more about this issue, I tried upgrading to Node 0.10.22 and this behavior disappeared. All is working now.

img

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.