From 7417d9291ce6735b277f36c58b9e85dd9359eca7 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 11 Dec 2024 16:48:33 -0500 Subject: [PATCH 1/2] fix(cli-repl): improve debuggability of `mongosh not initialized yet` errors These errors have historically been very hard to debug, so adding stack traces unconditionally will hopefully improve this situation in the long term. --- packages/cli-repl/src/mongosh-repl.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/cli-repl/src/mongosh-repl.ts b/packages/cli-repl/src/mongosh-repl.ts index 8903db2ab4..27813995de 100644 --- a/packages/cli-repl/src/mongosh-repl.ts +++ b/packages/cli-repl/src/mongosh-repl.ts @@ -126,6 +126,7 @@ type Mutable = { */ class MongoshNodeRepl implements EvaluationListener { _runtimeState: MongoshRuntimeState | null; + closeTrace?: string; input: Readable; lineByLineInput: LineByLineInput; output: Writable; @@ -1028,7 +1029,13 @@ class MongoshNodeRepl implements EvaluationListener { */ runtimeState(): MongoshRuntimeState { if (this._runtimeState === null) { - throw new MongoshInternalError('Mongosh not initialized yet'); + // This error can be really hard to debug, so we always attach stack traces + // from both when .close() was called and when + throw new MongoshInternalError( + `mongosh not initialized yet\nCurrentTrace: ${ + new Error().stack + }\nClose trace: ${this.closeTrace}\n` + ); } return this._runtimeState; } @@ -1043,6 +1050,7 @@ class MongoshNodeRepl implements EvaluationListener { const rs = this._runtimeState; if (rs) { this._runtimeState = null; + this.closeTrace = new Error().stack; rs.repl?.close(); await rs.instanceState.close(true); await new Promise((resolve) => this.output.write('', resolve)); From 4889c6abf16a82fa09272d85f6c600c481c39a3d Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 12 Dec 2024 10:43:35 -0500 Subject: [PATCH 2/2] fixup: adjust test --- packages/cli-repl/src/mongosh-repl.spec.ts | 2 +- packages/cli-repl/src/mongosh-repl.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/cli-repl/src/mongosh-repl.spec.ts b/packages/cli-repl/src/mongosh-repl.spec.ts index 3be21871fb..f5cac3853e 100644 --- a/packages/cli-repl/src/mongosh-repl.spec.ts +++ b/packages/cli-repl/src/mongosh-repl.spec.ts @@ -121,7 +121,7 @@ describe('MongoshNodeRepl', function () { it('throws an error if internal methods are used too early', function () { expect(() => mongoshRepl.runtimeState()).to.throw( - 'Mongosh not initialized yet' + /mongosh not initialized yet\nCurrent trace/ ); }); diff --git a/packages/cli-repl/src/mongosh-repl.ts b/packages/cli-repl/src/mongosh-repl.ts index 27813995de..a977572c22 100644 --- a/packages/cli-repl/src/mongosh-repl.ts +++ b/packages/cli-repl/src/mongosh-repl.ts @@ -1032,7 +1032,7 @@ class MongoshNodeRepl implements EvaluationListener { // This error can be really hard to debug, so we always attach stack traces // from both when .close() was called and when throw new MongoshInternalError( - `mongosh not initialized yet\nCurrentTrace: ${ + `mongosh not initialized yet\nCurrent trace: ${ new Error().stack }\nClose trace: ${this.closeTrace}\n` );