firebase-tools icon indicating copy to clipboard operation
firebase-tools copied to clipboard

Functions Python version conflict 3.12 and 3.11

Open curtis-jotson opened this issue 1 year ago • 8 comments

[REQUIRED] Environment info

firebase-tools: 13.3.0

Platform: macOS, Ubuntu (Github Actions)

[REQUIRED] Test case

Fresh Python functions project with both Python 3.11 and 3.12 installed

[REQUIRED] Steps to reproduce

  1. Start a new (or use existing) Firebase project and init Python functions
  2. Install Python 3.12
  3. Build the venv
  4. Install pip requirements (firebase-functions, firebase-admin)
  5. Build a function
  6. Attempt to run the emulators and it complains that you're not using Python 3.11
Failed to load function definition from source: FirebaseError: Failed to find location of Firebase Functions SDK. Did you forget to run '. "<redacted>/venv/bin/activate" && python3.11 -m pip install -r requirements.txt'?
  1. Install Python 3.11
  2. Build the venv using 3.11
  3. Install pip requirements using 3.11
  4. Emulators run
  5. Attempt to deploy the functions and it complains that you're not using Python 3.12
Error: Failed to find location of Firebase Functions SDK. Did you forget to run '. "<redacted>/venv/bin/activate" && python3.12 -m pip install -r requirements.txt'?

[REQUIRED] Expected behavior

Emulators should work with Python 3.12 and deploy should work with Python 3.11 when both are installed.

[REQUIRED] Actual behavior

Emulators fail to start the Python functions if the venv was setup using 3.12 Deploy fails when venv was setup using 3.11 if 3.12 is also installed

curtis-jotson avatar Feb 15 '24 21:02 curtis-jotson

I've got the same issue: since firebase-tools switched to version 13.3.0, my Github Actions builds fail to deploy Python Cloud functions (I am using python3.11, and with [email protected] it was working fine).

Run npx firebase-tools deploy --only functions --force --json --project *** --token ***
  
npm WARN exec The following package was not found and will be installed: [email protected]
***
  "status": "error",
  "error": "Failed to find location of Firebase Functions SDK. Did you forget to run '. \"/home/runner/work/webapp/webapp/functions-python/venv/bin/activate\" && python3.12 -m pip install -r requirements.txt'?"
*** 

PS Of course the fix is to simply to pre-install the working version, so this is just to report the bug on the new version.

antopolskiy avatar Feb 16 '24 08:02 antopolskiy

@antopolskiy So far our fix has been to pin firebase-tools to 13.2.1 for now.

curtis-jotson avatar Feb 16 '24 17:02 curtis-jotson

I got the same error and found a fix.

This error occurred when the runtime configuration was omitted from firebase.json. If your runtime is using python 3.11, it should look like this:

{
  "functions": [
    {
      "source": "functions",
      "codebase": "default",
      "runtime": "python311",  <--- here

    }
  ],
}

If the config is omitted, firebase-tools attempt to find latest python version. https://github.com/firebase/firebase-tools/blob/09c2641e861f2e31798dfb4aba1a180e8fd08ea5/src/deploy/functions/runtimes/python/index.ts#L33

But I think it makes more sense to use the default python binary than to find the latest version when the configuration is missing, which is already done by getPythonBinary. https://github.com/firebase/firebase-tools/blob/09c2641e861f2e31798dfb4aba1a180e8fd08ea5/src/deploy/functions/runtimes/python/index.ts#L50-L57

I think I'll make PR for it

kmsbernard avatar Feb 20 '24 02:02 kmsbernard

After further reading, it seems more reasonable to throw an error when the runtime field is missing, since the node.js function is already doing so.

kmsbernard avatar Feb 20 '24 04:02 kmsbernard

Hey folks, thanks for reporting this issue, and thanks @kmsbernard, for sharing your observations. I’m unable to reproduce the issue when using python3.12, however I was able to reproduce this using python3.11.

Let me know in case I made a mistake in following the provided steps. From what I can tell, the steps are:

  1. Run firebase init functions --project <project_id>
    • Choose Python
    • Do you want to install dependencies now? No
  2. Run cd functions
  3. Run python3.11 -m venv venv
  4. Run . venv/bin/activate && python3.11 -m pip install -r requirements.txt
  5. Uncomment the example function in main.py
  6. Run firebase emulators:start
    • Emulators raise an issue

It does seem like the issue here is that firebase-tools tries to find the latest version of python(currently 3.12). Setting the runtime as mentioned in https://github.com/firebase/firebase-tools/issues/6774#issuecomment-1953399977 fixes the issue.

Let me raise this issue to our engineering team so that we can determine the appropriate action for this. Also, feel free to make a PR for this if you’re up for it! Contributions are always welcome.

aalej avatar Feb 20 '24 10:02 aalej

@kmsbernard That's awesome, thanks! We're going to set this and see how it behaves.

@aalej Thanks for looking into this, my steps to reproduce might have been a bit off as we're mainly getting this issue on a Github Actions runner that has at least 6 different Python versions vs a local environment that we have more control over. Some is local in the case of the emulators, some is the runner in the case of the error using Python 3.12 instead of 3.11.

It all stems from not having the runtime option configured in firebase.json. I think maybe another question is "Should firebase init functions be initializing the runtime option?" In my "ignorant Firebase noob" world, the answer to that question is "Yes, it should initialize to the latest version the platform supports." As it stands right now, I didn't even realize it wasn't set and firebase-tools was trying to reason about it. If it were set by default (or even erroring if not specified), the necessity of reasoning about it is basically eliminated and it becomes the responsibility of the user to make sure it's set correctly.

curtis-jotson avatar Feb 20 '24 23:02 curtis-jotson

Hello. I can reproduce this issue with Python 3.11 as well. Adding a runtime does fix it, however my LSP does warn me of a unknown value. I'm guessing the schema for firebase.json is not updated.

LSP warning in JSON:

Value is not accepted. Valid values: "nodejs10", "nodejs12", "nodejs14", "nodejs16", "nodejs18", "nodejs20".

exaby73 avatar Mar 08 '24 03:03 exaby73