13

I've got an issue with Grunt Watch currently not re-running tasks after compilation error correction.

I get the error message, but then after correcting the error, grunt says the file has been changed, but no tasks are run after that point.

Grunt file:

watch: {
        less: {
            files: ['public/assets/less/**/*.less'],
            tasks: ['css'],
            options: {
                atBegin: true,
                nospawn: true
            }
        },
        scripts: {
            files: [
                'public/assets/js/homepage.js'
            ],
            tasks: ['jsApp'],
            options: {
                nospawn: true,
            }
        }
    },

Error log:

>> ParseError: Unrecognised input in public/assets/less/template.less on line 114, column 31:
>> 114         @media screen and (max-width: 767px) {
>> 115             left: 0;
Warning: Error compiling public/assets/less/main.less
// ----- Corrected the file here, saved again -----
>> File "public/assets/less/template.less" changed.

End of file. Nothing after this point.

Matt Cavanagh
  • 518
  • 5
  • 19
  • I have this exact issue, cannot find any information about this anywhere. For the love of God, if you find the solution, please let me know. If you set nospawn to false, this isn't an issue anymore, but for me it slowed down LESS compilation by an entire 1.5 seconds, which is massively annoying. – Craig Barben May 11 '16 at 03:41
  • can you post the code for: template.less , main.less and your full grunt file? I will try help – raduken May 11 '16 at 08:39
  • @Raduken the files themselves aren't the issue, this error was triggered, I corrected it and saved it, however Grunt detected the change but didn't actually trigger the tasks again. – Matt Cavanagh May 13 '16 at 14:33
  • but just in case can you upload the files I just curious too see if I can solve that :) – raduken May 13 '16 at 14:34
  • Unfortunately I don't have the files any more as the code has changed a lot over 3 days. I knew exactly what the error was, I missed a } in the line above, causing it to not recognise the next line. – Matt Cavanagh May 13 '16 at 14:36

3 Answers3

2

This is a problem with the grunt-contrib-watch package spawn function, you probably want to remove nospawn. You might want to try version 1.0.0 of grunt watch if you have not already.

This issue has been discussed before https://github.com/gruntjs/grunt-contrib-watch/issues/58

Also, note the documentation:

Not spawning task runs can make the watch more prone to failing so please use as needed.

In your position I would upgrade first, see that I have the latest version of grunt, grunt watch and grunt less. If that wouldn't solve the problem I would just let it spawn normally.

Oli
  • 1,132
  • 1
  • 12
  • 26
  • Sorry it took a while to get back to this. Tried this, it no longer fails on errors, but the process takes a long time to execute for `grunt watch`, in excess of 30 seconds. It's not doing anything overly complex, which nospawn was much quicker (a second or two at most) – Matt Cavanagh Nov 29 '16 at 13:49
  • I'll mark it as answer though as it does solve the issue, albeit if the spawning is god slow. – Matt Cavanagh Nov 29 '16 at 14:18
1

You can just simplify your file like this:

//src ===============================
                var src;
                config.src = src = {
                     libFolder       : 'lib/**/*.js',
                     lessFolder      : 'less/**/*.less',

                };

//Watch ===============================
                config.watch = {
                     scripts: {
                        files: ["<%= src.libFolder %>", "<%= src.lessFolder %>"]
                        ,tasks: ["dev", "less:dist"]

                     }
                }

you need to insert the tasks and files to be watched:

in that case above grunt watching all files from lib and less folder, if I do any change there grunt will run the task.

as well you need insert the tasks there to be run until you stop grunt watch.

in that case I am running dev ans less:dist at the same time.

on dev I am running: 'sprite','imagemin','concat:dev', 'uglify' ,

grunt.registerTask('dev',['sprite','imagemin','concat:dev', 'uglify']);

so I inserted this task in grunt watch, so grunt will be watching running all tasks with no error.

I hope this helped you.

raduken
  • 2,091
  • 16
  • 67
  • 105
1

I'm having the exactly same problem (combination of grunt-contrib-watch and grunt-contrib-less).

One solution is set spawn: true, but build times get much more longer.

I have found this issue occurs when using latest versions of grunt-contrib-less (all versions since 1.0.0). Using grunt-contrib-less 0.12.0 I've got no problem with watch successfully continuing after the correction of an error.

Jirsbek
  • 58
  • 7