Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New files in empty folder not watched #166

Open
markgoodyear opened this issue Jul 21, 2013 · 118 comments
Open

New files in empty folder not watched #166

markgoodyear opened this issue Jul 21, 2013 · 118 comments

Comments

@markgoodyear
Copy link

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.

@shama
Copy link
Member

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!

@markgoodyear
Copy link
Author

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!

@shama
Copy link
Member

shama commented Jul 22, 2013

Hmm which version of node.js and which os?

@markgoodyear
Copy link
Author

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

@luissquall
Copy link

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.

@markgoodyear
Copy link
Author

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

@markgoodyear
Copy link
Author

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
Copy link

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

@shama
Copy link
Member

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
Copy link

mnoble01 commented Aug 7, 2013

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

@brett-shwom
Copy link

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

@shama
Copy link
Member

shama commented Aug 16, 2013

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

@markgoodyear
Copy link
Author

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

@brett-shwom
Copy link

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.

@brett-shwom
Copy link

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

@shama
Copy link
Member

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.

@brett-shwom
Copy link

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?

@brett-shwom
Copy link

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?

@shama
Copy link
Member

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.

@shama
Copy link
Member

shama commented Aug 18, 2013

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

@markgoodyear
Copy link
Author

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

@brett-shwom
Copy link

@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().

@mnoble01
Copy link

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

@herebefrogs
Copy link

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

@pkieltyka
Copy link

+1 .. same issue with latest libraries

@brett-shwom
Copy link

@pkieltyka what issue are you having?

@markgoodyear
Copy link
Author

@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.

@ZaLiTHkA
Copy link

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.

@orthecreedence
Copy link

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

@parisholley
Copy link

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
Copy link

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
Copy link

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.

@stephanebachelier
Copy link

@foxx I confirm this behavior.

@lasekio
Copy link

lasekio commented Jan 28, 2015

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

@foxx
Copy link

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
Copy link

gaupoit commented Feb 25, 2015

@foxx same issues. Which one are u using?

@frickenate
Copy link

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.

@tandrewnichols
Copy link

@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".

@pigulla
Copy link

pigulla commented Feb 21, 2016

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

@ArmorDarks
Copy link

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

@idbartosz
Copy link

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
Copy link

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?

@ArmorDarks
Copy link

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

@STRML
Copy link

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:

@zachrybaker
Copy link

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

@ArmorDarks
Copy link

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

@idbartosz
Copy link

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

@mrhyde
Copy link

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

@zachrybaker
Copy link

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.

@idbartosz
Copy link

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

@jpodpro
Copy link

jpodpro commented Dec 1, 2017

wow. how could this still be an issue when it unpredictably fails to perform such a fundamentally necessary function? i guess i'm going to abandon grunt-contrib-watch and try something that actually-works.jpg

https://github.com/JimmyRobz/grunt-chokidar works just fine as expected. why wouldn't you merge their changes into your project and clear this right up?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests