GoogleCloudPlatform / functions-framework-nodejs

FaaS (Function as a service) framework for writing portable Node.js functions
Apache License 2.0
1.29k stars 158 forks source link

Local debug #15

Closed bluelightning2k closed 5 years ago

bluelightning2k commented 5 years ago

Hi, the readme doesn't mention anything about local debugging.

Is breakpoint debugging supported?

This would be a necessary feature for us to begin using Functions Framework. Is this supported, and can you consider updating the readme to address offline debugging (even if it's just to state that it's not supported, or to suggest a workaround).

Thanks!

stew-r commented 5 years ago

Hi bluelightning2k -- try the following:

  1. install the module (npm install @google-cloud/functions-framework) to node_modules
  2. run your function using node --inspect, e.g., node --inspect node_modules/@google-cloud/functions-framework <FLAGS>

Where <FLAGS> correspond to Functions Framework flags (e.g., target) as documented on the README.

That should produce something like:

node --inspect node_modules/@google-cloud/functions-framework --target helloWorld
Debugger listening on ws://127.0.0.1:9229/c3574d99-20f7-42e8-b34c-dd9ca27f4876
For help see https://nodejs.org/en/docs/inspector
Serving function...
Function: helloWorld
URL: http://localhost:8080/
stew-r commented 5 years ago

I also tried this out using npx: npx --node-arg=--inspect @google-cloud/functions-framework --target=helloWorld

As a non-Node.js developer, the npx tooling is pretty slick. You should also be able to pass arguments to Node using NODE_OPTIONS.

stew-r commented 5 years ago

Closing this. Please re-open if this doesn't work. I'd also love to get confirmation that this works for your purposes.

bluelightning2k commented 5 years ago

Hi @stew-r , This worked perfectly!

Specifically: your first method worked perfectly, even in a complex setup. We are using WebPack to do a reasonably complicated build, and we setup VSCode to do the build, run/restart the function, and attach the debugger - all by pressing F5.

The NPX method didn't seem to support reloading in this configuration. So if you add one to the documentation (highly recommended) I'd choose the first method.

Thank you!

stew-r commented 5 years ago

Specifically: your first method worked perfectly, even in a complex setup. We are using WebPack to do a reasonably complicated build, and we setup VSCode to do the build, run/restart the function, and attach the debugger - all by pressing F5.

That's awesome! I'm glad you were able to make this work so seamlessly. Do you think there would be any value in maybe writing a small blog/Medium post about your setup? I'm sure other developers could benefit from your learnings.

darshan-willow commented 5 years ago

In the latest version of functions-framework (1.1.0) node --inspect node_modules/@google-cloud/functions-framework --function-target helloWorld this command does not work. Instead please use node --inspect node_modules/@google-cloud/functions-framework --target helloWorld

@stew-r Can you please write a blog post on how to make basic debug work because it is very hard to find documentation on functions-framework

stew-r commented 5 years ago

Good catch. I edited my comments to change function-target to target.

I probably won't have time to write a blog post about debugging. But I'll see if we can add something to the Functions Framework readme or directly on the Cloud Functions docs.

quantuminformation commented 5 years ago

Would be nice to have a doc for webstorm setup for local debugging.

quantuminformation commented 5 years ago

Its ok, I managed to get it to work

Screenshot 2019-05-27 at 09 57 06

I'll make a youtube video

quantuminformation commented 5 years ago

@stew-r I created a Discord server for GCP and have a channel for functions if you're interested. Good for stuff you don't want to add to the noise of github issues.

https://discord.gg/9yGuxRV

stew-r commented 5 years ago

We already have more channels than I can manage well. For chat, we use the Google Cloud community on Slack.

quantuminformation commented 5 years ago

ah didnt know there was one, cool

stew-r commented 5 years ago

Added instructions: https://github.com/GoogleCloudPlatform/functions-framework-nodejs/tree/master/docs

wujekbogdan commented 5 years ago

@stew-r I think this issue should be re-opened. @google-cloud/functions-framework requires node to be ">=10.0.0" while Firebase Requires node v8, so we can't have both firebase and functions-framework installed at the same time.

I'm aware of the --ignore-engines, but still it's not a production-ready solution.

stew-r commented 5 years ago

I think Firebase has its own local emulation solution, no? You shouldn't need to use/rely on this as a Firebase customer.

joantune commented 4 years ago

@stew-r:

You shouldn't need to use/rely on this as a Firebase customer.

you shouldn't but fact of the matter is that AFAIK the firebase CLI has no way for you to debug the functions..

regarding this or firebase's method (in case any exists) is there a way to automatically reload the code in case of changes?

Many TIA!

grant commented 4 years ago

@joantune If there's an issue with Firebase Functions or the firebase CLI, that should be reported upstream with https://github.com/firebase/firebase-tools.

If there's some other issue with this framework, please open a new issue and we'll look at it!

joantune commented 4 years ago

Ok - managed to do something like that with Nodemon - and setting on PyCharm (or whichever debugger/IDE) the setting to reconnect automatically

RN I'm using something like:

FIREBASE_CONFIG="<escaped JSON of FIREBASE config>" GCLOUD_PROJECT="project id" ./node_modules/.bin/nodemon -e ts --exec "tsc && node --inspect node_modules/@google-cloud/functions-framework --target app "

it feels like it should be easier

Youlean commented 2 years ago

I was really struggling to start the debugger in VS Code on Windows, but this did the trick:

npx --node-options=--inspect @google-cloud/functions-framework --target=helloWorld

Unfortunately, the explanation here just spits some errors for me: https://github.com/GoogleCloudPlatform/functions-framework-nodejs/blob/master/docs/debugging.md

grant commented 2 years ago

@Youlean Using npx is probably a better solution. What was the windows issue you were facing? Bad paths?

We can update the docs to use npx. https://github.com/GoogleCloudPlatform/functions-framework-nodejs/pull/429

Youlean commented 2 years ago

Thanks for the reply!

Here are the errors I am getting when using:

node --inspect node_modules/.bin/functions-framework --target=helloWorld

Errors:

basedir=$(dirname "$(echo "$0" | sed -e 's,\,/,g')") ^^^^^^^

SyntaxError: missing ) after argument list at Object.compileFunction (node:vm:352:18) at wrapSafe (node:internal/modules/cjs/loader:1032:15) at Module._compile (node:internal/modules/cjs/loader:1067:27) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) at node:internal/main/run_main_module:17:47

I am a C++ dev, so it might be that I am doing something stupid. :)

This command however works as expected npx --node-options=--inspect @google-cloud/functions-framework --target=helloWorld

Youlean commented 2 years ago

BTW, I would really appreciate a Cloud Functions Hello World sample project for VS Code that has launch.json set up. I was looking around but couldn't find any sample code.

grant commented 2 years ago

Thanks for the reply!

Here are the errors I am getting when using:

node --inspect node_modules/.bin/functions-framework --target=helloWorld

Errors:

basedir=$(dirname "$(echo "$0" | sed -e 's,\,/,g')") ^^^^^^^

SyntaxError: missing ) after argument list at Object.compileFunction (node:vm:352:18) at wrapSafe (node:internal/modules/cjs/loader:1032:15) at Module._compile (node:internal/modules/cjs/loader:1067:27) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) at node:internal/main/run_main_module:17:47

I am a C++ dev, so it might be that I am doing something stupid. :)

This command however works as expected npx --node-options=--inspect @google-cloud/functions-framework --target=helloWorld

Interesting. This must be something not going correctly with the node command. Well, npx is better in this case.

grant commented 2 years ago

BTW, I would really appreciate a Cloud Functions Hello World sample project for VS Code that has launch.json set up. I was looking around but couldn't find any sample code.

I'm not sure we have any samples of a good launch.json. I'd assume a standard Node launch.json should be fine. Any specific features or things you want from a launch script?

I created this video about setting up debugging in case that's useful:

https://www.youtube.com/watch?v=X7-OcaGv5Yg

Youlean commented 2 years ago

I'm not sure we have any samples of a good launch.json. I'd assume a standard Node launch.json should be fine. Any specific features or things you want from a launch script?

I created this video about setting up debugging in case that's useful:

https://www.youtube.com/watch?v=X7-OcaGv5Yg

I have watched this video. You are awesome! It should be linked in git explanation. It has helped me a lot. For the launch.json it would be great if you can do F5 to run a debugger and then VS Code to call npm start automatically. Also, when the debugger is detached to stop the node process.

Other than that, adding support for nodemon would be great.

grant commented 2 years ago

For the launch.json it would be great if you can do F5 to run a debugger and then VS Code to call npm start automatically.

Sounds good. We should add that to the docs.

For nodemon, I have (yet another) resource that I think works for this use-case: https://medium.com/google-cloud/hot-reload-node-cloud-functions-64ffdb095a00, maybe document that here?

Youlean commented 2 years ago

For nodemon, I have (yet another) resource that I think works for this use-case: https://medium.com/google-cloud/hot-reload-node-cloud-functions-64ffdb095a00, maybe document that here?

Awesome. Working like a charm! ;) I would add that too to the docs. Thank you so much!

grant commented 2 years ago

Have a good weekend ☺️

minaelee commented 1 year ago

In case this helps anyone, noting here that using npx --node-options=--inspect @google-cloud/functions-framework --target=helloWorld instead of the still-in-the-docs-but-not-working command node --inspect node_modules/.bin/functions-framework --target=helloWorld will not automatically attach the debugger in your IDE.

In VS Code, to manually attach the debugger, first run the npx command, then open the Command Palette and enter the command Debug: Attach to Node process, then from the dropdown list that shows up, select the appropriate node process.

joansafr commented 11 months ago

In case this helps anyone, noting here that using npx --node-options=--inspect @google-cloud/functions-framework --target=helloWorld instead of the still-in-the-docs-but-not-working command node --inspect node_modules/.bin/functions-framework --target=helloWorld will not automatically attach the debugger in your IDE.

In VS Code, to manually attach the debugger, first run the npx command, then open the Command Palette and enter the command Debug: Attach to Node process, then from the dropdown list that shows up, select the appropriate node process.

it worked for me! thank you

BoscoDomingo commented 1 month ago

Local debugging is iffy right now.

As @minaelee mentioned, VS Code's debugger doesn't attach automatically, and the commands in the README here as well as in your own guide here are simply wrong, spitting out the error @Youlean mentioned in his comment above.

Is there any chance this will be looked at?