Visual Studio Node: debug into Worker Threads (node 11)

眉间皱痕 提交于 2021-02-16 04:28:05

问题


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?


回答1:


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

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




回答2:


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



来源:https://stackoverflow.com/questions/54251797/visual-studio-node-debug-into-worker-threads-node-11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!