14
votes

Can VS Code's Javascript debugger be made to debug node 11's new "Worker Threads"? Worker threads are modelled after web workers with a small number of extra capabilities on top and are available from the new worker_threads package (see https://nodejs.org/api/worker_threads.html). Other than with node's sub processes, one can share memory with worker threads in the form of SharedArrayBuffers.

My VS Code launch configuration looks like that:

   {
        "type": "node",
        "request": "launch",
        "name": "Mocha Tests",
        "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
        "runtimeArgs": [
            "--experimental-wasm-threads",
            "--experimental-worker"
        ],
        "args": [
            "-u", "tdd",
            "--timeout", "100000",
            "--colors", "${workspaceFolder}/test"
        ],
        "internalConsoleOptions": "openOnSessionStart",
        "autoAttachChildProcesses": true
   }

I can debug into the main node script, but the "autoAttachChildProcesses" flag is not effective in attaching to the spawned off worker threads and neither is adding a "debugger" statement within the code that runs inside the worker thread.

They mention that the "inspector" package is not yet supported for worker threads in their reference documentation, so I guess this may explain why that is.

Against all these odds, has anyone still succeeded in debugging into worker threads inside VS Code?

2
I suppose at this time the only thing we can do is use worker code in the same file and switch with isMainThread - Felipe
actually nevermind. Even debugging a worker that was activated in the same file does not seem to work - Felipe
I have been doing some research on this and found out through WebStorm's release notes, that Node added support for inspecting worker_threads in 10.12. blog.jetbrains.com/webstorm/2018/10/webstorm-2018-3-eap-6 If WebStorm can do it I am sure that there must be a way to hook on to it with Chrome devtools at least, or even VSC...Will post back if I get any further - edoDev

2 Answers

4
votes

UPDATE Feb 2021 It is now possible to debug worker threads in VSC using vscode-js-debug as pointed out in the other answer. I have tested it and it works quite well. Webstorm remains a great alternative, with perhaps a bit more features built-in, but bear in mind that it is a paid product.

Disclaimer: The answer below describes my experience debugging worker threads, but not using VS Code, which was the original question. I thought it would be useful to know alternative options anyway, since it seems that as of today the only option to debug worker threads is Webstorm.

Webstorm option

It is possible to debug worker threads using Webstorm: https://blog.jetbrains.com/webstorm/2018/10/webstorm-2018-3-eap-6/ I have tried it and it works really well (as documented in the link above).

Chrome tools

I have tried debugging worker threads using chrome dev tools, following the same approach in which you would debug web workers, but it did not work. When debugging web workers, they show up like this in chrome tools

enter image description here

Unfortunately, worker threads do not. When a worker thread runs, the debugger does not show it, and does not let you put breakpoints nor step through the code. I suspect that this may be because of this: https://github.com/nodejs/node/issues/26609

VS Code

VSC does not have a feature to debug worker threads. Interestingly, VCS also does not have a feature to debug Web Workers. This is an explicit decision on their part: https://github.com/Microsoft/vscode-chrome-debug/issues/675

3
votes

In the latest release of vscode-js-debug debugging of workers is now supported when combined with a recent version of nodejs.