-
Notifications
You must be signed in to change notification settings - Fork 640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix issue where sync render would not raise errors in included templates #1301
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1301 +/- ##
==========================================
- Coverage 89.61% 89.60% -0.01%
==========================================
Files 22 22
Lines 3043 3041 -2
==========================================
- Hits 2727 2725 -2
Misses 316 316
Continue to review full report at Codecov.
|
// tasks are both not passing errors up the chain of callbacks AND are not | ||
// causing a return from the top-most render function). But fixing that | ||
// will require a more substantial change to the compiler. | ||
if (didError && cb && typeof res !== 'undefined') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The second call to the root render callback in issue ##1029 happened here in compileRoot
:
nunjucks/nunjucks/src/compiler.js
Lines 1129 to 1133 in 63c4baf
this._emitLine('if(parentTemplate) {'); | |
this._emitLine('parentTemplate.rootRenderFunc(env, context, frame, runtime, cb);'); | |
this._emitLine('} else {'); | |
this._emitLine(`cb(null, ${this.buffer});`); | |
this._emitLine('}'); |
If an error occurs inside an included template, the error will be passed up the callback chain, and the user's callback will get called with the correct error argument. But it won't cause a return from the root compiled template function, so it will still call cb(null, this.buffer)
at the very end. this.buffer
is always a string, even if empty, so by confirming that res
is not undefined we can check specifically for the situation described above.
Summary
The fix introduced in PR #1049, preventing multiple calls to the render callback, was a bit too aggressive and prevented some template errors from being surfaced when
render
orrenderString
were called synchronously.The real bug here lies in the compiler, which should not be calling the same callback function more than once. But the way that waterfall tasks are chained makes it difficult to properly halt execution in a template. We ought to fix the underlying issue, but for now this should (hopefully) ensure that thrown errors always surface when rendering synchronously, while not causing a regression for #1029.
Closes #1272 . I suspect this also fixes #1246, but I can't confirm because the reporter of that issue hasn't provided a test case I can use.
Checklist
Documentation is added / updated to describe proposed change. (N/A)