cloudworker icon indicating copy to clipboard operation
cloudworker copied to clipboard

Question: Is it possible to run cloudworker in debug?

Open markusahlstrand opened this issue 5 years ago • 5 comments

I'm looking for an easy way to debug cloudflare workers wonder if this is possible with the cloudworker? I'm currently using more or less the runtime.js library from cloudworker to patch the node environment before starting the worker and it works pretty good, but it doesn't seem like the runtime is exposed? Is this possible, could it be possible or would it be better to do this in a separate project?

Thanks!

markusahlstrand avatar Jul 18 '19 07:07 markusahlstrand

Hi @markusahlstrand,

I don't understand the question. Could you give specific examples of what you are trying to accomplish and why it currently doesn't work? Thanks!

hankjacobs avatar Aug 03 '19 16:08 hankjacobs

Hi @hankjacobs,

What I'm trying to accomplish is debug multiple files in visual studio code, rather than compiling to a single text file. I wonder if there is a way to achieve this with the cloudworker lib or if it's a different use case? The examples I've seen with for instance unit tests loads the worker as a text file which I assume will make debugging in vs-code difficult? Thanks!

markusahlstrand avatar Aug 04 '19 10:08 markusahlstrand

If you enable source map for your webpacked result, you can debug the original multiple source files just fine.

As Cloudworker is not a lib, but an executor of your single-file worker code, you cannot just set a breakpoint (like you cannot set a breakpoint inside a string, which is eval()ed at runtime), then 'Debug > Start Debugging' and expect the debugger to stop (I will be happy to be corrected if there's an easy way).

But you can put a debugger statement in your code and then 'Start Debugging' to initiate a debugging session.

atmin avatar Aug 05 '19 10:08 atmin

This might be a stupid question, but is there an advantage to loading the script in a node vm sandbox? Would there be a downside to just adding shims for the cloudflare runtime and executing the script straight away? That way breakpoints would work just as in any other node program with no webpack required. Or is it more a question about that cloudworker isn't a lib, but an executor?

markusahlstrand avatar Aug 05 '19 15:08 markusahlstrand

The advantage is dev and prod environments are as similar, as possible. The disadvantage is harder to debug.

Before Cloudworker existed, I actually used the approach you're describing (doing feature detection and requireing/patching stuff when runtime is Node. Webpack the thing in the publish script (dead code elimination takes care of Node polyfills) and upload to Cloudflare). Yes, debugging is straightforward, but the code is uglier and environments differ in subtle ways, which can lead to hard to debug errors when the application is more complex. Also, harder to replicate errors in prod.

I guess it can be very feasible approach for simple apps, though.

atmin avatar Aug 07 '19 14:08 atmin