[SERVER-14072] Shell should load .js files before --eval Created: 28/May/14  Updated: 26/Oct/21  Resolved: 26/Oct/21

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Kevin Pulo Assignee: DO NOT USE - Backlog - Platform Team
Resolution: Won't Do Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-5039 Mongo Shell should support concept of... Closed
Participants:

 Description   

Currently the shell processes --eval prior to any .js files on the command line. Is there any particular reason this order is used? I find it confusing and backwards to what I expect, probably because I think of --eval as a non-interactive replacement for the interactive REPL (unless --shell is used), and when .js files are specified without --eval they are loaded prior to presenting the prompt.

My use case is a library of code contained in a .js file which defines some functions that I can then call from --eval. The code isn't general enough that I want to have it always loaded in my ~/.mongorc.js file. I can work around it by using load() from inside --eval, but that's ugly.

$ cat foo.js
print("foo");
foo = function () { print("bar"); }
$ mongo foo.js --eval 'foo(); print("baz")'
MongoDB shell version: 2.4.9
connecting to: test
Wed May 28 12:16:43.912 ReferenceError: foo is not defined
$ mongo --eval 'load("foo.js"); foo(); print("baz")'
MongoDB shell version: 2.4.9
connecting to: test
foo
bar
baz
$ 



 Comments   
Comment by Kevin Pulo [ 28/May/14 ]

Would it be a viable alternative to process .js files and --eval in the order they're specified, and/or allow --eval to be specified multiple times?

Comment by Andy Schwerin [ 28/May/14 ]

The primary use of --eval with command line JS files is to pass arguments to those scripts by setting global interpreter state. The testing system relies on it. Reversing the order would entail restructuring all the jstests tests, so the use case would have to be pretty compelling.

Generated at Thu Feb 08 03:33:47 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.