Skip to content

Commit

Permalink
module: simplify --inspect-brk handling
Browse files Browse the repository at this point in the history
Previously in the CommonJS loader, --inspect-brk is implemented
checking whether the module points to the result of re-resolving
process.argv[1] to determine whether the module is the entry point.
This is unnecessarily complex, especially now that we store that
information in the module as kIsMainSymbol. This patch updates
it to simply check that symbol property instead.
  • Loading branch information
joyeecheung committed Nov 1, 2024
1 parent 2505e21 commit 0830002
Showing 1 changed file with 4 additions and 28 deletions.
32 changes: 4 additions & 28 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1530,32 +1530,7 @@ Module.prototype._compile = function(content, filename, format) {
return;
}

// TODO(joyeecheung): the detection below is unnecessarily complex. Using the
// kIsMainSymbol, or a kBreakOnStartSymbol that gets passed from
// higher level instead of doing hacky detection here.
let inspectorWrapper = null;
if (getOptionValue('--inspect-brk') && process._eval == null) {
if (!resolvedArgv) {
// We enter the repl if we're not given a filename argument.
if (process.argv[1]) {
try {
resolvedArgv = Module._resolveFilename(process.argv[1], null, false);
} catch {
// We only expect this codepath to be reached in the case of a
// preloaded module (it will fail earlier with the main entry)
assert(ArrayIsArray(getOptionValue('--require')));
}
} else {
resolvedArgv = 'repl';
}
}

// Set breakpoint on module start
if (resolvedArgv && !hasPausedEntry && filename === resolvedArgv) {
hasPausedEntry = true;
inspectorWrapper = internalBinding('inspector').callAndPauseOnStart;
}
}
const dirname = path.dirname(filename);
const require = makeRequireFunction(this, redirects);
let result;
Expand All @@ -1565,9 +1540,10 @@ Module.prototype._compile = function(content, filename, format) {
if (requireDepth === 0) { statCache = new SafeMap(); }
setHasStartedUserCJSExecution();
this[kIsExecuting] = true;
if (inspectorWrapper) {
result = inspectorWrapper(compiledWrapper, thisValue, exports,
require, module, filename, dirname);
if (this[kIsMainSymbol] && getOptionValue('--inspect-brk')) {
const { callAndPauseOnStart } = internalBinding('inspector');
result = callAndPauseOnStart(compiledWrapper, thisValue, exports,

Check failure on line 1545 in lib/internal/modules/cjs/loader.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'inspectorWrapper' is assigned a value but never used

Check failure on line 1545 in lib/internal/modules/cjs/loader.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'inspectorWrapper' is never reassigned. Use 'const' instead
require, module, filename, dirname);
} else {
result = ReflectApply(compiledWrapper, thisValue,
[exports, require, module, filename, dirname]);
Expand Down

0 comments on commit 0830002

Please sign in to comment.