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

repl: Fix debugger / require bugs #1605

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions lib/internal/repl.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
'use strict';

module.exports = {createRepl: createRepl};

const Interface = require('readline').Interface;
const REPL = require('repl');
const path = require('path');

module.exports = Object.create(REPL);
module.exports.createInternalRepl = createRepl;

// XXX(chrisdickinson): The 15ms debounce value is somewhat arbitrary.
// The debounce is to guard against code pasted into the REPL.
const kDebounceHistoryMS = 15;

try {
// hack for require.resolve("./relative") to work properly.
module.filename = path.resolve('repl');
} catch (e) {
// path.resolve('repl') fails when the current working directory has been
// deleted. Fall back to the directory name of the (absolute) executable
// path. It's not really correct but what are the alternatives?
const dirname = path.dirname(process.execPath);
module.filename = path.resolve(dirname, 'repl');
// XXX(chrisdickinson): hack to make sure that the internal debugger
// uses the original repl.
function replStart() {
return REPL.start.apply(REPL, arguments);
}

// hack for repl require to work properly with node_modules folders
module.paths = require('module')._nodeModulePaths(module.filename);

function createRepl(env, cb) {
const opts = {
ignoreUndefined: false,
Expand Down Expand Up @@ -114,8 +107,13 @@ function setupHistory(repl, historyPath, ready) {
}
repl._historyHandle = hnd;
repl.on('line', online);
repl.resume();
return ready(null, repl);

// reading the file data out erases it
repl.once('flushHistory', function() {
repl.resume();
ready(null, repl);
});
flushHistory();
}

// ------ history listeners ------
Expand Down
14 changes: 14 additions & 0 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ const Console = require('console').Console;
const domain = require('domain');
const debug = util.debuglog('repl');

try {
// hack for require.resolve("./relative") to work properly.
module.filename = path.resolve('repl');
} catch (e) {
// path.resolve('repl') fails when the current working directory has been
// deleted. Fall back to the directory name of the (absolute) executable
// path. It's not really correct but what are the alternatives?
const dirname = path.dirname(process.execPath);
module.filename = path.resolve(dirname, 'repl');
}

// hack for repl require to work properly with node_modules folders
module.paths = require('module')._nodeModulePaths(module.filename);

// If obj.hasOwnProperty has been overridden, then calling
// obj.hasOwnProperty(prop) will break.
// See: https://github.com/joyent/node/issues/1707
Expand Down
3 changes: 2 additions & 1 deletion src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@
// If -i or --interactive were passed, or stdin is a TTY.
if (process._forceRepl || NativeModule.require('tty').isatty(0)) {
// REPL
Module.requireRepl().createRepl(process.env, function(err, repl) {
var cliRepl = Module.requireRepl();
cliRepl.createInternalRepl(process.env, function(err, repl) {
if (err) {
throw err;
}
Expand Down