Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functions debugging in VS Code

From VS Code integrated terminal I run firebase serve --only functions,hosting then in the debug tab I created the default launch.json:

{   "version": "0.2.0",   "configurations": [     {       "type": "node",       "request": "launch",       "name": "Launch Program",       "program": "${file}"     }   ] } 

I want to debug the server side (functions/index.js) not the client side.

I've tried some configuration from https://code.visualstudio.com/docs/nodejs/nodejs-debugging with no luck.

How to debug Firebase functions in VS Code?

like image 691
Alin C Avatar asked Aug 28 '17 13:08

Alin C


People also ask

How do I step into a function in Visual Studio?

Begin code stepping by selecting F10 or F11. Doing so allows you to quickly find the entry point of your app. You can then continue to press step commands to navigate through the code. Run to a specific location or function, for example, by setting a breakpoint and starting your app.

How do you Debug using breakpoints in Visual Studio Code?

Set breakpoints in source code To set a breakpoint in source code, click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint. The breakpoint appears as a red dot in the left margin.

How do I enable debugging in Visual Studio?

In the Visual Studio toolbar, make sure the configuration is set to Debug. To start debugging, select the profile name in the toolbar, such as <project profile name>, IIS Express, or <IIS profile name> in the toolbar, select Start Debugging from the Debug menu, or press F5.

What is F12 in VS Code?

If a language supports it, you can go to the definition of a symbol by pressing F12. If you press Ctrl and hover over a symbol, a preview of the declaration will appear: Tip: You can jump to the definition with Ctrl+Click or open the definition to the side with Ctrl+Alt+Click.


2 Answers

Update: May 2021

It's now possible to debug (put breakpoints) firebase functions running locally on VSCode.

  1. update firebase-tools to atleast v7.11.0 npm i -g firebase-tools
  2. add below to launch.json:
    {       "version": "0.2.0",       "configurations": [         {           "type": "node",           "request": "attach",           "name": "Attach",           "port": 9229,           "restart": true,           "skipFiles": ["<node_internals>/**"]         }       ]     } 
  1. run the emulator: firebase emulators:start --inspect-functions
  2. run the vscode debugger with attach option.
  3. put breakpoint in a cloud function.
  4. call that function (something like http://localhost:5001/your_project/us-central1/helloWorld). Breakpoint should hit.

note: Support for PubSub/scheduled functions is still not implemented. Upvote this issue: https://github.com/firebase/firebase-tools/issues/2034

note: if you're testing functions from local firebase hosting setup, then you need to point your hosting functions to local server instead of cloud server. see here https://stackoverflow.com/a/59381328/3073272


Old Answer on debugging(no breakpoint) firebase functions using terminal:

There's a firebase documentation for Testing functions interactively using shell. You can also test https callable functions. Though steps for attaching a debugger is not mentioned there.

  1. Open the Service Accounts pane of the Google Cloud Console.

  2. Make sure that App Engine default service account is selected, and use the options menu at right to select Create key.

  3. When prompted, select JSON for the key type, and click Create.

  4. Set your Google default credentials to point to the downloaded key

    $ set GOOGLE_APPLICATION_CREDENTIALS=path\to\key.json

    $ firebase functions:shell

The Cloud Functions shell emulates all types of function triggers with an interactive shell for invoking the functions with test data. Options vary by function type, but the basic usage format is:

myFunctionName(data, options) 
like image 96
GorvGoyl Avatar answered Oct 03 '22 01:10

GorvGoyl


You can't debug Firebase functions without defining Firebase configuration variables first. Firebase CLI does it for you.

To debug you can try the same trick as you would do for Firebase functions unit test.

Add following lines to the index.js file before you call admin.initializeApp(functions.config().firebase):

admin.initializeApp = function () {} functions.config = function() {     return {         firebase: {           databaseURL: 'https://not-a-project.firebaseio.com',           storageBucket: 'not-a-project.appspot.com',         }     }; } 

You can debug Firebase functions now in a same way as any other google cloud function:

  1. Install the Cloud Functions Emulator:

    npm install -g @google-cloud/functions-emulator 
  2. Start the Emulator:

    functions start 
  3. Deploy your function:

    functions deploy helloWorldFunction --trigger-http 

    You'll get output like this:

    Waiting for operation to finish...done. Deploying function........done. Function helloWorldFunction deployed.  Property | Value ---------|------------------------------------------------------------------------ Name     | helloWorldFunction Trigger  | HTTP Resource | http://localhost:8010/helloWorldProject/us-central1/helloWorldFunction 
  4. To debug using the standard Node.js Debugger type:

    functions debug helloWorldFunction 

    You'll get:

    Debugger for helloWorldFunction listening on port 5858. 
  5. Now add following lines to your launch.json VS Code

    {     "version": "0.2.0",     "configurations": [         {             "name": "Node.JS (local)",             "type": "node",             "request": "attach",             "port": 5858         }     ] } 
  6. Start debugging in your VS Code and trigger your function by calling URL you've got on step #3.

    You can also trigger the function by typing functions call helloWorldFunction in the terminal.

For more details refer to the instructions here Cloud Functions Local Emulator.

like image 22
Andrew Veresov Avatar answered Oct 03 '22 02:10

Andrew Veresov