Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace Node.js REPL with plain vm context for script usage MONGOSH-1720 #1849

Merged
merged 5 commits into from
Mar 6, 2024

Conversation

addaleax
Copy link
Contributor

@addaleax addaleax commented Feb 29, 2024

[Definitely recommend viewing the diff in whitespace-changes-disabled mode :)]

  • Allow running MongoshNodeRepl instances either based on:
    • The existing mechanism for running code, which is spinning up
      a Node.js REPL and using it to evaluate code; or
    • A more lightweight mechanism that only creates a vm context
      and then run code using that context directly.
  • Introduce a new command-line switch, --jsContext, that can be
    used to explicitly select the desired behavior, with possible values
    of repl, plain-vm and auto. The default behavior is to switch
    depending on whether the CLI will enter interactive mode or not.

Running in plain-vm mode will significantly improve runtime
performance (a 6× reduction of script run time in local testing),
coming from the removal of async context tracking that the REPL
uses to identify async operations which were originally spawned
from the REPL instance in question.

This lack of async context tracking comes with some implications,
in particular asynchronously thrown errors (in the Node.js sense,
e.g. setImmediate(() => { throw ... })) will lead to slightly
different behavior (e.g. different error code and error output).
That is probably acceptable breakage in this context.

…GOSH-1720

- Allow running `MongoshNodeRepl` instances either based on:
  - The existing mechanism for running code, which is spinning up
    a Node.js REPL and using it to evaluate code; or
  - A more lightweight mechanism that only creates a `vm` context
    and then run code using that context directly.
- Introduce a new command-line switch, `--jsContext`, that can be
  used to explicitly select the desired behavior, with possible values
  of `repl`, `plain-vm` and `auto`. The default behavior is to switch
  depending on whether the CLI will enter interactive mode or not.

Running in `plain-vm` mode will significantly improve runtime
performance (a 6× reduction of script run time in local testing),
coming from the removal of async context tracking that the REPL
uses to identify async operations which were originally spawned
from the REPL instance in question.

This lack of async context tracking comes with some implications,
in particular asynchronously thrown errors (in the Node.js sense,
e.g. `setImmediate(() => { throw ... })`) will lead to slightly
different behavior (e.g. different error code and error output).
That is probably acceptable breakage in this context.
@addaleax addaleax changed the title [WIP] investigate replacing Node.js REPL with plain vm context for script usage MONGOSH-1720 feat: replace Node.js REPL with plain vm context for script usage MONGOSH-1720 Mar 1, 2024
@addaleax addaleax marked this pull request as ready for review March 1, 2024 16:20
// for debugging
if (process.env.MONGOSH_DISPLAY_STARTUP_STACK_TRACE)
console.error(e?.stack);
else console.error(`${e?.name}: ${e?.message}`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just for us, it's pretty annoying to have the stack trace not showing up for errors occuring during startup

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should probably already know this answer, but why don't we display the stack trace by default all the time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original idea here was that mongosh wouldn't display stack traces because they often do not contain relevant information for users. Maybe this is something we can revisit after https://jira.mongodb.org/browse/NODE-5556 where the driver team aims to implement proper async stack traces.

@@ -160,7 +160,7 @@ export class TestShell {
return this._rawOutput;
}

get process(): ChildProcess {
get process(): ChildProcessWithoutNullStreams {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a drive-by to fix type errors showing up in the e2e.spec.ts file

@addaleax addaleax merged commit 21f7968 into main Mar 6, 2024
60 of 62 checks passed
@addaleax addaleax deleted the 1720-dev branch March 6, 2024 10:48
@MarcusElevait
Copy link

Why is this not versioned as a breaking change? This breaks javascript file executions that uses native things like require("fs")

@tiagoliveira9
Copy link

Yes, this should be versioned as a breaking change, I have several scripts impacted with these changes

@tiagoliveira9
Copy link

And the helper doesn't show up the new --jsContext option

@addaleax
Copy link
Contributor Author

@MarcusElevait @tiagoliveira9 Just for clarity, this was unintended breakage and we released mongosh 2.2.1 soon after to fix it: https://github.com/mongodb-js/mongosh/releases/tag/v2.2.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants