Skip to content
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

Cannot attach a debugger to Firebase Python Functions #6838

Open
johnnyoshika opened this issue Mar 6, 2024 · 10 comments
Open

Cannot attach a debugger to Firebase Python Functions #6838

johnnyoshika opened this issue Mar 6, 2024 · 10 comments

Comments

@johnnyoshika
Copy link

[REQUIRED] Environment info

firebase-tools: 12.9.1

Platform: Windows / macOS

[REQUIRED] Test case

Run emulator with --inspect-functions flag:

firebase emulators:start --project demo-project --inspect-functions

[REQUIRED] Steps to reproduce

Run emulator with --inspect-functions flag:

firebase emulators:start --project demo-project --inspect-functions

This message appears in the console:

>  Debugger attached.
!  --inspect-functions not supported for Python functions. Ignored.

[REQUIRED] Expected behavior

Debugger would attach to Python functions

[REQUIRED] Actual behavior

Receive a message in the Console that debugger is not supported

@aalej
Copy link
Contributor

aalej commented Mar 7, 2024

Hey @johnnyoshika, thanks for reaching out. Currently, --inspect-functions is only supported for Node.js runtimes. I’m going to label this as a feature request so we can better track this.

For those who also find this feature valuable, please leave a thumbs up on the original post. This helps our engineers prioritize future development efforts.

@pamafe1976
Copy link

As a workaround, I tried to debug my python functions starting the emulators and then executing the starting the function manually. However for this to work I need to provide credentials, and then the function targets the production services instead of the emulator.
Anyone knows a way of doing initialize_app forcing it to point to the firestore emulator?

@johnnyoshika
Copy link
Author

As a workaround, I tried to debug my python functions starting the emulators and then executing the starting the function manually. However for this to work I need to provide credentials, and then the function targets the production services instead of the emulator. Anyone knows a way of doing initialize_app forcing it to point to the firestore emulator?

Have you tried setting these env variables?

process.env.FIREBASE_AUTH_EMULATOR_HOST = '127.0.0.1:9099';
process.env.FIRESTORE_EMULATOR_HOST = '127.0.0.1:8080';

@pamafe1976
Copy link

pamafe1976 commented Apr 9, 2024

Thanks @johnnyoshika. That solution worked

As a workaround to debug a Firebase Cloud function in Python, with breakpoint and all, I added this code to my main.py


InitLocalDebug()

initialize_app()

callToTheFunctionYouWantToDebug()

where:

def InitLocalDebug() -> None:
    DIRNAME = os.path.dirname(__file__)
    BASEDIR = os.path.abspath(os.path.join(DIRNAME, '..'))
    GOOGLE_APPLICATION_CREDENTIALS = os.path.join(BASEDIR, 'credentials',
                                                  'firebase_adminsdk_credentials.json')
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = GOOGLE_APPLICATION_CREDENTIALS
    os.environ["FIREBASE_AUTH_EMULATOR_HOST"] = "127.0.0.1:9099"
    os.environ["FIRESTORE_EMULATOR_HOST"] = "127.0.0.1:8080"
   # this allows to use StringParam, IntParam, etc normally
    load_dotenv(os.path.join(DIRNAME, '.env.local'))

First I start the all the emulators:

firebase emulators:start --only auth,firestore

And then I run the main.py in debug mode directly from VSCode

@johnnyoshika
Copy link
Author

@pamafe1976 Thank you! I'll give that a try the next time I need to debug these functions.

@Chezlui
Copy link

Chezlui commented May 1, 2024

@pamafe1976 so the main.py is like a custom one that you have to test these functions, or it is the typical where you declare all the endpoints (@https_fn.on_request ...)?

@pamafe1976
Copy link

Its actually the same main.py

At the beginning I have a global variable

RUN_LOCAL = False

and the body of the main is something like this:

def InitLocalDebug() -> None:
    DIRNAME = os.path.dirname(__file__)
    BASEDIR = os.path.abspath(os.path.join(DIRNAME, ".."))
    GOOGLE_APPLICATION_CREDENTIALS = os.path.join(
        BASEDIR,
        "credentials",
        "credential_file.json",
    )
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = GOOGLE_APPLICATION_CREDENTIALS
    os.environ["FIREBASE_AUTH_EMULATOR_HOST"] = "192.168.1.93:9099"
    os.environ["FIRESTORE_EMULATOR_HOST"] = "192.168.1.93:8080"
    load_dotenv(os.path.join(DIRNAME, ".env.local"))

if RUN_LOCAL:
    InitLocalDebug()

initialize_app()

if RUN_LOCAL:
    requestLocations("")

So when I set RUN_LOCAL=True, I can debug locally the function that is called (in this case requestLocations)
And if I set RUN_LOCAL=False, I can either deploy or run with local emulator

@Chezlui
Copy link

Chezlui commented May 8, 2024

@aalej to be honest is pretty annoying.

I don't understand why there are so few resources related to Python environments compared to JS when both platforms are supposed to be supported in Firebase Functions.

Something fails silently and you spend hours adding traces everywhere when debugging you would find it in a matter of minutes.

Are we just 5 people using Python here?

@pamafe1976
Copy link

I guess it may because Python support started with 2nd gen

@Chezlui
Copy link

Chezlui commented May 8, 2024

@pamafe1976 I forgot, thank you very much for the tip.
Right now it doesn't work for me, because most of my problems are in the middle of sequences of requests and responses of different functions, so using just one doesn't help me a lot.

But it's really helpful though and I will use it for sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants