From a4eb5571eb6b65b037e3605aed8e47bbd8ffdb06 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 20 Apr 2021 16:26:24 +0200 Subject: [PATCH] repl: display prompt once after error callback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Do not call `.displayPrompt()` twice after the `eval` callback resulted in an error. (This does not affect the default eval because it doesn’t use the callback if an error occurs.) PR-URL: https://github.com/nodejs/node/pull/38314 Reviewed-By: Luigi Pinca Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell Reviewed-By: Colin Ihrig --- lib/repl.js | 7 ++++-- ...st-repl-uncaught-exception-evalcallback.js | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-repl-uncaught-exception-evalcallback.js diff --git a/lib/repl.js b/lib/repl.js index 40d585af526ffd..1ec438e5210e49 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -861,8 +861,11 @@ function REPLServer(prompt, self.output.write(self.writer(ret) + '\n'); } - // Display prompt again - self.displayPrompt(); + // Display prompt again (unless we already did by emitting the 'error' + // event on the domain instance). + if (!e) { + self.displayPrompt(); + } } }); diff --git a/test/parallel/test-repl-uncaught-exception-evalcallback.js b/test/parallel/test-repl-uncaught-exception-evalcallback.js new file mode 100644 index 00000000000000..a6f6e341049d6c --- /dev/null +++ b/test/parallel/test-repl-uncaught-exception-evalcallback.js @@ -0,0 +1,23 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const repl = require('repl'); +const { PassThrough } = require('stream'); +const input = new PassThrough(); +const output = new PassThrough(); + +const r = repl.start({ + input, output, + eval: common.mustCall((code, context, filename, cb) => { + r.setPrompt('prompt! '); + cb(new Error('err')); + }) +}); + +input.end('foo\n'); + +// The output includes exactly one post-error prompt. +const out = output.read().toString(); +assert.match(out, /prompt!/); +assert.doesNotMatch(out, /prompt![\S\s]*prompt!/); +output.on('data', common.mustNotCall());