denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
93.51k stars 5.19k forks source link

Not possible to debug worker #13572

Open jonek opened 2 years ago

jonek commented 2 years ago

It seems the debugger in VSCode and Chrome never stops at breakpoints in worker code.

Are there any variables, specific Deno command line flags or worker options I have to use to be able to debug worker code?

jonek commented 2 years ago

This is the main.js I used:

console.log('main: started');
debugger;
const myWorker = new Worker(new URL("./worker.js", import.meta.url).href, { type: "module" });
console.log('main: spawned', myWorker);

This is the worker.js:

try {
    console.log('worker: hello from worker');
    debugger;
 } catch(e) {
    console.error('error', e);
}

I also found this: https://github.com/denoland/deno/blob/1fb5858009f598ce3f917f9f49c466db81f4d9b0/runtime/web_worker.rs#L448

It seems to indicate, that the inspector will never stop in worker code.

I launched Deno with --inspect-brk.

VSCode was showing either the output of the main worker or the output of the worker in the debugger view. It seemed to choose either one indeterministically.

bartlomieju commented 2 years ago

@jonek that flag only indicates that debugger shouldn't break on the first line of worker code. I will verify how it works in VSCode, but in Chrome Devtools (chrome://inspect) once the worker is launched you will see a new entry show up that allows you to connect to it.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.

vazexqi commented 1 year ago

@bartlomieju - Any updates on this? Is this a limitation of deno? Or, of the deno VS Code extension?

I have only been able to get this to work if I use it with the Chrome Devtools (chrome://inspect) but I was hoping to have it work inside VS Code.

Screen Shot 2022-08-26 at 11 11 49 AM
bartlomieju commented 1 year ago

This is something I will be tackling in October as I'm OOO for the next two weeks.

bartlomieju commented 1 year ago

FYI I'm investigating this issue - it turned out to be much more complex than I expected. The way it works for Node is by using a custom "NodeWorker" domain for Chrome Devtools Protocol that is not really standardized. I will try to get in touch with VSCode team to see if there's a way forward to make it work more like in browser, if that won't be possible we'll look into using the same mechanism as Node does.

ntotten commented 1 year ago

Anyone interested in working on this on a sponsored basis? This is a major annoyance, but I’m not at all familiar with the internals at this level so I’d be willing to fund this work. Let me know.

jespertheend commented 11 months ago

I managed to trigger a breakpoint from code run in a worker.

mommysgoodpuppy commented 1 month ago

To my surprise I found a workaround to get worker breakpoints to hit in VSCode.

Unfortunately this is still highly inconsistent, I think the debugger accidentally attaches to the same process twice a lot of the time. If anyone knows how to explicitly have the ports between main and workers be different or different ws addresses let me know. https://github.com/mommysgoodpuppy/DenoDebugWebworkersExample