diff --git a/examples/alive.js b/examples/alive.js index 3cf557b..c8ad157 100644 --- a/examples/alive.js +++ b/examples/alive.js @@ -1,4 +1,3 @@ -'use strict'; let x = 0; function heartbeat() { ++x; diff --git a/examples/backtrace.js b/examples/backtrace.js index f5d71a1..f18b33e 100644 --- a/examples/backtrace.js +++ b/examples/backtrace.js @@ -1,4 +1,3 @@ -'use strict'; const { exports: moduleScoped } = module; function topFn(a, b = false) { diff --git a/examples/cjs/index.js b/examples/cjs/index.js index d5815e8..0ace6d9 100644 --- a/examples/cjs/index.js +++ b/examples/cjs/index.js @@ -1,5 +1,5 @@ -'use strict'; +const fourty = 40; const { add } = require('./other'); -const sum = add(40, 2); +const sum = add(fourty, 2); module.exports = sum; diff --git a/examples/cjs/other.js b/examples/cjs/other.js index efadaa1..44a9a43 100644 --- a/examples/cjs/other.js +++ b/examples/cjs/other.js @@ -1,4 +1,3 @@ -'use strict'; exports.add = function add(a, b) { return a + b; }; diff --git a/examples/exceptions.js b/examples/exceptions.js index 00c48ed..f57d48a 100644 --- a/examples/exceptions.js +++ b/examples/exceptions.js @@ -1,4 +1,3 @@ -'use strict'; let error = null; try { throw new Error('Caught'); diff --git a/examples/three-lines.js b/examples/three-lines.js index 4e1cb95..c17c7c1 100644 --- a/examples/three-lines.js +++ b/examples/three-lines.js @@ -1,4 +1,3 @@ -'use strict'; let x = 1; x = x + 1; module.exports = x; diff --git a/examples/use-strict.js b/examples/use-strict.js new file mode 100644 index 0000000..9fe4b8f --- /dev/null +++ b/examples/use-strict.js @@ -0,0 +1,2 @@ +'use strict'; +console.log('first real line'); diff --git a/lib/_inspect.js b/lib/_inspect.js index e80a516..5897887 100644 --- a/lib/_inspect.js +++ b/lib/_inspect.js @@ -22,6 +22,7 @@ 'use strict'; const { spawn } = require('child_process'); const { EventEmitter } = require('events'); +const net = require('net'); const util = require('util'); const runAsStandalone = typeof __dirname !== 'undefined'; @@ -90,6 +91,45 @@ function createAgentProxy(domain, client) { }); } +function portIsFree(host, port, timeout = 2000) { + const retryDelay = 150; + let didTimeOut = false; + + return new Promise((resolve, reject) => { + setTimeout(() => { + didTimeOut = true; + reject(new Error( + `Timeout (${timeout}) waiting for ${host}:${port} to be free`)); + }, timeout); + + function pingPort() { + if (didTimeOut) return; + + const socket = net.connect(port, host); + let didRetry = false; + function retry() { + if (!didRetry && !didTimeOut) { + didRetry = true; + setTimeout(pingPort, retryDelay); + } + } + + socket.on('error', (error) => { + if (error.code === 'ECONNREFUSED') { + resolve(); + } else { + retry(); + } + }); + socket.on('connect', () => { + socket.destroy(); + retry(); + }); + } + pingPort(); + }); +} + class NodeInspector { constructor(options, stdin, stdout) { this.options = options; @@ -130,8 +170,9 @@ class NodeInspector { process.once('SIGHUP', process.exit.bind(process, 0)); this.run() - .then(() => { - this.repl = startRepl(); + .then(() => startRepl()) + .then((repl) => { + this.repl = repl; this.repl.on('exit', () => { process.exit(0); }); @@ -141,15 +182,19 @@ class NodeInspector { } suspendReplWhile(fn) { - this.repl.rli.pause(); + if (this.repl) { + this.repl.rli.pause(); + } this.stdin.pause(); this.paused = true; return new Promise((resolve) => { resolve(fn()); }).then(() => { this.paused = false; - this.repl.rli.resume(); - this.repl.displayPrompt(); + if (this.repl) { + this.repl.rli.resume(); + this.repl.displayPrompt(); + } this.stdin.resume(); }).then(null, (error) => process.nextTick(() => { throw error; })); } @@ -164,7 +209,14 @@ class NodeInspector { run() { this.killChild(); - return this._runScript().then((child) => { + const { host, port } = this.options; + + const runOncePortIsFree = () => { + return portIsFree(host, port) + .then(() => this._runScript()); + }; + + return runOncePortIsFree().then((child) => { this.child = child; let connectionAttempts = 0; @@ -189,7 +241,6 @@ class NodeInspector { }); }; - const { host, port } = this.options; this.print(`connecting to ${host}:${port} ..`, true); return attemptConnect(); }); diff --git a/lib/internal/inspect_client.js b/lib/internal/inspect_client.js index d5ce46d..c247e2a 100644 --- a/lib/internal/inspect_client.js +++ b/lib/internal/inspect_client.js @@ -334,20 +334,7 @@ class Client extends EventEmitter { this.emit('close'); }); - Promise.all([ - this.callMethod('Runtime.enable'), - this.callMethod('Debugger.enable'), - this.callMethod('Debugger.setPauseOnExceptions', { state: 'none' }), - this.callMethod('Debugger.setAsyncCallStackDepth', { maxDepth: 0 }), - this.callMethod('Profiler.enable'), - this.callMethod('Profiler.setSamplingInterval', { interval: 100 }), - this.callMethod('Debugger.setBlackboxPatterns', { patterns: [] }), - this.callMethod('Runtime.runIfWaitingForDebugger'), - ]).then(() => { - this.emit('ready'); - }, (error) => { - this.emit('error', error); - }); + this.emit('ready'); }; return new Promise((resolve, reject) => { diff --git a/lib/internal/inspect_repl.js b/lib/internal/inspect_repl.js index c93d792..2de86b2 100644 --- a/lib/internal/inspect_repl.js +++ b/lib/internal/inspect_repl.js @@ -748,8 +748,8 @@ function createRepl(inspector) { .filter(({ location }) => !!location.scriptUrl) .map(({ location }) => setBreakpoint(location.scriptUrl, location.lineNumber + 1)); - if (!newBreakpoints.length) return; - Promise.all(newBreakpoints).then((results) => { + if (!newBreakpoints.length) return Promise.resolve(); + return Promise.all(newBreakpoints).then((results) => { print(`${results.length} breakpoints restored.`); }); } @@ -770,7 +770,8 @@ function createRepl(inspector) { const breakType = reason === 'other' ? 'break' : reason; const script = knownScripts[scriptId]; const scriptUrl = script ? getRelativePath(script.url) : '[unknown]'; - print(`${breakType} in ${scriptUrl}:${lineNumber + 1}`); + + const header = `${breakType} in ${scriptUrl}:${lineNumber + 1}`; inspector.suspendReplWhile(() => Promise.all([formatWatchers(true), selectedFrame.list(2)]) @@ -778,8 +779,10 @@ function createRepl(inspector) { if (watcherList) { return `${watcherList}\n${inspect(context)}`; } - return context; - }).then(print)); + return inspect(context); + }).then((breakContext) => { + print(`${header}\n${breakContext}`); + })); }); function handleResumed() { @@ -1026,7 +1029,30 @@ function createRepl(inspector) { aliasProperties(context, SHORTCUTS); } + function initAfterStart() { + const setupTasks = [ + Runtime.enable(), + Profiler.enable(), + Profiler.setSamplingInterval({ interval: 100 }), + Debugger.enable(), + Debugger.setPauseOnExceptions({ state: 'none' }), + Debugger.setAsyncCallStackDepth({ maxDepth: 0 }), + Debugger.setBlackboxPatterns({ patterns: [] }), + Debugger.setPauseOnExceptions({ state: pauseOnExceptionState }), + restoreBreakpoints(), + Runtime.runIfWaitingForDebugger(), + ]; + return Promise.all(setupTasks); + } + return function startRepl() { + inspector.client.on('close', () => { + resetOnStart(); + }); + inspector.client.on('ready', () => { + initAfterStart(); + }); + const replOptions = { prompt: 'debug> ', input: inspector.stdin, @@ -1035,6 +1061,7 @@ function createRepl(inspector) { useGlobal: false, ignoreUndefined: true, }; + repl = Repl.start(replOptions); // eslint-disable-line prefer-const initializeContext(repl.context); repl.on('reset', initializeContext); @@ -1044,14 +1071,8 @@ function createRepl(inspector) { repl.rli.emit('SIGINT'); }); - inspector.client.on('close', () => { - resetOnStart(); - }); - - inspector.client.on('ready', () => { - restoreBreakpoints(); - Debugger.setPauseOnExceptions({ state: pauseOnExceptionState }); - }); + // Init once for the initial connection + initAfterStart(); return repl; }; diff --git a/test/cli/backtrace.test.js b/test/cli/backtrace.test.js index 2e2ba4a..9cd8a82 100644 --- a/test/cli/backtrace.test.js +++ b/test/cli/backtrace.test.js @@ -19,11 +19,11 @@ test('display and navigate backtrace', (t) => { .then(() => cli.stepCommand('c')) .then(() => cli.command('bt')) .then(() => { - t.match(cli.output, `#0 topFn ${script}:8:2`); + t.match(cli.output, `#0 topFn ${script}:7:2`); }) .then(() => cli.command('backtrace')) .then(() => { - t.match(cli.output, `#0 topFn ${script}:8:2`); + t.match(cli.output, `#0 topFn ${script}:7:2`); }) .then(() => cli.quit()) .then(null, onFatal); diff --git a/test/cli/exceptions.test.js b/test/cli/exceptions.test.js index 6d3ff68..b66c09f 100644 --- a/test/cli/exceptions.test.js +++ b/test/cli/exceptions.test.js @@ -31,11 +31,11 @@ test('break on (uncaught) exceptions', (t) => { .then(() => cli.command('breakOnException')) .then(() => cli.stepCommand('c')) .then(() => { - t.match(cli.output, `exception in ${script}:4`); + t.match(cli.output, `exception in ${script}:3`); }) .then(() => cli.stepCommand('c')) .then(() => { - t.match(cli.output, `exception in ${script}:10`); + t.match(cli.output, `exception in ${script}:9`); }) // Next run: With `breakOnUncaught` it only pauses on the 2nd exception @@ -46,7 +46,7 @@ test('break on (uncaught) exceptions', (t) => { }) .then(() => cli.stepCommand('c')) .then(() => { - t.match(cli.output, `exception in ${script}:10`); + t.match(cli.output, `exception in ${script}:9`); }) // Next run: Back to the initial state! It should die again. diff --git a/test/cli/launch.test.js b/test/cli/launch.test.js index 962197e..99c6ce0 100644 --- a/test/cli/launch.test.js +++ b/test/cli/launch.test.js @@ -8,7 +8,9 @@ const startCLI = require('./start-cli'); test('examples/empty.js', (t) => { const script = Path.join('examples', 'empty.js'); const cli = startCLI([script]); - return cli.waitForPrompt() + + return cli.waitFor(/break/) + .then(() => cli.waitForPrompt()) .then(() => { t.match(cli.output, 'debug>', 'prints a prompt'); t.match( diff --git a/test/cli/preserve-breaks.test.js b/test/cli/preserve-breaks.test.js index a248b3a..8de8227 100644 --- a/test/cli/preserve-breaks.test.js +++ b/test/cli/preserve-breaks.test.js @@ -48,8 +48,17 @@ test('run after quit / restart', (t) => { }) .then(() => cli.command('breakpoints')) .then(() => { - t.match(cli.output, `#0 ${script}:2`); - t.match(cli.output, `#1 ${script}:3`); + if (process.platform === 'aix') { + // TODO: There is a known issue on AIX where the breakpoints aren't + // properly resolved yet when we reach this point. + // Eventually that should be figured out but for now we don't want + // to fail builds because of it. + t.match(cli.output, /#0 [^\n]+three-lines\.js\$?:2/); + t.match(cli.output, /#1 [^\n]+three-lines\.js\$?:3/); + } else { + t.match(cli.output, `#0 ${script}:2`); + t.match(cli.output, `#1 ${script}:3`); + } }) .then(() => cli.quit()) .then(null, onFatal); diff --git a/test/cli/use-strict.test.js b/test/cli/use-strict.test.js new file mode 100644 index 0000000..81f4d91 --- /dev/null +++ b/test/cli/use-strict.test.js @@ -0,0 +1,27 @@ +'use strict'; +const Path = require('path'); + +const { test } = require('tap'); + +const startCLI = require('./start-cli'); + +test('for whiles that starts with strict directive', (t) => { + const script = Path.join('examples', 'use-strict.js'); + const cli = startCLI([script]); + + function onFatal(error) { + cli.quit(); + throw error; + } + + return cli.waitFor(/break/) + .then(() => cli.waitForPrompt()) + .then(() => { + t.match( + cli.output, + /break in [^:]+:(?:1|2)[^\d]/, + 'pauses either on strict directive or first "real" line'); + }) + .then(() => cli.quit()) + .then(null, onFatal); +});