From 8817c16a195d398c5091d71273e5661e01076904 Mon Sep 17 00:00:00 2001 From: Denis Semenenko Date: Sun, 9 Feb 2020 02:47:53 -0800 Subject: [PATCH 1/2] feat: auto-resume on start breakpoint --- lib/_inspect.js | 12 ++++++++++++ test/cli/launch.test.js | 20 ++++++++++++++++++++ test/cli/start-cli.js | 4 ++-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/_inspect.js b/lib/_inspect.js index aa1dcdc..bc7503f 100644 --- a/lib/_inspect.js +++ b/lib/_inspect.js @@ -170,7 +170,19 @@ class NodeInspector { this.domainNames.forEach((domain) => { this[domain] = createAgentProxy(domain, this.client); }); + let startBreakReached = false; this.handleDebugEvent = (fullName, params) => { + if (!startBreakReached && + process.env.NODE_INSPECT_RESUME_ON_START === '1' && + fullName === 'Debugger.paused' && + params && params.reason === 'Break on start') { + startBreakReached = true; + debuglog('Paused on start, but NODE_INSPECT_RESUME_ON_START' + + ' environment variable is set to 1, resuming'); + this.client.callMethod('Debugger.resume'); + return; + } + const [domain, name] = fullName.split('.'); if (domain in this) { this[domain].emit(name, params); diff --git a/test/cli/launch.test.js b/test/cli/launch.test.js index 087a46c..c4ff3d8 100644 --- a/test/cli/launch.test.js +++ b/test/cli/launch.test.js @@ -174,3 +174,23 @@ test('run after quit / restart', (t) => { .then(() => cli.quit()) .then(null, onFatal); }); + +test('auto-resume on start if the environment variable is defined', (t) => { + const script = Path.join('examples', 'break.js'); + + const cli = startCLI([script], [], { + env: { NODE_INSPECT_RESUME_ON_START: '1' } + }); + + return cli.waitForInitialBreak() + .then(() => { + t.match( + cli.breakInfo, + { filename: script, line: 10 }, + 'skips to the first breakpoint'); + }) + .then(() => cli.quit()) + .then((code) => { + t.equal(code, 0, 'exits with success'); + }); +}); diff --git a/test/cli/start-cli.js b/test/cli/start-cli.js index bec4270..32c666c 100644 --- a/test/cli/start-cli.js +++ b/test/cli/start-cli.js @@ -20,8 +20,8 @@ function isPreBreak(output) { return /Break on start/.test(output) && /1 \(function \(exports/.test(output); } -function startCLI(args, flags = []) { - const child = spawn(process.execPath, [...flags, CLI, ...args]); +function startCLI(args, flags = [], spawnOpts = {}) { + const child = spawn(process.execPath, [...flags, CLI, ...args], spawnOpts); let isFirstStdoutChunk = true; const outputBuffer = []; From 12530bfc603d0748f61da68a926f015c4afb987d Mon Sep 17 00:00:00 2001 From: Denis Semenenko Date: Sun, 19 Apr 2020 16:55:18 -0700 Subject: [PATCH 2/2] refactor: move resume on start logic to repl --- lib/_inspect.js | 12 ------------ lib/internal/inspect_repl.js | 8 ++++++++ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/_inspect.js b/lib/_inspect.js index bc7503f..aa1dcdc 100644 --- a/lib/_inspect.js +++ b/lib/_inspect.js @@ -170,19 +170,7 @@ class NodeInspector { this.domainNames.forEach((domain) => { this[domain] = createAgentProxy(domain, this.client); }); - let startBreakReached = false; this.handleDebugEvent = (fullName, params) => { - if (!startBreakReached && - process.env.NODE_INSPECT_RESUME_ON_START === '1' && - fullName === 'Debugger.paused' && - params && params.reason === 'Break on start') { - startBreakReached = true; - debuglog('Paused on start, but NODE_INSPECT_RESUME_ON_START' + - ' environment variable is set to 1, resuming'); - this.client.callMethod('Debugger.resume'); - return; - } - const [domain, name] = fullName.split('.'); if (domain in this) { this[domain].emit(name, params); diff --git a/lib/internal/inspect_repl.js b/lib/internal/inspect_repl.js index 7acb3c6..bfbedf6 100644 --- a/lib/internal/inspect_repl.js +++ b/lib/internal/inspect_repl.js @@ -782,6 +782,14 @@ function createRepl(inspector) { } Debugger.on('paused', ({ callFrames, reason /* , hitBreakpoints */ }) => { + if (process.env.NODE_INSPECT_RESUME_ON_START === '1' && + reason === 'Break on start') { + debuglog('Paused on start, but NODE_INSPECT_RESUME_ON_START' + + ' environment variable is set to 1, resuming'); + inspector.client.callMethod('Debugger.resume'); + return; + } + // Save execution context's data currentBacktrace = Backtrace.from(callFrames); selectedFrame = currentBacktrace[0];