2

Within some project I use Grunt for tasks like SASS compilation and so on. Basically it works like a charm, but as long as I am typing within the SASS files obviously sometimes an error occurs as I am not finished.

The watch task of my Gruntfile is like this:

// watch files
watch: {
    sass: {
        files: ['web/var/static/**/*.scss','web/var/static/lib/**','web/var/static/js/*.js'],
        tasks: ['default']
    }
},

I start it with grunt watch in the cli.

Sample error in Grunt log:

Completed in 1.968s at Wed Sep 18 2019 13:43:01 GMT+0200 (Central European Summer Time) - Waiting...
>> File "web/var/static/sass/layout/footer.scss" changed.
Running "sass:dist" (sass) task

Running "autoprefixer:dist" (autoprefixer) task
>> 4 autoprefixed stylesheets created.

Running "cssmin:dist" (cssmin) task
>> 4 files created. 706.54 kB → 598.46 kB

Running "concat:dist" (concat) task

Running "uglify:main" (uglify) task

Done.
Completed in 1.769s at Wed Sep 18 2019 13:43:25 GMT+0200 (Central European Summer Time) - Waiting...
>> File "web/var/static/sass/layout/footer.scss" changed.
Running "sass:dist" (sass) task
Fatal error: Error: property "ul" must be followed by a ':'
        on line 52 of web/var/static/sass/layout/footer.scss
        from line 13 of web/var/static/sass/main.scss
>>     ul

   ----^

I always have to re-run the task if this happens because grunt stops watching. That means:

  • open the console window
  • stop the current task
  • start the task again
  • change a file

This is really slowing down.

Has anybody had a similar problem? How to make grunt retry automatically after an error as soon as a new file changes happens?

Blackbam
  • 17,496
  • 26
  • 97
  • 150
  • 1
    Perhaps setting a [`debounceDelay`](https://github.com/gruntjs/grunt-contrib-watch#optionsdebouncedelay) or the maybe [`interval`](https://github.com/gruntjs/grunt-contrib-watch#optionsinterval) in `options` object may help. – RobC Sep 18 '19 at 12:50
  • Helps a little bit, but also has a downside. Thanks, still another solution would be better. – Blackbam Sep 18 '19 at 21:19
  • What is your grunt version? Can you post your full grunt file and the sample sass file that cause the above error? Did you change some settings such as **spawn**, **forever** – ChickenSoups Sep 29 '19 at 16:16
  • @ChickenSoups Grunt is latest. The SASS file is broken during typing sometimes obviously. I tried forever I did not try spawn yet. – Blackbam Sep 30 '19 at 06:26
  • 2
    So you can try `options: {spawn: true}`. It's helpful if you post your full grunt file content. – ChickenSoups Sep 30 '19 at 07:25

1 Answers1

1

The spawn method returns a reference to the spawned child. When the child exits, the doneFunction is called. And you can retry from the function below on error:

function doneFunction(error, result, code) {
  // If the exit code was non-zero and a fallback wasn't specified, an Error
  // object, otherwise null.
  error
  // The result object is an object with the properties .stdout, .stderr, and
  // .code (exit code).
  result
  // When result is coerced to a string, the value is stdout if the exit code
  // was zero, the fallback if the exit code was non-zero and a fallback was
  // specified, or stderr if the exit code was non-zero and a fallback was
  // not specified.
  String(result)
  // The numeric exit code.
  code
}
Sreeram Nair
  • 2,369
  • 12
  • 27