Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/cli-repl/src/mongosh-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/
);
});

Expand Down
10 changes: 9 additions & 1 deletion packages/cli-repl/src/mongosh-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ type Mutable<T> = {
*/
class MongoshNodeRepl implements EvaluationListener {
_runtimeState: MongoshRuntimeState | null;
closeTrace?: string;
input: Readable;
lineByLineInput: LineByLineInput;
output: Writable;
Expand Down Expand Up @@ -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\nCurrent trace: ${
new Error().stack
}\nClose trace: ${this.closeTrace}\n`
);
}
return this._runtimeState;
}
Expand All @@ -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));
Expand Down
Loading