gatsby: VS Code debugger broken (Exception: Inspector is already activated)

Gatsby has recently stopped working with defined debugger launch configurations in Visual Studio Code.

I don’t know if this was due to a change in Gatsby or in VS Code and/or its extensions; I just know it worked at some point in the last couple of months, and now it doesn’t.

Steps to reproduce

Update: I can reproduce this issue in a starter repo using the exact launch configuration from the docs:

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Gatsby develop",
      "type": "node",
      "request": "launch",
      "protocol": "inspector",
      "program": "${workspaceRoot}/node_modules/.bin/gatsby",
      "autoAttachChildProcesses": true,
      "args": ["develop", "--inspect-brk"],
      "stopOnEntry": false,
      "runtimeArgs": ["--nolazy"],
      "sourceMaps": false
    },
    {
      "name": "Gatsby build",
      "type": "node",
      "request": "launch",
      "protocol": "inspector",
      "program": "${workspaceRoot}/node_modules/.bin/gatsby",
      "args": ["build"],
      "stopOnEntry": false,
      "runtimeArgs": ["--nolazy"],
      "sourceMaps": false
    }
  ]
}

This is the line throwing an unhandled exception:

https://github.com/gatsbyjs/gatsby/blob/7e83aced6b5c9fc421fd8f3b67b77269a6b59f09/packages/gatsby/src/commands/develop-process.ts#L67

This is the exception:

Exception has occurred: Error [ERR_INSPECTOR_ALREADY_ACTIVATED]: Inspector is already activated. Close it with inspector.close() before activating it again.
  at Object.open (inspector.js:137:11)

Some things I’ve tried adjusting without success:

  • Changing "type": "node" to "type": "pwa-node" and/or setting debug.node.useV3 to true; either one should cause VS Code to use the new Node.js debugger
  • Moving "--inspect-brk" to runtimeArgs or removing it entirely

Workaround

Auto-attach is not affected. If I set VS Code setting debug.node.autoAttach to true and then run node --nolazy node_modules/.bin/gatsby develop --inspect-brk in the terminal, the debugger attaches and Gatsby runs without exception.

Environment

  System:
    OS: Linux 4.19 Debian GNU/Linux 9 (stretch) 9 (stretch)
    CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
    Shell: 4.4.12 - /bin/bash
  Binaries:
    Node: 12.18.3 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/bin/yarn
    npm: 6.14.6 - /usr/local/bin/npm
  Languages:
    Python: 2.7.13 - /usr/bin/python
  npmPackages:
    gatsby: ^2.24.52 => 2.24.52
    gatsby-source-sanity: ^6.0.3 => 6.0.3

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 22 (18 by maintainers)

Commits related to this issue

Most upvoted comments

It was a simple fix, so I put in a PR.

Hi, I build the JS debugger.

Two things:

  • @aaronadamsCA passing --inspect-brk to Gatsby is probably unnecessary. We --require a ‘bootloader’ script via NODE_OPTIONS which sets up debugging before the process (i.e. Gatsby) gets activated. Omitted the argument will supposedly cause Gatsby to not try to enter debug mode again which should bypass the error.
  • Throwing the error is new behavior as of https://github.com/nodejs/node/commit/dd5f209213a2b75bb386b44c296a059fc10dfb02. You can detect that the inspector is already open by seeing if inspector.url() !== undefined (eg)

I think this is still valid

Amazing, thank you @connor4312, removing --inspect-brk does indeed bypass the error.

@wardpeet, at this point this might just be a documentation thing? Compared to the current docs it seems this is now the better recommendation for launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Gatsby develop",
      "type": "pwa-node",
      "request": "launch",
      "program": "${workspaceRoot}/node_modules/.bin/gatsby",
      "args": ["develop"],
      "runtimeArgs": ["--nolazy"],
      "console": "integratedTerminal"
    },
    {
      "name": "Gatsby build",
      "type": "pwa-node",
      "request": "launch",
      "program": "${workspaceRoot}/node_modules/.bin/gatsby",
      "args": ["build"],
      "runtimeArgs": ["--nolazy"],
      "console": "integratedTerminal"
    }
  ]
}

In addition to removing --inspect-brk, other changes include:

  • Updated "type": "pwa-node" to opt into the new Node.js debugger; it ships with VS Code stable, works well, and will soon be the only option, so IMO it’s already the right choice
  • Removed "protocol" because it does nothing with "request": "launch"
  • Removed "autoAttachChildProcesses" and "stopOnEntry" because there’s no need to restate defaults
  • Removed "sourceMaps": false, I don’t see any documented/ticketed reason to recommend this
  • Added "console": "integratedTerminal" to be able to see Gatsby console output

Re-opened, i’ll have a look