From 0831cc8fbc12de542bf4e92809923099759c2751 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.) --- 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 26d992adbae0fc..cf544fb224a446 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -889,8 +889,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());