From 3689813fdd4a2290640c56759b517ca538adf83c Mon Sep 17 00:00:00 2001 From: Kelvin Jin Date: Fri, 30 Sep 2016 17:24:03 -0700 Subject: [PATCH] module: check -e flag in debug break setup When both --debug-brk and --eval are set, and a filename is specified, its full path is not set correctly, causing an error for relative filenames with './' omitted. For example, 'node --debug-brk -e 0 hello.js' throws an error. Since the script referenced by the filename is never run anyway, this change skips resolving its full path if both --debug-brk and --eval are set. PR-URL: https://github.com/nodejs/node/pull/8876 Reviewed-By: Ben Noordhuis Reviewed-By: James M Snell --- lib/module.js | 2 +- test/parallel/test-debug-brk.js | 91 +++++++++++++++++++++++---------- 2 files changed, 64 insertions(+), 29 deletions(-) diff --git a/lib/module.js b/lib/module.js index b065cab5d11e3c..f0d149723d8a6c 100644 --- a/lib/module.js +++ b/lib/module.js @@ -545,7 +545,7 @@ Module.prototype._compile = function(content, filename) { displayErrors: true }); - if (process._debugWaitConnect) { + if (process._debugWaitConnect && process._eval == null) { if (!resolvedArgv) { // we enter the repl if we're not given a filename argument. if (process.argv[1]) { diff --git a/test/parallel/test-debug-brk.js b/test/parallel/test-debug-brk.js index 7f099258417348..89d9c892145676 100644 --- a/test/parallel/test-debug-brk.js +++ b/test/parallel/test-debug-brk.js @@ -3,33 +3,68 @@ const common = require('../common'); const spawn = require('child_process').spawn; -var procStderr = ''; -var agentStdout = ''; -var needToSpawnAgent = true; -var needToExit = true; - -const procArgs = [`--debug-brk=${common.PORT}`, '-e', '0']; -const proc = spawn(process.execPath, procArgs); -proc.stderr.setEncoding('utf8'); - -const exitAll = common.mustCall((processes) => { - processes.forEach((myProcess) => { myProcess.kill(); }); -}); - -proc.stderr.on('data', (chunk) => { - procStderr += chunk; - if (/Debugger listening on/.test(procStderr) && needToSpawnAgent) { - needToSpawnAgent = false; - const agentArgs = ['debug', `localhost:${common.PORT}`]; - const agent = spawn(process.execPath, agentArgs); - agent.stdout.setEncoding('utf8'); - - agent.stdout.on('data', (chunk) => { - agentStdout += chunk; - if (/connecting to .+ ok/.test(agentStdout) && needToExit) { - needToExit = false; - exitAll([proc, agent]); +let run = () => {}; +function test(extraArgs, stdoutPattern) { + const next = run; + run = () => { + var procStdout = ''; + var procStderr = ''; + var agentStdout = ''; + var debuggerListening = false; + var outputMatched = false; + var needToSpawnAgent = true; + var needToExit = true; + + const procArgs = [`--debug-brk=${common.PORT}`].concat(extraArgs); + const proc = spawn(process.execPath, procArgs); + proc.stderr.setEncoding('utf8'); + + const tryStartAgent = () => { + if (debuggerListening && outputMatched && needToSpawnAgent) { + needToSpawnAgent = false; + const agentArgs = ['debug', `localhost:${common.PORT}`]; + const agent = spawn(process.execPath, agentArgs); + agent.stdout.setEncoding('utf8'); + + agent.stdout.on('data', (chunk) => { + agentStdout += chunk; + if (/connecting to .+ ok/.test(agentStdout) && needToExit) { + needToExit = false; + exitAll([proc, agent]); + } + }); } + }; + + const exitAll = common.mustCall((processes) => { + processes.forEach((myProcess) => { myProcess.kill(); }); }); - } -}); + + if (stdoutPattern != null) { + proc.stdout.on('data', (chunk) => { + procStdout += chunk; + outputMatched = outputMatched || stdoutPattern.test(procStdout); + tryStartAgent(); + }); + } else { + outputMatched = true; + } + + proc.stderr.on('data', (chunk) => { + procStderr += chunk; + debuggerListening = debuggerListening || + /Debugger listening on/.test(procStderr); + tryStartAgent(); + }); + + proc.on('exit', () => { + next(); + }); + }; +} + +test(['-e', '0']); +test(['-e', '0', 'foo']); +test(['-p', 'process.argv[1]', 'foo'], /^\s*foo\s*$/); + +run();