diff --git a/packages/cli-repl/src/mongosh-repl.spec.ts b/packages/cli-repl/src/mongosh-repl.spec.ts index 1d2a38a883..502e9b46a1 100644 --- a/packages/cli-repl/src/mongosh-repl.spec.ts +++ b/packages/cli-repl/src/mongosh-repl.spec.ts @@ -710,4 +710,18 @@ describe('MongoshNodeRepl', () => { expect(output).to.contain('Atlas Data Lake > '); }); }); + + context('before the REPL starts', () => { + beforeEach(async() => { + await mongoshRepl.initialize(serviceProvider); + // No .start() call here. + }); + + it('does not show a prompt', async() => { + await mongoshRepl.loadExternalCode('setImmediate(() => { throw new Error(); })', ''); + await tick(); + expect(output).to.include('Error: \n'); + expect(output).not.to.include('>'); + }); + }); }); diff --git a/packages/cli-repl/src/mongosh-repl.ts b/packages/cli-repl/src/mongosh-repl.ts index 7bd0eee73e..b5fca92bd2 100644 --- a/packages/cli-repl/src/mongosh-repl.ts +++ b/packages/cli-repl/src/mongosh-repl.ts @@ -70,6 +70,7 @@ class MongoshNodeRepl implements EvaluationListener { onClearCommand?: EvaluationListener['onClearCommand']; insideAutoComplete: boolean; inspectDepth = 0; + started = false; constructor(options: MongoshNodeReplOptions) { this.input = options.input; @@ -151,6 +152,9 @@ class MongoshNodeRepl implements EvaluationListener { const originalDisplayPrompt = repl.displayPrompt.bind(repl); repl.displayPrompt = (...args: any[]) => { + if (!this.started) { + return; + } originalDisplayPrompt(...args); this.lineByLineInput.nextLine(); }; @@ -242,6 +246,7 @@ class MongoshNodeRepl implements EvaluationListener { // eslint-disable-next-line @typescript-eslint/no-unused-vars async startRepl(_initializationToken: InitializationToken): Promise { + this.started = true; const { repl, internalState } = this.runtimeState(); // Only start reading from the input *after* we set up everything, including // internalState.setCtx().