Firebase Functions Shell Debugging
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
--debugflag if I can get that to work, and possibly PR if firebase is happy
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.
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.
Has there been any changes to the debugging process for firebase functions?
Not yet unfortunately. We've been scrambling on a number of other things.
@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.
Any success?, trying to debug firebase functions in vs code here too.
@seikilos @nschurmann nothing here, no 😔
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.
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 how soon? 😄
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!
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 😉
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 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.
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.
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 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.
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.
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).
Actually, here's a much simpler way:
-
Run
firebase serve --port 8010with whatever other parameters you use -
Run
npx functions inspect YOUR_FUNCTION_NAMEto enable debugging -
With debugging enabled, your code edits won't be picked up. To fix this, run
npx functions reset YOUR_FUNCTION_NAME --keepwhenever your code changes, to keep your debugging settings but reload the new code.