-
Notifications
You must be signed in to change notification settings - Fork 211
Firebase Cloud Functions V2: setGlobalOptions Does Not Apply Region Correctly #1502
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
Comments
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight. |
Hello @masanori-iwata. Is the issue that functions in another file don't get the region settings applied? |
While not the original author, I'm experiencing the same. And yes, for me I set setGlobalOptions in the index file but the functions themselves are in multiple other files. |
Yes. |
@masanori-iwata can you give the version of firebase --version |
the version is 13.0.2. |
I cannot reproduce this issue, unfortunately. Here's what I tried: // src/index.ts
import { setGlobalOptions } from "firebase-functions/v2/options";
import * as admin from "firebase-admin";
import { onRequest } from "firebase-functions/v2/https";
admin.initializeApp({
credential: admin.credential.applicationDefault(),
});
setGlobalOptions({ region: "asia-south1", maxInstances: 10 });
export const helloWorld = onRequest((_, res) => {
res.send("Hello from Firebase!");
});
export * from "./more-functions"; // src/more-functions.ts
import { onRequest } from "firebase-functions/v2/https";
import { onDocumentWritten } from "firebase-functions/v2/firestore";
export const anotherHelloWorld = onRequest((_, res) => {
res.send("Hello from another Firebase function!");
});
export const firestoreTrigger = onDocumentWritten(
"users/{userId}",
(context) => {
console.log("New user:", context.data?.after.data());
}
); I get the following in the terminal after running ✔ functions[helloWorld(asia-south1)] Successful update operation.
✔ functions[anotherHelloWorld(asia-south1)] Successful update operation.
✔ functions[firestoreTrigger(asia-south1)] Successful create operation. As you can see in the first file, I have set the global options only once, but it applies to all functions. Do note you have to select the region for V1 functions separately as |
Hey @exaby73, I found some interesting results:
|
@OutdatedGuy thanks for this input! I can reproduce it with a JS project. I'm guessing in TS, all files are bundled into a single file by // src/options.js
import { setGlobalOptions } from "firebase-functions/v2/options";
export function configureOptions() {
setGlobalOptions({ region: "asia-south1", maxInstances: 10 });
} That function can be called at the top of each file so that there is a single source of truth for global options |
Experiencing the same issue, using Typescript. index.ts:
Region is not applied to the functions when deployed. Unfortunately, using the workaround suggested by @exaby73 will result in the following errors:
|
@OskarGroth, it shouldn't be an issue as long as the options are the same (which is why I recommended having a global function called). But yes, I agree this is not ideal in the slightest. |
I'm experiencing the same, despite bundling to a single file (from TypeScript). It fails when doing setGlobalOptions in the top-level index-file (which re-exports functions from discrete files), and it works then it is done from within the discrete files. Transpiled and bundled output that works:
Bundled output that fails:
Seems like the order here matters, and the way (Vite) bundles is the real problem (for me). |
I would think that vite wouldn't mess with the ordering of the code. Can you give an example 2 files I can pop into a new generated TS project to try and investigate what is going on? |
I just stumbled across this issue when I wanted to deploy a document trigger with |
@GerroDen Could you check which region your database is from the Google Cloud Console |
@exaby73 Google Cloud Console says like Firebase Console
|
FWIW, I could not get |
We are seeing the same issue after migrating the TypeScript project from CommonJS to ES Modules. This was working fine before in setGlobalOptions({ region: 'europe-west1' })
export * from './functions' but now with ESM the global options are not applied. Going back to CommonJS is not a solution, unfortunately. |
I ended up replacing setGlobalOptions and explicitly set the region for all functions individually. |
I fear we have to go the same route but |
I tried to use dynamic imports. Interestingly the region is applied for functions imported dynamically but then unfortunately all functions have a export default {
...(await import('./function1.js')),
...(await import('./function2.js')),
} It's possible to work around this by exporting each function separately, however it becomes quite tedious if you have many functions and/or your modules export more than just one function. export const function1 = (await import('./function1.js')).function1
export const function2 = (await import('./function1.js')).function2 |
When migrating from CommonJS to ES Modules (ECMAScript) in a Firebase Functions project using TypeScript, I had the same issue where setGlobalOptions() from "firebase-functions/v2/options" was not being applied properly. After switching to ES Modules, my Cloud Functions no longer respected the global configuration options set using:
The issue seems to be related to the evaluation order of module imports in ES Modules. Since ES Modules execute imports before any statements in the main file, some Firebase functions were being defined before setGlobalOptions() was applied. Solution that worked for me: Fixed Code (TypeScript + ES Modules)
|
Well, that's exactly what I wrote above 😉 However this solution becomes impractical when you have many Cloud Functions. |
Related issues
[REQUIRED] Version info
node:
v18.7.0
firebase-functions:
v4.5.0
firebase-tools:
firebase-admin:
v11.11.1
[REQUIRED] Test case
In Firebase Cloud Functions V2, when using
setGlobalOptions
to set the region toasia-northeast1
, some Firestore trigger functions are still being initialized in the default regionus-central1
.[REQUIRED] Steps to reproduce
setGlobalOptions
to specify the region asasia-northeast1
.us-central1
instead of the specifiedasia-northeast1
.[REQUIRED] Expected behavior
All functions should respect the global options set, especially regarding the region. The expected behavior is that functions are initialized in the specified region
asia-northeast1
.[REQUIRED] Actual behavior
Functions such as Firestore triggers are being initialized in the
us-central1
region, despite thesetGlobalOptions
specifyingasia-northeast1
.code
log
Were you able to successfully deploy your functions?
Yes, the functions were deployed successfully, but the regional setting was not applied as expected.
The text was updated successfully, but these errors were encountered: