How can I get debug info when running mongosh script?

After having moved from the old mongo commandline to mongosh, I am struggling with the lack of info from debug output when scripts run into problems. For comparison with the old (i.e. 3.6ish) behaviour, consider the two following JS files:

foo.js:

print('Hello');
load('./bar.js');

bar.js:

print(' World');
noSuchFunction();

Running this on the legacy mongo 3.6 instance, I get some helpful info:

~ /usr/bin/mongo --quiet foo ./foo.js
Hello
 World
2023-09-21T09:56:49.765+0200 E QUERY    [thread1] [./bar.js:2:1] ReferenceError: noSuchFunction is not defined
Stack trace:
@./bar.js:2:1
@./foo.js:2:1
----------
2023-09-21T09:56:49.765+0200 E QUERY    [thread1] Error: error loading js file: ./bar.js @./foo.js:2:1
failed to load: ./foo.js

In mongosh (1.10.1, running on MongoDB 6.0.8), debug output is somewhat on the meagre side:

~ /usr/bin/mongosh --quiet foo ./foo.js
Hello
 World
ReferenceError: noSuchFunction is not defined

I have scanned the documentation, but I didn’t find anything referencing mongosh debug output. The --verbose-Flag doesn’t help either. Once you have to deal with some more complex scripts running generated aggregations, this is becoming a major pain. I am fairly certain that I’ve just overlooked something in the configuration, but I cannot for the life of me figure out what it could be.

Is there any way to get the old behaviour back with mongosh, so a stack trace with filenames and line numbers is shown for errors? I already tried with /etc/mongosh.conf - this currently reads

mongosh:
  showStackTraces: true

If I add some error to /etc/mongosh.conf, mongosh complains, so I am fairly sure that this config is actually read. Still, no stack traces for me.

Kind regards

Markus

1 Like

Hey @Markus_Wollny,

Have you tried using the config API to see if it suits your use case / is useable in the meantime whilst I’m testing the config file setting? Updates made using the config API persist between sessions.

I ran the following in my test environment:

[primary] test> config.get('showStackTraces') 
false /// <--- initially false
[primary] test> noSuchFunction()
ReferenceError: noSuchFunction is not defined

[primary] test> config.set('showStackTraces',true) /// <--- set to true
Setting "showStackTraces" has been changed
[primary] test> noSuchFunction()
Uncaught:
ReferenceError: noSuchFunction is not defined
    at REPL7:25:9
    at REPL7:39:5
    at REPL7:43:3
    at Script.runInContext (node:vm:141:12)
    at PrettyREPLServer.defaultEval (node:repl:574:29)
    at bound (node:domain:433:15)
    at PrettyREPLServer.runBound (node:domain:444:12)
    at /opt/homebrew/Cellar/mongosh/1.10.1/libexec/lib/node_modules/@mongosh/cli-repl/lib/async-repl.js:147:20
    at /opt/homebrew/Cellar/mongosh/1.10.1/libexec/lib/node_modules/@mongosh/cli-repl/lib/async-repl.js:167:20
    at node:internal/util:364:7
[primary] test> 

I’ll try with a config file and see if i’m getting similar behaviour to what you’ve detailed on the post. Can you advise what operating system you’ve tried this on as well?

Thanks in advance,
Jason

2 Likes

The behaviour seems to be different depending on how commands are being executed. Using the config-API, I can confirm, that the setting from /etc/mongosh.conf does in fact work, as the initial value for showStackTraces is already true:

rs0 [direct: primary] test> config.get('showStackTraces') ;
true

If I add print("showStackTraces is set to " + config.get('showStackTraces')); to my test-script and run it, I still get no stacktrace in the output:

# /usr/bin/mongosh --quiet foo ./foo.js
Hello
showStackTraces is set to true
 World
ReferenceError: noSuchFunction is not defined

Omitting the --quiet flag just adds the usual server info and startup warning, but alas, no stack trace.

If I however run mongosh in interactive mode instead of passing a JS file on the commandline, the stack trace is working fine:

rs0 [direct: primary] foo> load('./foo.js');
Hello
showStackTraces is set to true
 World
Uncaught:
ReferenceError: noSuchFunction is not defined
    at /home/foo/bar.js:26:9
    at async ShellEvaluator.innerEval (/tmp/m/boxednode/mongosh/node-v16.20.1/out/Release/node:100:375625)
    at async ShellEvaluator.customEval (/tmp/m/boxednode/mongosh/node-v16.20.1/out/Release/node:100:375764)
    at async MongoshNodeRepl.eval (/tmp/m/boxednode/mongosh/node-v16.20.1/out/Release/node:22:137768)
    at async PrettyREPLServer.h.eval (/tmp/m/boxednode/mongosh/node-v16.20.1/out/Release/node:22:92116)

So a viable workaround for the time being is to explicitly load() my scripts in an interactive shell, though for testing purposes, getting the stack trace when passing the script as commandline argument would really, really be helpful.

Operating system is Debian 11.7, shell is bash.

Kind regards

Markus

1 Like