Skip to content

Line numbers reported from code in new Function are incorrect #32688

Closed
@connor4312

Description

@connor4312
  • Version: 13.12.0
  • Platform: Windows
  • Subsystem: unsure

What steps will reproduce the bug?

Copied from microsoft/vscode#94437

  1. Create a file containing
const code = `    console.log(arg1);
    const a = arg1 + 1;
    console.log(a);
    return a;`;

const fn = new Function('arg1', code);
console.log(process.version);
const result = fn(1);
console.log('result', result);

2 Either in Chrome devtools attached to the process, or a debugger like VS Code, set a breakpoint on const result = fn(1);
3. Step into and through the evaluated function

How often does it reproduce? Is there a required condition?

Every time

What is the expected behavior?

The debugger should show the line being evaluated currently

What do you see instead?

The debugger is two lines off (notice '1' is logged even though I haven't gotten there yet)

This does not happen when running the code directly Chrome or Chrome Canary; it seems to be Node-specific behavior.

Additional information

Looking in CDP, this is the data I see:

{
  "method": "Debugger.paused",
  "params": {
    "callFrames": [
      {
        "callFrameId": "{\"ordinal\":0,\"injectedScriptId\":1}",
        "functionName": "",
        "functionLocation": {
          "scriptId": "130",
          "lineNumber": -2,   // <- this part is weird!
          "columnNumber": 19
        },
        {
          "scriptId": "130",
          "lineNumber": 0, // <- should be 2, not 0
          "columnNumber": 4
        }
        "url": "",
        
        // ...

It seems like Node.js offsets the evaluated code so that the generated function head is before the 'start' of the file, I assume so that stacktraces show the right line. However, this messes up debuggers. Some suggestions:

  • Make the function header a single line prefixing the user's function, like the module wrapper, so that line numbers are identical.
  • In the Debugger.scriptParsed event, set the startLine to compensate for the header. I think this should make all debuggers 'just work' provided they deal with the lineNumber as a signed integer.
  • Don't send the wrapper code in Debugger.getScriptSource

Metadata

Metadata

Assignees

No one assigned

    Labels

    inspectorIssues and PRs related to the V8 inspector protocol

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions