question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enable --inspect & --inspect-brk arguments on the build/develop args

See original GitHub issue

Summary

Add gatsby --inspect & and gatsby --inspect-brk commands to gatsby to enable easier debugging.

Now with node 10 we can use the https://nodejs.org/docs/latest-v10.x/api/inspector.html API to enable debugging programmatically, especially with the new gatsby develop changes.

For develop we could just enable it when we spawn the command.

Basic example

When gatsby --inspect is ran, in gatsby we do:

const inspector = require('inspector');

inspector.open()

Motivation

Fixes https://github.com/gatsbyjs/gatsby/issues/24349#issuecomment-635384602

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
piehcommented, Jun 2, 2020

I think that in gatsby develop “child process” is process that people most likely would want to debug (as this is the process where most of code is being executed, including all the plugins and user code) and optimizing for ease of setting up debugger for that process should be goal here (IMO).

So what about something in the middle between option 1) and 2):

  • --inspect-brk could set up child process (develop-process) using regular nodejs semantics (port 9229 unless specified otherwise via cli option)
  • --multi-process-inspect-brk - boolean switch which will open debug ports on all other process (parent process, jest-worker ones, etc) - this would be using random ports out of necessity. Separating this from “happy path” also have some nice characteristics - depending on your setup ( a) everyone use vscode b) for profiling you need chrome-dev-tools/ndb) - connecting to multiple processes waiting for debugger might be a pain, so having happy path to only handle one of them seems like good idea in my head

(it doesn’t have to be single change to address all of those - I think addressing happy path first with plan on how to address more complex one is very reasonable)

What do you think?

1reaction
herecydevcommented, Jun 2, 2020

So the good news is I got it to debug the child process 🚀

However, there’s actually a non-trivial amount of choice/complexity here. I’ll try and articulate this as best as possible:

Parent process = develop.ts running before spawn is called Child process = develop-process.ts, runs as a result of the spawn call and handles the majority of what gatsby does

Option 1

Tell the child process to use the inspector on the default port (9229) or a chosen port if the user supplies one: --inspect-brk=10876. The upside of this is that vscode sees --inspect-brk and with auto-attach on will correctly attach to the child process and you can debug the majority of gatsby including user code. This would allow a simple gatsby dev --inspect-brk to work for most scenarios from the cmd line.

Downside: You’re unable to debug the parent code, although it’s minimal. Uses default port so only 1 debug at a time, unless you’re supplying a chosen port

Option 2

Tell the child process to use the inspector on a random port. By passing a “fake --inspect-brk” argument to the spawn command, vscode will attach to this automatically if autoAttachChildProcesses is true.

Advantage: Debug all the things! 🎉 + uses random ports to avoid collision. vscode end to end debugging is easy Disadvantage: Because the port is random it might not be intuiative for some tools to debug (i.e. it’s not the default 9229)

I feel like option 2 is better, but I’m also skewed on my opinion because almost everyone I know uses vscode. Another very important note is how the vscode javascript debugger preview negates any need for this; simply spawning another process will automatically attach.

@wardpeet let me know your thoughts!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Debug Node.js Apps using Visual Studio Code
The Visual Studio Code editor includes Node.js debugging support. Set breakpoints, step-in, inspect variables and more.
Read more >
Debugging - Getting Started | Node.js
Create a new Node.js debug configuration and hit Debug. --inspect will be used by default for Node.js 7+. To disable uncheck js.debugger ...
Read more >
Running and debugging Node.js | WebStorm Documentation
WebStorm recognizes --inspect , --inspect-brk , and now deprecated --debug flags so you can make any application accessible for debugging.
Read more >
What is the difference between args and runtimeArgs in ...
So it appears that runtimeArgs is to runtimeExecutable as args is to ... --inspect-brk=35238 node_modules\jest-cli\bin\jest.js --runInBand.
Read more >
Debugging the Build Process
Search for on debug and set attach to enable. Using VS Code's integrated terminal run node --nolazy node_modules/.bin/gatsby develop --inspect-brk instead ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found