GoogleChromeLabs / ndb

ndb is an improved debugging experience for Node.js, enabled by Chrome DevTools
Apache License 2.0
10.9k stars 231 forks source link

CLI option to attach debugger to a port #249

Open hediet opened 5 years ago

hediet commented 5 years ago

I've implemented a library that makes spawning a debugger for node applications extremely easy. However, chrome does not allow opening chrome-devtools:// urls on launch - the user has to manually paste it into the address bar.

It would be awesome if I could use ndb for that! I.e. if ndb provides a CLI option to specify the debug port ndb connects to.

alexkozy commented 5 years ago

You can spawn ndb process and add file to os.homedir()/.ndb/ndd_store/<pid> With following content: {"targetListUrl":"http://127.0.0.1:<port>/json","argv":process.argv],"cwd":process.cwd()} ndb will detect this process and automatically attach to it. I am wondering how does your nice library get websocket url?

hediet commented 5 years ago

Thanks for your answer :) Does this allow debugging multiple processes in parallel? Generally, I don't like to pass launch arguments by writing into global files as this is prone to race conditions and other bugs.

Getting the websocket url is quite easy, the node debugger hosts a rest endoint for that.

Or do you mean how I get the url for the rest endoint? It's just localhost + port. I open the port with process._debug. I've put a sequence diagram in the Readme.

alexkozy commented 5 years ago

In general I prefer some node flag that will radically simplify node process discovery, I have open pull request for node: https://github.com/nodejs/node/pull/28235 . If it will get merge ndb will run ipc server and you will be able to send detected web socket URL to this server instead of writing to files.

More discussion about node process discovery: https://github.com/nodejs/diagnostics/issues/298

alexkozy commented 5 years ago

ndb@1.1.0 runs a little ipc server, external tool can send required information about process to it. User can get ipc address on Terminal tab in Console drawer (available by Esc), it is listed as NDD_IPC. For expected data format, please take a look on preload.js.

I will think about ways how I can eliminate dependency on NDD_IPC environment variable.