Skip to content

Commit a66268e

Browse files
committed
Support --inspect-brk
1 parent 0baaf01 commit a66268e

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

lib/internal/modules/esm/loader.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,12 @@ class ModuleLoader {
382382
return onImport.traceSync(() => {
383383
const request = { specifier: url, phase: kEvaluationPhase, attributes: kEmptyObject, __proto__: null };
384384
const job = this.getOrCreateModuleJob(undefined, request, kImportInImportedESM);
385-
job.module.instantiate();
385+
if (getOptionValue('--inspect-brk')) {
386+
const { callAndPauseOnStart } = internalBinding('inspector');
387+
callAndPauseOnStart(job.module.instantiate, job.module);
388+
} else {
389+
job.module.instantiate();
390+
}
386391
if (job.module.hasAsyncGraph) {
387392
return { __proto__: null, module: job.module, completed: false };
388393
}

lib/internal/modules/run_main.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,9 @@ function executeUserEntryPoint(main = process.argv[1]) {
157157
const mainPath = resolvedMain || main;
158158
const mainURL = getOptionValue('--entry-url') ? new URL(mainPath, getCWDURL()) : pathToFileURL(mainPath);
159159

160-
// When no async hooks or --inspect-brk are needed, try the fully synchronous path first.
160+
// When no async hooks are needed, try the fully synchronous path first.
161161
// This avoids creating any promises during startup.
162-
if (!getOptionValue('--inspect-brk') &&
163-
getOptionValue('--experimental-loader').length === 0 &&
162+
if (getOptionValue('--experimental-loader').length === 0 &&
164163
getOptionValue('--import').length === 0) {
165164
const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader();
166165
const { module: entryModule, completed } = cascadedLoader.importSyncForEntryPoint(mainURL.href);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
// Test that --inspect-brk pauses at the first line of an ESM entry point.
5+
6+
common.skipIfInspectorDisabled();
7+
8+
const assert = require('assert');
9+
const fixtures = require('../common/fixtures');
10+
const { NodeInstance } = require('../common/inspector-helper.js');
11+
12+
async function testBreakpointOnStart(session) {
13+
const commands = [
14+
{ 'method': 'Runtime.enable' },
15+
{ 'method': 'Debugger.enable' },
16+
{ 'method': 'Debugger.setPauseOnExceptions',
17+
'params': { 'state': 'none' } },
18+
{ 'method': 'Debugger.setAsyncCallStackDepth',
19+
'params': { 'maxDepth': 0 } },
20+
{ 'method': 'Profiler.enable' },
21+
{ 'method': 'Profiler.setSamplingInterval',
22+
'params': { 'interval': 100 } },
23+
{ 'method': 'Debugger.setBlackboxPatterns',
24+
'params': { 'patterns': [] } },
25+
{ 'method': 'Runtime.runIfWaitingForDebugger' },
26+
];
27+
28+
await session.send({ method: 'NodeRuntime.enable' });
29+
await session.waitForNotification('NodeRuntime.waitingForDebugger');
30+
await session.send(commands);
31+
await session.send({ method: 'NodeRuntime.disable' });
32+
await session.waitForBreakOnLine(0, session.scriptURL());
33+
}
34+
35+
async function runTests() {
36+
const child = new NodeInstance(['--inspect-brk=0'], '', fixtures.path('es-modules', 'loop.mjs'));
37+
const session = await child.connectInspectorSession();
38+
39+
await testBreakpointOnStart(session);
40+
await session.runToCompletion();
41+
42+
assert.strictEqual((await child.expectShutdown()).exitCode, 55);
43+
}
44+
45+
runTests().then(common.mustCall());

0 commit comments

Comments
 (0)