VS Code is cool!
I set up a project here on Github to demonstrate debugging two processes— a parent process and a child.
The parent launch configuration looks like this:
{
"type": "node",
"request": "launch",
"name": "Parent",
"program": "${workspaceRoot}/parent.js",
"args": [],
"cwd": "${workspaceRoot}"
// ,"port": 5858
,"protocol": "auto"
}
Note that the
port:5858
line is commented out. If it’s included, everything works, but later, when we launch both parent and child together, there are problems.
Parent code:
console.log("Parent running")
const cp = require('child_process');
const child = cp.fork(`${__dirname}/child.js`,{
//force the debug port to the one in the launch config
execArgv: ["--debug=5859"]
,silent: true
// ,stdio: 'ipc'
}
);
//Child data does not go to console with VS Code
//So do this.
child.stdout.on("data", (m) => console.log(m.toString()));
const sender = () => {
child.send({ hello: 'world' });
}
child.on('message', (m) => {
console.log('PARENT got message:', m);
sender()
});
The parent has to pass
debug=5859
to the child, and the Child launch configuration has to have that port set:
{
"name": "Child",
"type": "node",
"request": "attach",
"port": 5859,
"address": "localhost",
"restart": false,
"sourceMaps": false,
"outFiles": [],
"localRoot": "${workspaceRoot}",
"remoteRoot": null,
"protocol": "legacy"
}
The child looks like this:
console.log("Child started\nu");
console.log(process.argv)
sender = () => {
process.send({ foo: 'bar' });
}
process.on('message', (m) => {
console.log('CHILD got message:', m);
sender()
});
//start by sending a message
sender();
The README.md file explains how to use the project
VS Code Child Process Debug
This is a VS code project that lets you open a debugger on a parent process and a child at the same time.
It shows several ways to connect:
- Connecting to one process at a time in debugger
- Connecting to both together in debugger
- Connecting to a parent started in a console
Connecting to one process at a time in debugger
- Switch to the debug view
- Set a breakpoint in parent.js in the line that reads:
console.log('PARENT got message:', m);
- Set a breakpoint in child.js in the line that reads:
console.log('CHILD got message:', m);
- Select
Program
as the launch configuration - Start the debugger by clicking on the green arrow key or F5. A debug widget
will appear and the debugger will stop inparent.js
and display the file in an editor pane. You’ll get output in the console that looks like this:
Debugging with inspector protocol because Node v6.10.0 was detected.
node --inspect=SOME PORT --debug-brk parent.js
Debugger listening on port 5858.
Warning: This is an experimental feature and could change at any time.
Debugger attached.
Parent running
Child started
[ '/home/mwolf/tools/node-v6.10.0-linux-x64/bin/node',
'/home/mwolf/tools/junk/child.js' ]
*******************
Like this one
- Choose
Child
in the launch configuration, and press the green arrow key to attach to the child.
Note: in this case F5 will not attach to the child, but cause the already running parent to continue and stop at the breakpoint again.
- The debug widget will now have a a drop-down list and let you switch between the parent (launch) process or to the child. You can also disconnect from one or the other
- If you press the green arrow in the debugger widget, or F5, the program will run to the next breakpoint—which by design is in the other process, display the code and stop there. Pressing F5 again moves it back.
- If you have two tab sets open and move one file to each set, then you’ll see it toggle back and forth as you hit successive breakpoints.
Connecting to both together
- Select the Program/Child launch configuration.
- Press F5 of click on the green arrow.
- The debugger will stop in parent.js and both
Disconnecting and reconnecting
If you disconnect from the parent, then the child disconnects as well. But if you disconnect from the child, you can reconnect using the
child
launch configuration
No comments:
Post a Comment