-
Notifications
You must be signed in to change notification settings - Fork 300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🚀 Feature Request: Breakpoint debugging support in workerd
#371
Comments
@threepointone, didn't you manage to get this working with VS Code? |
I tried it with the configuration given in the screenshot: {
"type": "node",
"request": "attach",
"name": "Attach to remote",
"address": "127.0.0.1",
"port": 9229
} ...but in my case it did not work. I got a couple of issues. Somehow the debugger has problems attaching. Sometimes it does, sometimes it does not. A common error showing up in wrangler is:
While the debugger complains that its session has ended: I managed to get one step ahead and get it briefly attached, just to see that the breakpoints still aren't working. This time the message looks as follows: It suggested to add Back to "console.log" debugging 🤔 |
Just ask VSCode to debug the wrangler cli.ts and it'll oblige, creating a launch.json in .vscode Then add an "args" key to provide normal wrangler args, plus here I'm asking it to run a particular worker script. {
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/packages/wrangler/src/cli.ts",
"outFiles": [
"${workspaceFolder}/**/*.js"
],
"args": ["dev", "/Users/you/worker/src/index.ts"]
}
]
} |
Sorry for the late reply, but it did not work.
After I added
Running |
@dagnelies can you share your launch.json if it's working? I have the same error. Edit: Oh, you probably mean running I managed to get VS Code to launch the project, but breakpoints aren't working. It also doesn't have quite the same output in the debug console as running in terminal, e.g. it doesn't state the localhost address or port being listened on. In package.json
In launch.json
|
.vscode/launch.json
during project creationworkerd
Hi all! I've updated the title of this to reflect the internal work that's required here |
Fundamentally everything that is needed for this is in place except for the fact that when a breakpoint is hit we have to be able to sleep the request thread. This implies that the dev protocol connection needs to be run on a separate thread. @kentonv probably has the most insight into how to make that work. |
I am not sure, but I think we just need to set up a dedicated thread to run the inspector socket listen loop (in server.c++), thinking carefully about synchronization issues. Luckily the types in |
Seems related to this PR, but it has recently been closed without indications as to why: #595 |
Thank you very much for the prompt answer. |
This change place the InspectorService in a separate thread that manages communication with over the websocket to the inspector. A step towards supporting breakpoints for workerd.
This change place the InspectorService in a separate thread that manages communication with over the websocket to the inspector. A step towards supporting breakpoints for workerd.
This change place the InspectorService in a separate thread that manages communication with over the websocket to the inspector. A step towards supporting breakpoints for workerd.
Supports breakpoints, debugger statements, stepping through code, live edit when applicable. This largely comes for free by having a separate thread for V8 inspector messages and implementing runMessageLoopOnPause. Bug: #371 Bug: EW-7264
Do we have any update or ETA to enable debugging? My team really want to use Cloudflare Pages and Page Workers but without proper debugging I cannot possibly justify the additional time required to get to market. |
This change places the InspectorService in a separate thread that manages communication with over the websocket to the inspector. The change also adds support for runMessageLoopOnPause() and quitMessageLoopOnPause() to support breakpoints and debugger break statements. There is also refactoring of the CDP message handling code so it can be called with or without the isolate lock held. This requires workerd to run with the command-line flag -i to turn on inspector support. This change only works for single service configurations. Support for multi service configurations to follow. To try this out using samples/helloworld as an example: 1) Edit "samples/helloworld/worker.js" and add a debugger statement to the handle(request) method. 2) Open workerd in VSCode, select 'workerd with inspector enabled (dbg)' as the Run and Debug Target panel. Hit F5 to run and select `samples/helloworld/config.capnp` as the config to use. 3) Open devtools in Chrome using either: * https://devtools.devprod.cloudflare.dev/js_app?ws=localhost:9229/main * chrome:://inspect 4) On the command-line run, `curl http://localhost:8080/` 5) Devtools should break into the running worker. Bug: #371 Test: manual Test: existing internal ew tests do not break
This change places the InspectorService in a separate thread that manages communication with over the websocket to the inspector. The change also adds support for runMessageLoopOnPause() and quitMessageLoopOnPause() to support breakpoints and debugger break statements. There is also refactoring of the CDP message handling code so it can be called with or without the isolate lock held. This requires workerd to run with the command-line flag -i to turn on inspector support. This change only works for single service configurations. Support for multi service configurations to follow. To try this out using samples/helloworld as an example: 1) Edit "samples/helloworld/worker.js" and add a debugger statement to the handle(request) method. 2) Open workerd in VSCode, select 'workerd with inspector enabled (dbg)' as the Run and Debug Target panel. Hit F5 to run and select `samples/helloworld/config.capnp` as the config to use. 3) Open devtools in Chrome using either: * https://devtools.devprod.cloudflare.dev/js_app?ws=localhost:9229/main * chrome:://inspect 4) On the command-line run, `curl http://localhost:8080/` 5) Devtools should break into the running worker. Bug: #371 Test: manual Test: existing internal ew tests do not break
Here’s the current PR for Wrangler: cloudflare/workers-sdk#3774. It’ll require a Miniflare bump before it can land—but we’re on the home stretch now 🥹 (or if you use |
I Need this for webstorm, how the old version support attach debugger and this does not, |
We can close this issue, it shipped in Wrangler 3.7.0! |
Thanks @huw, that's the last piece. |
This is fantastic. Thanks for all the hard work! I can set a breakpoint on the JS bundle generated by esbuild (it's something), but of course I really want to set my breakpoint on my TypeScript API (the dream). I tried Identifying the right process to attach to is also unclear (I use pnpm to run these in parallel: 1) esbuild src/index.ts --bundle --watch --format=esm --outfile=dist/_worker.js 2) wrangler pages dev dist --live-reload) After looking through the existing issues, I was going to raise a new issue requesting a short explanation for "How to breakpoint debugging on TypeScript code running under pages locally", but I couldn't see a fit with any of the available categories... Does anyone have any suggestions on how to achieve this or how to raise the requirement? Thanks again! (Sorry for such a long comment) |
Hey @joseph-simpson! 👋 Try adding |
@mrbbot What exactly is d supposed to do? I get that it says "open Devtools" but it has never done anything for me. I just assumed it was an uncompleted placeholder for something. Is there a sample .vscode/launch.json config that can be shared so that we can get debugging working properly in VSCode now? |
Hey @tforster! 👋 d should open Breakpoint debugging is currently supported using these DevTools, or WebStorm's. I've struggled to get VSCode's debugger working. 😕 VSCode's |
The |
Put a PR up to add support for VSCode's debugger to Miniflare: cloudflare/miniflare#681. We'll need to make a few Wrangler changes too, but these should be done soon. 🙂 |
Actually, I do. However, I am running Wrangler in Ubuntu 22.04 under WSL2 on Windows. So Wrangler is on the Ubuntu side but my Chrome is on Windows. I had no idea that "d" was supposed to open Chrome Dev Tools. But now I do I have been able to open it manually. Do you happen to know where the code is that launches Chrome from the "d" keypress (e.g. what repo? Maybe even where in the repo?) If you can point me in that direction I can take a look and perhaps figure out how to get it to work in WSL2 world.
Breakpoint debugging is something I have taken for granted since I first experienced it with Turbo Pascal on DOS back in the late 80's. So much so that I never stopped to think about how it works. From day 1 JavaScript debugging in VSCode just worked out of the box. And, the Serverless Framework has the serverless-offline plugin making debugging AWS Lambda's a breeze too. CloudFlare workers are the first time I have come across the inability to debug in many years. So, it's really good to understand the intricacies and challenges of the process. I am going to read the VSCode docs you linked so I have a better appreciation and understanding. Thanks for all your work on this!!! |
Thanks for offering to take a look! 😃 |
When using webstorm's debugger, the worker freezes and all subsequent requests will time out. The devtools debugger works fine. |
@ben-xD can you open a new ticket and post repro instructions for the issue in WebStorm and/or capture a Chrome Debugger Protocol trace in WebStorm? Thanks |
Just tried wrangler 3.9 which I believe has these changes. Unfortunately it doesn't seem to be quite there yet. The problem from what I see is that the sourcemap generated for the bundle by wrangler's toolchain doesn't follow along from previous files correctly. Eg: right now if I run the app with VSCode's debugger, it correctly maps the bundle (under "deployed" in the devtools) to the compiled file my tooling is generating, but if sourcemaps were processed correctly, it should map all the way to the original files: You can see in the screenshot that the bundle (ick3smw8et.js) maps correctly to The issue happens both in Visual Studio Code, or at the Cloudflare devtool url. If I copy paste the Considering mapping from my own bundler's output, to wrangler's bundler output, seems to be working correctly, I have a feeling if the final sourcemap was generated using the previous sourcemaps, this would "Just work". Sorry for the very convoluted explanation! I know just enough about the topic to be dangerous. Update: Yeah, I can confirm thats (at least part of) the issue. If I look at the sourcemap directly (the one generated in tmp), it has the info, but not the file mapping all the way back. Even if I load it manually in the devtools, all I get is the functions directory files showing up in the devtool, but loading my own sourcemaps, it behaves correctly. You can reproduce in a more isolated environment by pasting the sourcemap in this visualizer (note I had to remove the sourcemap url from the sourcemap for it to work). Then you'll see in the dropdown that only the intermediate files are available, not the original source (eg: typescript). I can't find in the esbuild documentation how to ensure that multiple chained sourcemap transformations apply correctly, so I can't suggest a solution. |
@Phoenixmatrix It looks like the With that change, I am able to get the breakpoint debugging working with the Chrome Dev Tools. Though, if you are using Remix, I wasn't able to get debugging working inside VS Code though. |
@BrandonNoad, are you on macOS? cloudflare/workers-sdk#4067 will likely need to be released before this works in VSCode (hopefully going out tomorrow) 👍 |
Yes, I'm on a Mac. I'll keep an eye out for the release. Thanks for fixing! |
|
Awesome. I verified that it works as expected with both patches applied. Thanks for fixing it! |
My notes for getting vscode working and operating with the vitest vscode test runner extension are here: |
saving people some time from reading thru a bunch of the above - the vscode + wrangler instructions you need are here. https://blog.cloudflare.com/debugging-cloudflare-workers#breakpoints |
I just noticed that I can get DevTools working (but not vscode debugging) in windows following instructions at: https://blog.cloudflare.com/debugging-cloudflare-workers#breakpoints ...but DevTools still won't work in WSL2 - anybody else have that problem and workaround? |
I'm not a Windows user, but I also had issues attaching my debugger. For me, downgrading wrangler fixed it - see reported issue here cloudflare/workers-sdk#5297 |
On one of my projects, I couldn't get either chrome devtools nor vscode to work, no matter what, where on the other, vscode breakpoints were working but devtools wouldn't until vscode's debugger session was started then closed, but even in that case the devtools just showed a bunch of random Honestly could just be some configuration reason with my first project, idk what though, and it's really annoying to keep having to use |
Hi,
Basically, I don't know how to debug a worker with VS Code. When clicking "Run/Debug" in VS Code, it basically invokes what is configured in
.vscode/launch.json
. I just have no idea what should be written there since the dev environment is wrapped by wrangler.Since most of the people are using VS Code and they probably have a similar problem, I assume it would be a nice addition and improving developer experience significantly.
I've also posted a related thread here: https://community.cloudflare.com/t/how-to-debug-use-breakpoints-with-vs-code/423775
Thanks for consideration.
The text was updated successfully, but these errors were encountered: