Skip to content

The repl module's default setting for useGlobal invites hard-to-isolate bugs #13827

Description

@searls
  • Version: v8.1.2
  • Platform: Darwin bayswater.local 16.6.0 Darwin Kernel Version 16.6.0: Fri Apr 14 16:21:16 PDT 2017; root:xnu-3789.60.24~6/RELEASE_X86_64 x86_64
  • Subsystem: repl

I think the built-in repl module's default option value of useGlobal: false is likely very surprising for many use cases, and should be reconsidered. For reference, the node CLI sets this value manually to true, so the REPL that Node users are accustomed to has their code run in a single shared context.

What I just discovered today, while working on testdouble.js with its little repl script is that the consequences of useGlobal defaulting to false are quite striking. We found a very insiduous bug that manifested itself as functions defined in the REPL not seeming to have Object or Function in their prototype chain (which, of course, every function does).

After lots of digging, we discovered it was due to the fact that useGlobal is false by default. For a minimal example of how absurd this seems, see the following output:

$ node -e 'require("repl").start()'
> setTimeout instanceof Function
false

Of course, it's ridiculous that setTimeout, or any built-in/host method would fail an instanceof check with Function. With the node CLI or the useGlobal option manually set to true, however, things behave much more akin to a real-world Node.js program.

$ node -e 'require("repl").start({useGlobal: true})'
> setTimeout instanceof Function
true
$ node
> setTimeout instanceof Function
true

As a result, it seems to me that useGlobal: true would have been a more sensible default for the built-in repl module, but since it's stable, maybe we can at least document the ramifications of this quirk in behavior. Thoughts?

Activity

  1. added
    replIssues and PRs related to the REPL subsystem.
    on Jun 20, 2017
  2. added
    docIssues and PRs related to Node.js documentation.
    good first issueIssues that are suitable for first-time contributors.
    on Jun 21, 2017
  3. bnoordhuis commented on Jun 21, 2017

    @bnoordhuis
    Member

    A documentation pull request would be welcome.

    Of course, it's ridiculous that setTimeout, or any built-in/host method would fail an instanceof check with Function.

    That's due to how stuff is copied over from the main context. Should probably be fixed but that's going to be a fair bit of work.

  4. added
    good first issueIssues that are suitable for first-time contributors.
    and removed
    good first issueIssues that are suitable for first-time contributors.
    on Jun 21, 2017
  5. added a commit that references this issue on Jun 23, 2017
  6. vishwasrao commented on Jul 12, 2017

    @vishwasrao

    Hi, I would like to work on this.
    Eager to work on my first contribution.

  7. gibfahn commented on Jul 13, 2017

    @gibfahn
    Member

    @vishwasrao looks like @starkwang already raised a Pull Request for this (#13866).

    I'll take off the good first contribution label

  8. removed
    good first issueIssues that are suitable for first-time contributors.
    on Jul 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to Node.js documentation.replIssues and PRs related to the REPL subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions