From 8ab31402c100a0dc622622d36288ce9f7d91519a Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sun, 3 Nov 2024 17:11:34 +0000 Subject: [PATCH] module: simplify --inspect-brk handling 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. PR-URL: https://github.com/nodejs/node/pull/55679 Reviewed-By: Marco Ippolito Reviewed-By: Benjamin Gruenbaum --- lib/internal/modules/cjs/loader.js | 42 +++--------------------------- 1 file changed, 4 insertions(+), 38 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index da948167e7fdd6..3f161daa7f3c55 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -1332,15 +1332,6 @@ Module.prototype.require = function(id) { } }; -/** - * Resolved path to `process.argv[1]` will be lazily placed here - * (needed for setting breakpoint when called with `--inspect-brk`). - * @type {string | undefined} - */ -let resolvedArgv; -let hasPausedEntry = false; -/** @type {import('vm').Script} */ - /** * Resolve and evaluate it synchronously as ESM if it's ESM. * @param {Module} mod CJS module instance @@ -1542,32 +1533,6 @@ 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; @@ -1577,9 +1542,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, + require, module, filename, dirname); } else { result = ReflectApply(compiledWrapper, thisValue, [exports, require, module, filename, dirname]);