Firebase-tools: Firebase Functions Shell Debugging

Created on 23 Oct 2017  路  27Comments  路  Source: firebase/firebase-tools

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

Most helpful comment

@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.

All 27 comments

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:

  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 It does not work for me:

$ npx functions inspect my-fn
npx: installed 1 in 0.619s
command not found: functions

@dvdmmc Can you share the details about how you made that work?

@bageren For the most part, the online documentation should help you get set up. Let me know if you have any particular error messages and I can help you.
It gets a bit trickier if your functions uses Authorization, however. For that, I did the following:

  • Setup a simple local site using https://github.com/firebase/firebaseui-web The site just needs to be a standard sign-in using the Google Auth provider (firebase.auth.GoogleAuthProvider.PROVIDER_ID)
  • Create a success page to get the signed-in user's token firebase.auth().currentUser.getIdToken
  • use this token to make Authorized requests to the functions you're serving locally via the emulator, for example:
    curl -X POST -H "Content-Type:application/json" -H "Authorization: Bearer $FIREBASE_TOKEN" http://localhost:8010/your-project/europe-west1/isUserValid -d '{"data":{ }}'

I haven't looked at this in a while so perhaps there is a better way of making Authorized requests now, but it's still working for me so I haven't looked at it too closely.

To debug, make sure you're deploying the emulator functions using something like: functions deploy isUserValid -H -t 999s and calling functions inspect isUserValid before attaching in your editor (VSCode, etc.)

Hope this helps.

ndb firebase serve allows me to debug my firebase functions (hit debugger breakpoints and see stack traces) as with the previous functions inspect FUNCTION-NAME which is replaced by the functions-framework (as I understand it anyhow). Per https://cloud.google.com/functions/docs/functions-framework I was able to debug functions in isolation using GCLOUD_PROJECT=FIREBASE-PROJECT node --inspect-brk /path/to/functions-framework --target FUNCTION-NAME --port=5000

I think it's time to reopen this as https://github.com/googlearchive/cloud-functions-emulator has been deprecated.

I tried doing what @jimmont said but I got

Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing. Initializing firebase-admin will fail

So setting GCLOUD_PROJECT wouldn't be enough - and also, I can't find information on what these variables should be.

@marcospgp we are working on breakpoint debugging in the newer emulator (see #1798) and others.

@marcospgp I was able to set the GCLOUD_PROJECT variable by itself (as @jimmont wrote above) and everything worked as expected. There was a message indicating that FIREBASE_CONFIG was missing but would be inferred by the value of GCLOUD_PROJECT. As long as you are using a standard Firebase setup (standard database url) everything should work.

Was this page helpful?
0 / 5 - 0 ratings