0

I'm using grunt-watch to re-build my less style sheets:

    watch: {
        less: {
            files: ['media/less/**/*.less'],
            tasks: ['less'],
            options: {
                atBegin: true,
                spawn: false
            }
        }
    }

But if there is a syntax error in any of the .less files, the task just loops, trying to re-build the .less files every second… which makes debugging fairly difficult, because the error messages scroll past very quickly.

Is there any way fix that, so grunt-watch will only re-run the task once the .less files have been changed again?

This is using:

grunt@0.4.2
grunt-contrib-less@0.8.3
grunt-contrib-watch@0.5.3
David Wolever
  • 148,955
  • 89
  • 346
  • 502

2 Answers2

2

I think the issue you're describing is this one, which was fixed in master but hasn't yet been released (as of 2013/12/17).

matthewwithanm
  • 3,733
  • 2
  • 21
  • 27
0

Well, for debugging purposes you could do a simple envelope of the less task with a custom task:

grunt.registerTask('myless', 'my less task', function() {
  // do whatever debugging you want and stop the loop if needed.
  grunt.task.run(['less']);
});

Then use your myless task in the watch.

UPDATE:

the idea is that since any repeated call to less now goes through your code - you can do whatever needed to either provide a more concrete output or preven repeated calls if failing is "desired" outcome and should fail, but not loop.

UPDATE 2:

Something like this:

watch: {
    `less`: {
        files: ['**/*.less'],   // or whatever the extension is
        tasks: ['myless']       // your envelope task
    }
}

var flag;

grunt.registerTask('myless', 'My LESS task', function() {
    if(flag === true) {
        // if you are here - it means watch just invoked you repeatedly
        // do whatever you need to analyze the issue (includig running an additional task)

        flag = false;
        return; // you exit task without altering any less files again - 
                // that should NOT trigger watch again
    } else {
        flag = true;
        grunt.task.run(['less']);
    }
});
ZenMaster
  • 12,363
  • 5
  • 36
  • 59
  • Forgive me for being dense (I'm still fairly new to Grunt and friends), but how will this prevent the `grunt watch` task from looping if the less build fails? How can I tell whether the the less compile failed? – David Wolever Dec 16 '13 at 15:04
  • The idea is that in the function handler for the `myless` task - which will get called continuosly by the `watch` task - you do whatever printout you need that provides some information as to why this happens. You, since it's the same grunt process and instance, theoretically can prevent repeated calls to `less` from **your** task. – ZenMaster Dec 16 '13 at 18:17
  • Right… but how can I tell whether the less compile failed? It doesn't look like `grunt.task.run(…)` returns anything useful. Also, how would I prevent grunt-watch from re-running the task until the files are changed again? – David Wolever Dec 16 '13 at 21:33
  • See my update. Remember - grunt and all friends are eventually just JavaScript - so you can do a lot of things, like dynamically enabling or disabling 'watch' task, using `grunt.config` API. – ZenMaster Dec 17 '13 at 04:11
  • I'm sorry, but I think that the solution you're describing isn't appropriate for my problem. Specifically, it looks like the problem I've been experiencing was a bug (see accepted answer). – David Wolever Dec 17 '13 at 18:15
  • Not a problem. My solution was a workaround that could have perhaps narrowed down the issue or provided a stopgap. No harm. – ZenMaster Dec 17 '13 at 20:57