firebase / firebase-tools

The Firebase Command Line Tools
MIT License
4.01k stars 929 forks source link

Cloud Function deploy fails for Firestore-triggered function #6364

Closed dominoFire closed 11 months ago

dominoFire commented 1 year ago

[REQUIRED] Environment info

firebase-tools: 12.4.7

Platform: Windows

[REQUIRED] Test case

When I try to deploy a triggered Python, 2nd-gen cloud function, it fails with a database not found error.

[REQUIRED] Steps to reproduce

  1. Create a function in your main.py:
@firestore_fn.on_document_created(document='a/{aId}/b/{bId}')
def do_magic(event: Event[DocumentSnapshot]) -> None:
    """
    Do magic
    """

    return None
  1. Run firebase deploy on the function.

[REQUIRED] Expected behavior

Deploys succeeds.

[REQUIRED] Actual behavior

Deploy fails with the following message:

firebase-debug.log


=== Deploying to 'propio-app'...

i  deploying functions
i  functions: preparing codebase default for deployment
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
i  artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
+  functions: required API cloudfunctions.googleapis.com is enabled
+  functions: required API cloudbuild.googleapis.com is enabled
+  artifactregistry: required API artifactregistry.googleapis.com is enabled
i  functions: Loading and analyzing source code for codebase default to determine what to deploy
 * Serving Flask app 'serving'
 * Debug mode: off

WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:8081
Press CTRL+C to quit

127.0.0.1 - - [15/Sep/2023 13:13:27] "GET /__/functions.yaml HTTP/1.1" 200 -

2023-09-15 13:13:27,719 - werkzeug - INFO - 127.0.0.1 - - [15/Sep/2023 13:13:27] "GET /__/functions.yaml HTTP/1.1" 200 -

127.0.0.1 - - [15/Sep/2023 13:13:27] "GET /__/quitquitquit HTTP/1.1" 200 -

2023-09-15 13:13:27,728 - werkzeug - INFO - 127.0.0.1 - - [15/Sep/2023 13:13:27] "GET /__/quitquitquit HTTP/1.1" 200 -

i  functions: preparing functions directory for uploading...
i  functions: packaged C:\src\propio3\webapp\backend\functions (40.74 KB) for uploading
There was an error retrieving the Firestore database. Currently, the database id is set to (default), make sure it exists.

Error: Failed to make request to http://127.0.0.1:8080/v1/projects/propio-app/databases/(default)
dominoFire commented 1 year ago

Please help me diagnosing this issue. I'm blocked on writing trigger cloud functions and I don't want to default to HTTPs functions.

This issue also happens in firebase-tools 12.5.4

aalej commented 1 year ago

Hi @dominoFire, thanks for reaching out and for providing a detailed report. I tried to reproduce the behavior you mentioned, but I encountered no errors when trying to deploy the code snippet you provided.

One thing I noticed is that in your debug logs, it makes a request to GET http://127.0.0.1:8080/v1/projects/<project_id>/databases/(default). I tried comparing this to my debug logs, and mine shows GET https://firestore.googleapis.com/v1/projects/<project_id>/databases/(default).

Could you provide additional information such as your firebase.json file or if you’re setting any environment variables during/before deploying? Please remove any PII (personal identifiable information) before sharing the file.

joehan commented 1 year ago

Specifically, could you check that the FIRESTORE_EMULATOR_HOST and FIRESTORE_URL environment vairables are not set? Looking through the provided debug logs, it seems like one of them must be set & that is causing your requests to go to the wrong host.

jjgriff93 commented 1 year ago

Getting this now as well after upgrading from a Firestore v1 trigger to v2

google-oss-bot commented 12 months ago

Hey @dominoFire. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 3 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

landa commented 12 months ago

I was getting the same error and it was fixed after unsetting @joehan's environment variables:

vscode ➜ /workspaces/functions (main) $ export FIRESTORE_EMULATOR_HOST=
vscode ➜ /workspaces/functions (main) $ firebase deploy --only functions:handle_dataset_created

It seems that this is a bug in the Firebase CLI as using the emulators shouldn't interfere with deploying.

dominoFire commented 12 months ago

Yeah, somehow I added the envvars. I haven't tested yet without the envvars, but this seems to be the error root cause. Thanks all!

image
joehan commented 11 months ago

Glad that this fixed your issues! I suspect there is a minor underlying issue here - during functions deployment, we try to get the default database(https://github.com/firebase/firebase-tools/blob/master/src/deploy/functions/services/firestore.ts#L12) using getDatabase, which uses an API client that calls firestoreOriginOrEmulator (https://github.com/firebase/firebase-tools/blob/master/src/gcp/firestore.ts#L8)

I suspect we can prevent this issue by just switching that to firestoreOrigin, but we ought to double check that doing so won't break the emualtor anywhere. @blidd-google do you mind taking a pass through the usage of the firestore functions in that file and sending a PR to switch it over if this is safe?

LouisBensard commented 9 months ago

Hello, I'm running into a similar issue. I've never user the emulator but my functions seem to deploy only on the development environment (see warning in code below). See below the result of the firebase deploy --only functions command:

(node:9312) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)

=== Deploying to 'myproject-prod'...

i  deploying functions
i  functions: preparing codebase default for deployment
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
i  artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
✔  functions: required API cloudfunctions.googleapis.com is enabled
✔  artifactregistry: required API artifactregistry.googleapis.com is enabled
✔  functions: required API cloudbuild.googleapis.com is enabled
i  functions: Loading and analyzing source code for codebase default to determine what to deploy
 * Serving Flask app 'serving'
 * Debug mode: off

WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:8081
Press CTRL+C to quit

127.0.0.1 - - [07/Dec/2023 11:02:26] "GET /__/functions.yaml HTTP/1.1" 200 -

127.0.0.1 - - [07/Dec/2023 11:02:26] "GET /__/quitquitquit HTTP/1.1" 200 -

/bin/sh: line 1:  9316 Terminated: 15          python3.11 "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/firebase_functions/private/serving.py"

(After this the functions proceed to deploy)

I have tried unsetting my environment variables, similar to suggested by @landa but no success. This is how I tried to unset them:

username@user-MacBook-Pro functions % export FIRESTORE_EMULATOR_HOST=   
username@user-MacBook-Pro functions % export FIRESTORE_URL=           
username@user-MacBook-Pro functions % firebase deploy --only functions:handle_dataset_created

I am using 2nd Gen cloud function, written in python. Deployment via the default (apparently dev server) has been successful without issue.

EDIT: I gave up and moved my Python 2nd Gen functions to Typescript 1st Gen as it doesn't have the 'development environment warning' issue. Hopefully the 2nd Gen functions get fixed.

hasnainv commented 9 months ago

@Genev-a I'm running into the same issue and I do not have any FIRESTORE_* environment variable set.

segunak commented 8 months ago

@Genev-a I'm running into the same issue and I do not have any FIRESTORE_* environment variable set.

Same issue here. Python Gen 2, no FIRESTORE_* environment variables set. Any attempts to use firebase deploy end with failure and a warning that the development server is being used.

LouisBensard commented 8 months ago

@hasnainv @segunak I gave up using Gen 2 function in Python and reverted back to using 1st gen functions written in Typescript. Even aside from this unresolved issue, the documentation online is more robust for the tried and tested 1st gen functions. Good luck!

david-shortman commented 6 months ago

I'm also getting this issue for the first time specifically when migrating database triggered functions to v2. I've successfully migrated other HTTP triggered v1 functions to v2.

david-shortman commented 6 months ago

I'm also getting this issue for the first time specifically when migrating database triggered functions to v2. I've successfully migrated other HTTP triggered v1 functions to v2.

In my case, I needed to add another role to my deployment service agent that allowed reading Cloud Datastore, which is apparently used by v2 database triggered functions.

jimmy0251 commented 3 months ago

In my case, I needed to add another role to my deployment service agent that allowed reading Cloud Datastore, which is apparently used by v2 database triggered functions.

This worked for me as well. I had to add "Cloud Datastore Viewer" role to be specific. I think error message can be improved here.