Attila Szeremi Attila Szeremi - 2 months ago 9x
Javascript Question

gulp watch not always watching during task

Gulp works almost great. It watches and runs tasks properly, except if it's in the middle of running a task, it won't always be watching the files related to that same task in order to run the task again when the last task is finished.

[08:30:51] Starting 'composer'...
composer task
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Warning: The lock file is not up to date with the latest changes in composer.json. You may be getting outdated dependencies. Run update to update them.
Nothing to install or update
Generating autoload files
Process exited with code 0
[08:30:53] Finished 'composer' after 1.62 s

EDIT: don't mind the short time; this is just a bad example. The task I'm really running runs in 10-15s under which time it's realistic that I could make additional relevant changes and save.

This is triggered when editing the composer.lock file.

After having edited the composer.lock file and the task having begun running, on editing and saving composer.lock again during the time "composer task" is output,

Expected: the task to run again after it finishes

Actual: the task finishes and does not rerun to accomodate for the changes that happened since

I'm using gulp watch on Ubuntu.


It's not that isn't watching when you run a task. Although there is a debounceDelay option in gaze that prevents identical events from firing within a certain time window it is so short that it's unlikely that you're running into it. You can try setting it to 0 just to be sure, but that might cause more problems than it solves.

The more likely cause is that orchestrator simply doesn't run a task if it is already running:

Orchestrator will ensure each task and each dependency is run once during an orchestration run even if you specify it to run more than once. [...] If you need it to run a task multiple times, wait for the orchestration to end (start's callback) then call start again.

I can in fact reproduce your problem by simulating a long running task using setTimeout():

gulp.task('task', function (done) {
  setTimeout(done, 10000);

gulp.task('watch', function() {'some_file.txt', {debounceDelay:0}, ['task']);

The only thing I can come up with to solve this is to manually track whether the task is already running and then schedule a rerun once the executing task has finished:

function runTask(taskName) {
  var running = false;
  var rerun = false;
  return function() {
    if (running) {
      rerun = true;
    running = true;
    gulp.start(taskName, function() {
      if (rerun) {
        rerun = false;
      } else {
        running = false;

gulp.task('task', function (done) {
  setTimeout(done, 10000);

gulp.task('watch', function() {'some_file.txt', {debounceDelay:0}, runTask('task'));