sindresorhus / gulp-nunjucks

Precompile Nunjucks templates
MIT License
152 stars 20 forks source link

Add useful stack traces for compilation errors #34

Closed demeralde closed 5 years ago

demeralde commented 6 years ago

When an error is thrown by Nunjucks during compilation, no stack trace is given for the source of the error. This makes debugging very difficult.

For example:

> gulp build                                                        

[16:51:38] Using gulpfile ~/Dev/freelance/trizma/gulpfile.js        
[16:51:38] Starting 'build'...                                      
[16:51:38] Finished 'build' after 13 ms                             

events.js:165                                                       
      throw er; // Unhandled 'error' event                          
      ^                                                             
Template render error: TypeError: Cannot read property 'children' of undefined                                                          
    at Object._prettifyError (/home/daniel/Dev/freelance/trizma/node_modules/nunjucks/src/lib.js:35:11)                                 
    at Template.render (/home/daniel/Dev/freelance/trizma/node_modules/nunjucks/src/environment.js:526:21)                              
    at Environment.renderString (/home/daniel/Dev/freelance/trizma/node_modules/nunjucks/src/environment.js:364:17)                     
    at DestroyableTransform._transform (/home/daniel/Dev/freelance/trizma/node_modules/gulp-nunjucks/index.js:25:36)                    
    at DestroyableTransform.Transform._read (/home/daniel/Dev/freelance/trizma/node_modules/readable-stream/lib/_stream_transform.js:184:10)                                                                                                                                    
    at DestroyableTransform.Transform._write (/home/daniel/Dev/freelance/trizma/node_modules/readable-stream/lib/_stream_transform.js:172:83)                                                                                                                                   
    at doWrite (/home/daniel/Dev/freelance/trizma/node_modules/readable-stream/lib/_stream_writable.js:428:64)                          
    at writeOrBuffer (/home/daniel/Dev/freelance/trizma/node_modules/readable-stream/lib/_stream_writable.js:417:5)                     
    at DestroyableTransform.Writable.write (/home/daniel/Dev/freelance/trizma/node_modules/readable-stream/lib/_stream_writable.js:334:11)                                                                                                                                      
    at write (/home/daniel/Dev/freelance/trizma/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:623:24)      
Emitted 'error' event at:                                           
    at DestroyableTransform.onerror (/home/daniel/Dev/freelance/trizma/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:558:12)                                                                                                                       
    at DestroyableTransform.emit (events.js:180:13)                 
    at DestroyableTransform._transform (/home/daniel/Dev/freelance/trizma/node_modules/gulp-nunjucks/index.js:28:9)                     
    at DestroyableTransform.Transform._read (/home/daniel/Dev/freelance/trizma/node_modules/readable-stream/lib/_stream_transform.js:184:10)                                                                                                                                    
    [... lines matching original stack trace ...]                   
    at write (/home/daniel/Dev/freelance/trizma/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:623:24)      
    at flow (/home/daniel/Dev/freelance/trizma/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:632:7)        
npm ERR! code ELIFECYCLE                                            
npm ERR! errno 1                                                    
npm ERR! @ build: `gulp build`                                      
npm ERR! Exit status 1                                              
npm ERR!                                                            
npm ERR! Failed at the @ build script.                              
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.                                      

npm ERR! A complete log of this run can be found in:                
npm ERR!     /home/daniel/.npm/_logs/2018-04-10T07_21_38_514Z-debug.log        

Here's the task for my Nunjucks templates:

const templates = () => gulp
  .src(paths.templates.src)
  .pipe(nunjucks.compile())
  .pipe(rename({
    dirname: '.',
    extname: '.html',
  }))
  .pipe(prettify())
  .pipe(gulp.dest(paths.templates.dest));
sindresorhus commented 6 years ago

Any suggestions how? This plugin just forwards the error from the Nunjucks API.

demeralde commented 6 years ago

Oh okay. If there's no options to show useful info in the stack trace with the Nunjucks API, that's a limitation.

I don't see how that could be worked around but perhaps there's a way.

On Tue., 10 Apr. 2018, 5:17 pm Sindre Sorhus, notifications@github.com wrote:

Any suggestions how? This plugin just forwards the error from the Nunjucks API.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/sindresorhus/gulp-nunjucks/issues/34#issuecomment-380007114, or mute the thread https://github.com/notifications/unsubscribe-auth/AI2lFWdTAfD5bgPuX1oLVgS5arvXczPmks5tnGOrgaJpZM4TNuwA .

sindresorhus commented 5 years ago

Closing. You should open an issue on Nunjucks about this instead.