firebase / firebase-tools

The Firebase Command Line Tools
MIT License
4.04k stars 952 forks source link

Emulators for functions v2 not supporting different regions #7580

Open KantiKuijk opened 3 months ago

KantiKuijk commented 3 months ago

[REQUIRED] Environment info

firebase-tools:13.15.4

Platform:macOS

[REQUIRED] Test case

index.ts:

import { onValueCreated } from "firebase-functions/v2/database";
import { setGlobalOptions } from "firebase-functions/v2";
import { region } from "firebase-functions/v1";

setGlobalOptions({ region: "europe-west1" });

export const NewInQueueV1 = region("europe-west1")
  .database.ref("mailing/queue/{pushID}/")
  .onCreate(() => {
    console.log("There is a new mail in the mailing queue (v1)");
  });

export const NewInQueueV2 = onValueCreated("mailing/queue/{pushID}/", () => {
  console.log("There is a new mail in the mailing queue (v2)");
});

[REQUIRED] Steps to reproduce

Create a new project with the default firebase in region europe-west1 and run the firebase functions and database emulators. Using the emulators ui, import the following json:

{
  "mailing": {
    "queue": {
      "somePushID": {
        "email": "test@email.com",
        "action": "whitelist"
      }
    }
  }
}

making sure the checkbox "Also execute database triggers in the Functions emulator" is checked. Observe the log.

[REQUIRED] Expected behavior

Both functions should be initialised without warning and on importing the json, both console.log statements should be executed indicating both functions were triggered.

[REQUIRED] Actual behavior

The v2 version warns about functions[europe-west1-NewInQueueV2]: function region is defined outside the database region, will not trigger.. On import, only the v1 functions Is triggered. Even when repeating the setup without the v1 function in index.ts (in case a second function triggering on the same path would be a problem), the v2 function does not get triggered.

I found the following code:

// TODO(colerogers): yank/change if RTDB emulator ever supports multiple regions
    if (region !== "us-central1") {
      this.logger.logLabeled(
        "WARN",
        `functions[${id}]`,
        `function region is defined outside the database region, will not trigger.`,
      );
    }

Where the comment together with the if clause seems to indicate that regions other than us-central1 are not supported in the emulators. (so I guess this might be interpreted as a feature request instead of an issue?)

Also, the warning message itself is misleading, since it infers the location of the database is wrong, but I believe the message is shown based on the region of the function. In this case, since the region is prepended to the function name, the warning seems to indicate that the referenced database is not in the preferred region europe-west1, when the warning is due to the region of the function, not the database.

KantiKuijk commented 3 months ago

A workaround is to not set the global region variable when the emulators are running:

if (process.env["FUNCTIONS_EMULATOR"] !== "true") {
  setGlobalOptions({ region: "europe-west1" });
}

If you set the region on a function-level, it may be more cumbersome to do so.

This makes the database functions trigger again, however, this will also apply to https functions, changing their URLs.

risalfajar commented 1 month ago

This also happened in v2 Realtime Database Triggers. When I'm using onValueUpdated with global region set, it'll print this message in CLI: function region is defined outside the database region, will not trigger..

This does not happen in v1 triggers.