13

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?

Soeren
  • 541
  • 3
  • 9
  • I suppose at this time the only thing we can do is use worker code in the same file and switch with `isMainThread` – Felipe Feb 20 '19 at 00:49
  • actually nevermind. Even debugging a worker that was activated in the same file does not seem to work – Felipe Feb 20 '19 at 00:55
  • 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. https://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 Mar 26 '19 at 09:54

2 Answers2

4

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

edoDev
  • 481
  • 1
  • 3
  • 14
  • 1
    Typical Microsoft response to something that could make their product a LOT more usable - it would take too much work to do - how about making OUR lives easier MS ?? guess I will fall back on 1980 debugging technique : console.log('HERE'); – MLissCetrus May 18 '20 at 16:05
  • Did you have to configure anything to make it work? It doesn't work for me in my CRA web app. – Mike Lischke Mar 04 '21 at 12:39
3

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

Steven
  • 1,513
  • 16
  • 16
  • Thanks for posting this. I tested it and it works really well. Considering that Webstorm is a paid product and VSC + vscode-js-debug is not it has a clear advantage since the functionality is the same now. – edoDev Feb 12 '21 at 09:17
  • I have both, the vscode-js-debug preview enabled and a recent version of Node.js. Still vscode is not able to debug web workers for me. Breakpoints aren't validated and do not stop execution. – Mike Lischke Mar 04 '21 at 12:29