Only mess with uncaught exceptions during a grunt run. #536

wants to merge 1 commit into


None yet
3 participants

sorccu commented Nov 19, 2012

I had the need to do a require('grunt') in my own app. However just doing so would cause all uncaught exceptions in my own app to be caught by grunt, which would then print out the error message but leave out everything useful like the stack trace. In my case I actually had to require grunt during a test run (the app is a bit special, bear with me) so it was dying on every async assertion. In short, this behavior makes building an app around grunt very difficult.

Furthermore and in general, I believe you also agree that just requiring something should not alter your environment.

So, I made some minor changes: the uncaughtException handler is moved to right before the task run starts, and removed after finishing in the done callback. The error callback calls fail.warn which either exists directly or just keeps going, so I didn't add it there.

Seeing how the code is commented out in 0.3.17, I don't think this patch introduces any breaking changes.

Only mess with uncaught exceptions during a grunt run. Previously, ju…
…st doing a require('grunt') would make your own app undebuggable.

sorccu commented Nov 20, 2012

FYI I was able to circumvent the handler by always requiring this wrapper instead of grunt directly:

function listeners() {
  return process.listeners('uncaughtException').slice(0);

var oldExceptionHandlers = listeners(),
    grunt = require('grunt'),
    newExceptionHandlers = listeners(),

while ((handler = newExceptionHandlers.pop())) {
  if (oldExceptionHandlers.indexOf(handler) === -1) {
    process.removeListener('uncaughtException', handler);

module.exports = grunt;

However it's a very ugly hack and a real solution would be much preferred.

@cowboy cowboy closed this Dec 12, 2012


shama commented Dec 12, 2012

devel is now master. Could you reopen this pull request against the master branch now? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment