firebase / firebase-tools

The Firebase Command Line Tools
MIT License
4.03k stars 941 forks source link

Cannot attach a debugger to Firebase Python Functions #6838

Open johnnyoshika opened 8 months ago

johnnyoshika commented 8 months ago

[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 commented 8 months ago

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 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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

Chezlui commented 6 months ago

@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 commented 6 months ago

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 commented 6 months ago

@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 commented 6 months ago

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

Chezlui commented 6 months ago

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

fatihsever commented 5 months ago

In the AI ​​era, we use Python functions more. It is really challenging not being able to debug these Python functions that we use a lot while developing the AI/ML features of our applications. Could you please prioritize this issue? Thanks.

Chezlui commented 5 months ago

@aalej I feel any developer that uses Python with Firebase needs this, don't you? I mean, I understand using thumbs up can be a good way to prioritize, but maybe most of developers don't find this issue.

Just curiosity, how do you sort it out this if you don't have the chance to debug? Pro tips may help others.

vikx01 commented 5 months ago

At the very least your docs should reflect the fact that currently there is no way to attach a debugger to firebase functions written in python. This might be a good place: https://firebase.google.com/docs/emulator-suite/install_and_configure#startup

Chezlui commented 5 months ago

That's true, @vikx01 , I spent hours setting up the way to debug and when I got it ready I found out this amazing suprise:

image

fatihsever commented 5 months ago

I totally agree, @vikx01 Anyone who has come to this GitHub issue has probably experienced the following :)

After starting the emulator with --inspect-functions, you began debugging in VSCode as usual. You liked it because the debugging started. Then you realized that it never hits the breakpoint. You spent hours searching for the answer on StackOverflow, ChatGPT, Gemini, Llama, Mistral, and various other LLMs, but couldn't find the correct answer. Finally, after seeing the amazing surprise that @Chezlui mentioned, you gave up and started filling the code with print()s to find the error in the ancient way.

I think --inspect-functions only supported for Node.js message deserves more attention than the following warning that's printed hundreds of times in the emulator console:

zsh_—_functions