unjs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.unjs.io
MIT License
5.91k stars 497 forks source link

Error when deploying Gen 1 Firebase function with no region set in config #1698

Open AlexLavoie42 opened 1 year ago

AlexLavoie42 commented 1 year ago

Environment

Nuxt 3.7.1 with Nitro 2.6.2

Reproduction

Hard to provide a reproduction since deployment is necessary to reproduce.

I have tested with older versions to find the root cause, and it seems that providing no region in firebase config will cause the error. Once I provided a region the error no longer occured.

Describe the bug

When deploying, all you will see in console is Error: An unexpected error has occurred.

After digging into the firebase-debug.log, you will see this error:

[debug] [2023-09-06T00:35:12.299Z] TypeError: Cannot read properties of null (reading 'match')
    at resolveString (/usr/lib/node_modules/firebase-tools/lib/deploy/functions/params.js:31:27)
    at /usr/lib/node_modules/firebase-tools/lib/deploy/functions/params.js:47:36
    at Array.map (<anonymous>)
    at Object.resolveList (/usr/lib/node_modules/firebase-tools/lib/deploy/functions/params.js:47:21)
    at toBackend (/usr/lib/node_modules/firebase-tools/lib/deploy/functions/build.js:180:30)
    at Object.resolveBackend (/usr/lib/node_modules/firebase-tools/lib/deploy/functions/build.js:73:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async prepare (/usr/lib/node_modules/firebase-tools/lib/deploy/functions/prepare.js:68:62)
    at async chain (/usr/lib/node_modules/firebase-tools/lib/deploy/index.js:38:9)
    at async deploy (/usr/lib/node_modules/firebase-tools/lib/deploy/index.js:95:5)

Obviously this is not very helpful. After some trial and error, I discovered that the functions.yaml output was slightly different between v2.6.2 & v.2.5.2:

v2.6.2:

{
    "endpoints": {
        "server": {
            "platform": "gcfv1",
            "availableMemoryMb": null,
            "timeoutSeconds": null,
            "minInstances": null,
            "maxInstances": null,
            "ingressSettings": null,
            "serviceAccountEmail": null,
            "vpc": null,
            "region": [
                null
            ],
            "httpsTrigger": {},
            "entryPoint": "server"
        }
    },
    "specVersion": "v1alpha1",
    "requiredAPIs": []
}

v2.5.2:

{
    "endpoints": {
        "server": {
            "platform": "gcfv1",
            "availableMemoryMb": null,
            "timeoutSeconds": null,
            "minInstances": null,
            "maxInstances": null,
            "ingressSettings": null,
            "serviceAccountEmail": null,
            "vpc": null,
            "httpsTrigger": {},
            "entryPoint": "server"
        }
    },
    "specVersion": "v1alpha1",
    "requiredAPIs": []
}

As you can see, the older version did not have "region" defined at all while the new version has it set to [null] by default. After discovering this I set the region in the nitro config and sure enough it deployed without the same error.

Additional context

No response

Logs

No response

AlexLavoie42 commented 1 year ago

So I've been doing some digging, and it seems like .region(functions.RESET_VALUE) is the same as doing .region(undefined). Would this be a bug with firebase? The only way I can seem to work around this is by removing the region function entirely.

Maybe @posva could provide some insight?

howbizarre commented 1 year ago

In my firebase-debug.log it says that the functions.yaml does not exist

The last few lines of the log

...
[debug] [2023-09-12T10:33:36.567Z] Building nodejs source
[info] i  functions: Loading and analyzing source code for codebase default to determine what to deploy 
[debug] [2023-09-12T10:33:36.568Z] Could not find functions.yaml. Must use http discovery
[debug] [2023-09-12T10:33:36.571Z] Found firebase-functions binary at '/home/how/Dev/saas-nuxt-channel/node_modules/.bin/firebase-functions'
[info] Serving at port 8565

[debug] [2023-09-12T10:33:36.879Z] Got response from /__/functions.yaml {"endpoints":{"server":{"platform":"gcfv1","availableMemoryMb":null,"timeoutSeconds":null,"minInstances":null,"maxInstances":null,"ingressSettings":null,"serviceAccountEmail":null,"vpc":null,"region":[null],"httpsTrigger":{},"entryPoint":"server"}},"specVersion":"v1alpha1","requiredAPIs":[]}
[info] shutdown requested via /__/quitquitquit

[debug] [2023-09-12T10:33:36.984Z] TypeError: Cannot read properties of null (reading 'match')
    at resolveString (/home/how/node_modules/firebase-tools/lib/deploy/functions/params.js:31:27)
    at /home/how/node_modules/firebase-tools/lib/deploy/functions/params.js:47:36
    at Array.map (<anonymous>)
    at Object.resolveList (/home/how/node_modules/firebase-tools/lib/deploy/functions/params.js:47:21)
    at toBackend (/home/how/node_modules/firebase-tools/lib/deploy/functions/build.js:180:30)
    at Object.resolveBackend (/home/how/node_modules/firebase-tools/lib/deploy/functions/build.js:73:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async prepare (/home/how/node_modules/firebase-tools/lib/deploy/functions/prepare.js:68:62)
    at async chain (/home/how/node_modules/firebase-tools/lib/deploy/index.js:38:9)
    at async deploy (/home/how/node_modules/firebase-tools/lib/deploy/index.js:95:5)
[error] 
[error] Error: An unexpected error has occurred.

My environment:


$ firebase -V
12.5.3
AlexLavoie42 commented 1 year ago

In my firebase-debug.log it says that the functions.yaml does not exist

This is normal, I am referring to the output after [debug] [2023-09-12T10:33:36.879Z] Got response from /__/functions.yaml

bcspragu commented 1 year ago

I ran into the same issue. Without digging into anything any further, I'm guessing this broke as part of adding support for the 2nd generation Cloud Functions runtime, which supports a region field.

My (dumb, not necessarily recommended) fix was just to upgrade our whole app to Node 18 + the 2nd gen runtime, which didn't require any changes except updating my nuxt.config.ts nitro section to:

  nitro: {
    preset: 'firebase',
    firebase: {
      nodeVersion: '18', // 16 reached EOL September 2023, https://nodejs.org/en/blog/announcements/nodejs16-eol
      gen: 2,
      httpsOptions: {
        region: 'us-central1',
        // Concurrency defaults to 80, we should tweak as desired/needed.
        // concurrency: 80,
      },
    },

And correspondingly, as the documentation notes, updating my firebase.json to include:

  "functions": {
    "source": ".output/server",
    "runtime": "nodejs18" <-- the new part
  },

This...just worked, with no further updates required on my part. It fixed the deploy, and the app seems to be humming along just fine on the new runtime.

Edit: I also had to update firebase-tools to ^12.5.4 in my package.json, as mentioned in the Cloud Functions migration docs

howbizarre commented 1 year ago

....he 2nd gen runtime,

Yee - with the 2nd generation it works, but have to delete the old function before deploy.

bcspragu commented 1 year ago

Yee - with the 2nd generation it works, but have to delete the old function before deploy.

Ah yes, though the error message I got when trying to push the first time indicates the GCP will actually fix that at some point. For what it's worth, it must have been less than two minutes of downtime, our probers didn't even pick it up. I just deleted the existing 1st gen function in the UI and immediately ran the deploy.

The 'new' Cloud Function even picked up existing metrics and whatnot from the original