-
Notifications
You must be signed in to change notification settings - Fork 30.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lib: use debugger statement to break scripts in -e --inspect-brk #24946
Conversation
When wrapping scripts being evaluated with `-e` and `--inspect-brk`, instead of wrapping it with an internal binding: ``` process.binding('inspector').callAndPauseOnStart(function() { ${source}; }, {}) ``` simply insert a debugger statement: ``` debugger; ${source} ``` so that the wrapped script do not rely on any internal magic since we should treat it as a normal user land script. In addition, if the source starts with declarations, it now breaks before the declarations instead of skipping through the declarations and jumping to the first statement. This patch also introduces helpers for the inspector tests to calculate debug break line numbers programmatically, instead of hard-coding the numbers, since these numbers can change when the way we wrap different kinds of scripts change.
just to be sure, there isn't some API we can use to do this without modifying the source? |
Maybe, but I suspect it would be harder to implement because node/lib/internal/bootstrap/node.js Lines 666 to 687 in d695a01
And |
I think another alternative is to pass the result of a |
This had been discussed back when this was originally implemented - and I was informed that the wrapper is considered a part of Node.JS API and that some tools may be relying on a fixed length of the wrapper prefix. I do prefer solution with the debugger statement, but my understanding is that this may need to be cleared with the TSC or someone like that. Do ES6 modules get wrapped as well? |
@eugeneo I think that would be more critical for the CJS module evaluation, specifically, this part in the code base: node/lib/internal/modules/cjs/loader.js Lines 696 to 719 in 9fb4fa8
(BTW, #21573 is trying to change that part) This PR, however, is changing the wrapping of node/lib/internal/bootstrap/node.js Lines 666 to 687 in d695a01
(to summarize: |
@eugeneo for esm: node/lib/internal/modules/esm/module_job.js Lines 74 to 80 in 9fb4fa8
|
I assumed this was for all the codepaths. I understand the reasons explained is the first comment, but I believe all code paths should use same call so it is easier to maintain. |
It is already quite different, though, because this is what it conceptually looks like when evaluating a CJS module with const source = fs.readFileSync(filename); // and strip the BOM, etc.
const result = vm.runInThisContext(
`(function (exports, require, module, __filename, __dirname) {${source}})`, {
...
});
process.binding('inspector').callAndPauseOnStart(result, exports, require, module, filename, dirname); And this is what happens when evaluating a script with const source = ... // read from CLI
// modifies module, filename, ...
const wrappedSource = `
global.__filename = ${JSON.stringify(name)};
global.exports = exports;
global.module = module;
global.__dirname = __dirname;
global.require = require;
return require("vm").runInThisContext('
${JSON.stringify(`
process.binding('inspector').callAndPauseOnStart(function() {
${source};
}, {})`)}, { filename: ${JSON.stringify(name)}, displayErrors: true });
`;
const result = vm.runInThisContext(
`(function (exports, require, module, __filename, __dirname) {${wrappedSource}})`, {
...
});
result.call(exports, require, module, '[eval]-wrapper', dirname); In particular we are not calling And as #24931 demonstrates, calling |
Superseded by #25832 |
When wrapping scripts being evaluated with
-e
and--inspect-brk
,instead of wrapping it with an internal binding:
simply insert a debugger statement:
so that the wrapped script do not rely on any internal magic
since we should treat it as a normal user land script. In addition,
if the source starts with declarations, it now breaks before the
declarations instead of skipping through the declarations and jumping
to the first statement.
This patch also introduces helpers for the inspector tests to calculate
debug break line numbers programmatically, instead of hard-coding the
numbers, since these numbers can change when the way we wrap different
kinds of scripts change.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes