diff --git a/lib/watcher.js b/lib/watcher.js index 4befa8748..a6055bb70 100644 --- a/lib/watcher.js +++ b/lib/watcher.js @@ -4,6 +4,8 @@ var mm = require('minimatch'); var helper = require('./helper'); var log = require('./logger').create('watcher'); +var DIR_SEP = require('path').sep; + // Get parent folder, that be watched (does not contain any special globbing character) var baseDirFromPattern = function(pattern) { return pattern.replace(/\/[^\/]*[\*\(].*$/, '') || '/'; @@ -32,7 +34,7 @@ var watchPatterns = function(patterns, watcher) { // watch only common parents, no sub paths pathsToWatch.forEach(function(path) { if (!pathsToWatch.some(function(p) { - return p !== path && path.substr(0, p.length) === p; + return p !== path && path.substr(0, p.length + 1) === p + DIR_SEP; })) { watcher.add(path); log.debug('Watching "%s"', path); diff --git a/test/unit/watcher.spec.coffee b/test/unit/watcher.spec.coffee index fea91dda1..55ed85a54 100644 --- a/test/unit/watcher.spec.coffee +++ b/test/unit/watcher.spec.coffee @@ -62,6 +62,12 @@ describe 'watcher', -> expect(chokidarWatcher.watchedPaths_).to.deep.equal ['/some'] + it 'should watch a file matching subpath directory', -> + # regression #521 + m.watchPatterns patterns('/some/test-file.js', '/some/test/**'), chokidarWatcher + expect(chokidarWatcher.watchedPaths_).to.deep.equal ['/some/test-file.js', '/some/test'] + + it 'should not watch if watched false', -> m.watchPatterns [ new config.Pattern('/some/*.js', true, true, false)