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

node inspect crashes when tab-completing with a require #41793

Open
langdon-holly opened this issue Nov 16, 2018 · 14 comments
Open

node inspect crashes when tab-completing with a require #41793

langdon-holly opened this issue Nov 16, 2018 · 14 comments
Labels
debugger Issues and PRs related to the debugger subsystem. repl Issues and PRs related to the REPL subsystem.

Comments

@langdon-holly
Copy link

Steps to reproduce:

  1. Create an empty file named "test.js"
  2. Run node inspect test.js
  3. Enter the debug repl
  4. Type require("
  5. Press tab

Result: Node prints the following message to standard error, then exits with status 1.

There was an internal error in node-inspect. Please report this bug.
Cannot read property 'extensions' of undefined
TypeError: Cannot read property 'extensions' of undefined
    at REPLServer.complete (repl.js:1031:51)
    at REPLServer.completer (repl.js:492:14)
    at REPLServer.Interface._tabComplete (readline.js:476:8)
    at REPLServer.Interface._ttyWrite (readline.js:975:16)
    at REPLServer.self._ttyWrite (repl.js:719:7)
    at ReadStream.onkeypress (readline.js:178:10)
    at ReadStream.emit (events.js:182:13)
    at ReadStream.EventEmitter.emit (domain.js:441:20)
    at emitKeys (internal/readline.js:424:14)
    at emitKeys.next (<anonymous>)

Observed rules for reproduction:

  • test.js can be any JavaScript file.
  • A single quote may be substituted for the double quote in step 3.
  • require(" may be preceded by any JavaScript code.
  • Space characters may be typed between require and (".
  • The paren must be immediately followed by the quote (no whitespace).
  • Any additional characters may be typed before pressing tab.
  • The cursor must be somewhere after the quote when pressing tab.
@Trott
Copy link
Member

Trott commented Nov 16, 2018

@jkrems Should this bug report be transferred to https://github.com/nodejs/node-inspect? Or is this the better location for it?

@jkrems
Copy link
Contributor

jkrems commented Nov 16, 2018

I think it would be best to transfer it to node-inspect since that is where it would need to be fixed.

@Trott Trott transferred this issue from nodejs/node Nov 16, 2018
@Trott
Copy link
Member

Trott commented Nov 16, 2018

I think it would be best to transfer it to node-inspect since that is where it would need to be fixed.

Done!

@militello
Copy link

I had the same problem, the cause was using vm context:
const vm = require('vm');
r.context = vm.createContext(mycontext);
changed to use context directly and it works again
r.context.something = something

@Trott
Copy link
Member

Trott commented Apr 28, 2021

I'm unable to reproduce this bug. @langdon-holly Are you still seeing this issue in Node.js 16.x or 14.x?

@langdon-holly
Copy link
Author

@Trott I am unable to reproduce in v16.0.0.

@militello
Copy link

I was using 14.15.0

@militello
Copy link

This code is failing on LTS (tested on 14.15.0 / 14.16.1) with the problem reported:

const repl = require('repl');
const r = repl.start('teste> ');

const context = { x: 1, y: 2 };
r.context = require('vm').createContext(context);

It's the first time I create my own REPL so could be my mistake...

But if the problem is just with the auto-complete, if it was caught we could still require (without auto-complete) and continue to use the REPL.

This works...

const repl = require('repl');
const r = repl.start('teste> ');

r.context.x = 1;
r.context.y = 2;

@militello
Copy link

Is there any better way to make a complex context from an object to use in REPL?

@Trott
Copy link
Member

Trott commented May 2, 2021

@langdon-holly I'm curious if this might be Linux distribution and/or processor specific. I just installed an Ubuntu docker image on macOS, then used nvm to install Node.js 11.1.0 and could not reproduce. (I thought maybe I had to use Linux to replicate, but apparently it would be more than just that.)

@langdon-holly
Copy link
Author

I doubt it's processor-specific, but it could be OS-specific. I just tried node v11.1.0 on Arch Linux, again, and was able to reproduce. I also briefly looked at the source code, and it appears that the crash occurs at repl.js:1031 when completing a require, when the REPLServer's context has no 'require' property. Perhaps the OS or installation details affect the REPLServer's context?

@militello
Copy link

I'm running on ubuntu 20 here. Installed Node via ASDF.

@jkrems
Copy link
Contributor

jkrems commented May 14, 2021

For the original bug (require combined with tab completion): There's something special about require in the context of the debugger. We inject a runtime API for require which allows it to work in the debug repl (or Chrome DevTools console) without a "real" global require when not stopped in a callstack frame with a require in scope.

The exception looks like the REPL code to auto-complete require paths blindly assumes that any require would have a extensions property - but the runtime API one doesn't have any properties. So maybe the answer here is to add defensive code to the REPL server code? See:

node/lib/repl.js

Line 1228 in a03d3af

const extensions = ObjectKeys(this.context.require.extensions);

The require function in the command line API is set up here:

function installConsoleExtensions(commandLineApi) {
if (commandLineApi.require) { return; }
const { tryGetCwd } = require('internal/process/execution');
const CJSModule = require('internal/modules/cjs/loader').Module;
const { makeRequireFunction } = require('internal/modules/cjs/helpers');
const consoleAPIModule = new CJSModule('<inspector console>');
const cwd = tryGetCwd();
consoleAPIModule.paths = ArrayPrototypeConcat(
CJSModule._nodeModulePaths(cwd),
CJSModule.globalPaths
);
commandLineApi.require = makeRequireFunction(consoleAPIModule);
}

@Trott
Copy link
Member

Trott commented May 15, 2021

@nodejs/repl

@Trott Trott closed this as completed May 15, 2021
@Trott Trott reopened this May 15, 2021
@jkrems jkrems transferred this issue from nodejs/node-inspect Jan 31, 2022
@jkrems jkrems added repl Issues and PRs related to the REPL subsystem. debugger Issues and PRs related to the debugger subsystem. labels Jan 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
debugger Issues and PRs related to the debugger subsystem. repl Issues and PRs related to the REPL subsystem.
Projects
None yet
Development

No branches or pull requests

4 participants