firebase-tools icon indicating copy to clipboard operation
firebase-tools copied to clipboard

Firebase Functions Shell Debugging

Open ahaverty opened this issue 9 years ago • 27 comments

Version info

"firebase-tools": "3.13.1"
"firebase-functions": "0.7.1"

I'm looking to locally debug my emulated functions (in vscode for now) using the new experimental:functions:shell. In https://github.com/firebase/firebase-functions/issues/4 @laurenzlong mentions it should be capable with firebase shell, if it's possible in google cloud functions emulator. (I'm going with a github issue over an SO post or support ticket, as this may lead to a nice PR if the firebase team aren't already working on this feature.)

I've yet to get anything to attach, and i'm not too familiar with attaching to debug processes. Here's what I've tried, working backwards from the google cloud functions instructions:

Launch command for firebase emulator: firebase experimental:functions:shell --port 5000 //forcing port 5000 to simplify the launch task, we could possibly look into connecting to the process directly if this is smelly

My launch.json in vscode:

{
    "type": "node",
    "request": "attach",
    "name": "Debug Firebase Function Emulator",
    "port": 5000,
    "protocol": "legacy" //As we're using node 6, this seems essential
}

In the firebase tools emulator script, it looks like we're only calling start() for each function. Whereas, according to the debugging google cloud functions guide, we need to be calling debug() in order to allow attaching the debugger.

I'm assuming the firebase functions emulator will have some options in the future, something like --debug. If anyone has any tips/pointers that might help me investigate, please let me know 👌

I'm going to attempt:

  • [x] Modifying the script to attach to my node debugger.

  • [ ] Adding --debug flag if I can get that to work, and possibly PR if firebase is happy

ahaverty avatar Oct 23 '17 13:10 ahaverty

So, my original thoughts were wrong.. I can see firebase-tools calling .deploy(name, opts) and I'm guessing it's around there that the .debug() should be called. But I'm unable to pass the type parameter, or find any documentation on the debug api for cloud functions. It seems much more complex than I initially though, interested to hear if anyone has any ideas. Happy to close issue if inappropriate.

ahaverty avatar Oct 23 '17 14:10 ahaverty

Node debugging is a bit tricky. The Cloud Functions emulator creates child processes to properly emulate the isolation of production. We have a handful of bugs to tackle to configure debuggers to follow the forked processes. Eventually this will work without any hackery on your part.

inlined avatar Oct 23 '17 21:10 inlined

Has there been any changes to the debugging process for firebase functions?

Eric-Larson avatar Nov 27 '17 02:11 Eric-Larson

Not yet unfortunately. We've been scrambling on a number of other things.

inlined avatar Nov 27 '17 17:11 inlined

@ahaverty were you successful debugging the shell? I have a fairly complex function that runs fine when everything is mocked but fails hard when executing in emulator. Alas, I hoped I would never have to println debug again in my life.

Seikilos avatar Jan 26 '18 17:01 Seikilos

Any success?, trying to debug firebase functions in vs code here too.

nschurmann avatar Apr 01 '18 21:04 nschurmann

@seikilos @nschurmann nothing here, no 😔

ahaverty avatar Apr 02 '18 22:04 ahaverty

Is there any way at all to debug firebase functions with a debugger? Any option I've tried hasn't worked. Specifically trying to debug onWrite functions.

jfhidakatsu avatar Apr 03 '18 01:04 jfhidakatsu

Hey everyone, I know it's probably been a bit disappointing to have us be so silent on this thread.

Unfortunately IDE debugger integration isn't staffed right now, so we'll need some community help if you want firebase serve or firebase functions:shell to support breakpoints. That being said, we have been focused on unit testability of your cloud functions. Keep an eye out for the next major release, coming very soon.

inlined avatar Apr 03 '18 15:04 inlined

@inlined how soon? 😄

nschurmann avatar Apr 03 '18 20:04 nschurmann

I believe he was referring to our slate of new releases today including the brand-new firebase-functions-test library that should help you test your functions!

mbleigh avatar Apr 03 '18 20:04 mbleigh

VScode just released node debug process attaching. https://code.visualstudio.com/updates/v1_22#_node-debugging

I'll give it a shot next week or if any of the firebase team have some undocumented/unofficial guides 😉

ahaverty avatar Apr 08 '18 12:04 ahaverty

Just tried out "live" RTDB unit testing on functions v1.0.1 + mocha (followed the guides here), https://firebase.google.com/docs/functions/unit-testing And can confirm breakpoints/debugging works in vscode for functions without any extra steps needed! 😍

ahaverty avatar Apr 08 '18 15:04 ahaverty

@ahaverty care to share? I'm trying to do the following:

npm run build && firebase serve --only functions from VS Code and it's not attaching in debug mode.

dfischer avatar Apr 11 '18 01:04 dfischer

If I understand @ahaverty 's comment correctly, they're referring to mocha tests, which are much simpler to debug. Mocha runs everything in a single process, whereas the Cloud Functions emulator actually emulates the process isolation you get in GCF. The firebase-tools code will need to change to add the right flags that cause debuggers to follow child processes.

inlined avatar Apr 11 '18 01:04 inlined

I know this has been up for some time, is there any progress with VSCode or firebase-tools that would finally allow to debug functions when run locally?

alexsorokoletov avatar Dec 03 '18 05:12 alexsorokoletov

@alexsorokoletov I use the Cloud Functions Emulator for this and it works fine for me with Firebase functions, it gives you a command-line tool that you can attach to a function in VSCode.

dvdmmc avatar Dec 20 '18 15:12 dvdmmc

It looks like firebase-functions-test and the ability to attach to the Cloud Functions Emulator have solved the particular issue raised in this thread. Closing this out.

thechenky avatar Jan 09 '19 00:01 thechenky

If anyone's interested, I figured out a small hack to make this work:

https://github.com/firebase/firebase-tools/compare/master...jsherwani:support-debug

FYI, you can add the above changes (just one function call) manually into node_modules/firebase-tools/lib/functionsEmulator.js if you want a quick fix (that's what I've done).

jsherwani avatar Jan 17 '19 04:01 jsherwani

Actually, here's a much simpler way:

  1. Run firebase serve --port 8010 with whatever other parameters you use

  2. Run npx functions inspect YOUR_FUNCTION_NAME to enable debugging

  3. With debugging enabled, your code edits won't be picked up. To fix this, run npx functions reset YOUR_FUNCTION_NAME --keep whenever your code changes, to keep your debugging settings but reload the new code.

jsherwani avatar Jan 17 '19 05:01 jsherwani