Skip to content

Firebase Functions Shell Debugging #517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 of 2 tasks
ahaverty opened this issue Oct 23, 2017 · 27 comments
Closed
1 of 2 tasks

Firebase Functions Shell Debugging #517

ahaverty opened this issue Oct 23, 2017 · 27 comments

Comments

@ahaverty
Copy link

ahaverty commented Oct 23, 2017

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 firebase/firebase-functions#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:

  • 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
Copy link
Author

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.

@inlined
Copy link
Member

inlined commented Oct 23, 2017

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.

@Eric-Larson
Copy link

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

@inlined
Copy link
Member

inlined commented Nov 27, 2017

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

@Seikilos
Copy link

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

@nschurmann
Copy link

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

@ahaverty
Copy link
Author

ahaverty commented Apr 2, 2018

@Seikilos @nschurmann nothing here, no 😔

@jfhidakatsu
Copy link

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.

@inlined
Copy link
Member

inlined commented Apr 3, 2018

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.

@nschurmann
Copy link

@inlined how soon? 😄

@mbleigh
Copy link
Contributor

mbleigh commented Apr 3, 2018

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!

@ahaverty
Copy link
Author

ahaverty commented Apr 8, 2018

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
Copy link
Author

ahaverty commented Apr 8, 2018

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! 😍

@dfischer
Copy link

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

@inlined
Copy link
Member

inlined commented Apr 11, 2018

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.

@alexsorokoletov
Copy link

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?

@dvdmmc
Copy link

dvdmmc commented Dec 20, 2018

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

@thechenky
Copy link
Contributor

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.

@jsherwani
Copy link

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

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
Copy link

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.

@janoist1
Copy link

@jsherwani It does not work for me:

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

@bageren
Copy link

bageren commented Jun 14, 2019

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

@dvdmmc
Copy link

dvdmmc commented Jun 17, 2019

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

@jimmont
Copy link

jimmont commented Jun 22, 2019

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

@marcospgp
Copy link

marcospgp commented Nov 23, 2019

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.

@samtstern
Copy link
Contributor

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

@dvdmmc
Copy link

dvdmmc commented Dec 3, 2019

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

joehan added a commit that referenced this issue Mar 12, 2020
* starts on extension emualtor commands

* further work on ext emulator

* Progress on emulator

* more logs

* Successfully emualted firestore counter! with some crednetial problems

* cleans up consoel.logs

* sets env during laod triggers

* format

* removes uneeded file changes

* removes uneeded file changes

* add ext:dev:emulators:exec

* formats

* DRYing up exec

* reorganizing code and making initial fixes suggested by Sam

* more fixes

* added ext:emualtor flags

* adds directory climbing to find an extension.yaml file

* adds unit tests for triggerHelper

* adds unit tests for paramHelper

* typo

* adds unit tests for paramHelper

* adds unit tests for paramHelper

* pr fixes

* typo and unused import

* pr fixes

* add sourceDirectory detection

* formats

* pr fixes

* moving some flags around;

* default sourcedirectory to functions

* adds param validation

* style fix

* adds tests for param validation

* adds regex validation and tests

* emulator-> emulators

* pr fixes;

* adds log for missing trigger

* formatted

* removes unneeded import of _

* slightly better error message

* Add "firebase ext:dev:init" command (#506)

* formatting;

* fixing package-lock

* pass predefined triggers to FunctionEmulatorRuntime so they are always available at runtime

* remove commented out code;

* removes newline

* format/

* predefinedTriggers->extensiontriggers

* use project provided by --project flag

* remove source directory line (#510)

* Update WELCOME.md for ext:dev:init (#509)

* adds validation for multiselect and select params

* typo fix

* also reject empty string for reqd params

* moves validation utils to extensionHelper, adds validateSpec, adds unit tests

* copy fixes, and adds an extra check for param type

* pr fixes and extra tests

* remove hardcoded variable;

* remove hardcoded variable;

* remove hardcoded variables

* bug fix and remove console log

* add runtime field to ext spec (#514)

* add runtime field to ext spec

* use getProjectId instead of options.project

* typo fix in test description

* differentiate javascript/typescript welcome messages (#513)

* differentiate javascript/typescript welcome messages

* adds support for --test-config

* Update src/extensions/emulator/optionsHelper.ts

Co-Authored-By: rachelsaunders <[email protected]>

* Update src/extensions/emulator/optionsHelper.ts

Co-Authored-By: rachelsaunders <[email protected]>

* Update src/extensions/emulator/optionsHelper.ts

Co-Authored-By: rachelsaunders <[email protected]>

* Update src/extensions/emulator/optionsHelper.ts

Co-Authored-By: rachelsaunders <[email protected]>

* use JSON.parse instead;

* finalizing copy

* Update src/extensions/emulator/optionsHelper.ts

Co-Authored-By: rachelsaunders <[email protected]>

* Update src/extensions/emulator/optionsHelper.ts

Co-Authored-By: rachelsaunders <[email protected]>

* Update src/extensions/emulator/optionsHelper.ts

Co-Authored-By: rachelsaunders <[email protected]>

* Validate all params instead of stopping on a failed param (#518)

* check all params instead of stopping on a failed param

* copy fix

* Update src/test/extensions/askUserForParam.spec.ts

Co-Authored-By: rachelsaunders <[email protected]>

* Update src/extensions/askUserForParam.ts

Co-Authored-By: rachelsaunders <[email protected]>

* Update src/test/extensions/askUserForParam.spec.ts

Co-Authored-By: rachelsaunders <[email protected]>

Co-authored-by: rachelsaunders <[email protected]>

* adds validation that default value passes validationRegex (#517)

* Add extdev preview and hide ext:dev commands behind it (#519)

* add extdev preview and hide ext:dev commands behind it

* formats;

* add ext to previews

* formats

* formats

* formats

* hide local  functionality from non preview users

* more accurate error messages

* formats

* missing space

Co-authored-by: Lauren Long <[email protected]>
Co-authored-by: Tina Liang <[email protected]>
Co-authored-by: rachelsaunders <[email protected]>
yuchenshi pushed a commit that referenced this issue Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests