Skip to content

Commit 0c3a688

Browse files
authored
Fix bug where environment variables for an emulated function did not consider emulators running remotely. (#5269)
#5079 refactored the way to set environment variables on Functions Emulator. As implemented, the refactored function does not consider "remote" emulators i.e. emulator started by another instance of `emulators:start` command. This broke users who relies this to connect `functions:shell` command to the remote emulators. Fixes #5225
1 parent 2a25c5f commit 0c3a688

File tree

5 files changed

+44
-42
lines changed

5 files changed

+44
-42
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
- Support Next.js Middleware (#5320)
44
- Log the reason for a Cloud Function if needed in Next.js (#5320)
55
- Fixed service enablement when installing extensions with v2 functions (#5338)
6+
- Fix bug where functions:shell command didn't connect to emulators running on other processes. (#5269)

scripts/triggers-end-to-end-tests/tests.ts

100755100644
File mode changed.

src/emulator/commandUtils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,8 @@ async function runScript(script: string, extraEnv: Record<string, string>): Prom
330330

331331
const env: NodeJS.ProcessEnv = { ...process.env, ...extraEnv };
332332

333-
setEnvVarsForEmulators(env);
333+
const emulatorInfos = EmulatorRegistry.listRunningWithInfo();
334+
setEnvVarsForEmulators(env, emulatorInfos);
334335

335336
const proc = childProcess.spawn(script, {
336337
stdio: ["inherit", "inherit", "inherit"] as childProcess.StdioOptions,

src/emulator/env.ts

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,43 @@
11
import { Constants } from "./constants";
2-
import { Emulators } from "./types";
3-
import { EmulatorRegistry } from "./registry";
2+
import { EmulatorInfo, Emulators } from "./types";
3+
import { formatHost } from "./functionsEmulatorShared";
44

55
/**
66
* Adds or replaces emulator-related env vars (for Admin SDKs, etc.).
77
* @param env a `process.env`-like object or Record to be modified
8+
* @param emulators the emulator info to use
89
*/
9-
export function setEnvVarsForEmulators(env: Record<string, string | undefined>): void {
10-
if (EmulatorRegistry.isRunning(Emulators.DATABASE)) {
11-
env[Constants.FIREBASE_DATABASE_EMULATOR_HOST] = EmulatorRegistry.url(Emulators.DATABASE).host;
12-
}
13-
14-
if (EmulatorRegistry.isRunning(Emulators.FIRESTORE)) {
15-
const { host } = EmulatorRegistry.url(Emulators.FIRESTORE);
16-
env[Constants.FIRESTORE_EMULATOR_HOST] = host;
17-
env[Constants.FIRESTORE_EMULATOR_ENV_ALT] = host;
18-
}
19-
20-
if (EmulatorRegistry.isRunning(Emulators.STORAGE)) {
21-
const { host } = EmulatorRegistry.url(Emulators.STORAGE);
22-
env[Constants.FIREBASE_STORAGE_EMULATOR_HOST] = host;
23-
// The protocol is required for the Google Cloud Storage Node.js Client SDK.
24-
env[Constants.CLOUD_STORAGE_EMULATOR_HOST] = `http://${host}`;
25-
}
26-
27-
if (EmulatorRegistry.isRunning(Emulators.AUTH)) {
28-
env[Constants.FIREBASE_AUTH_EMULATOR_HOST] = EmulatorRegistry.url(Emulators.AUTH).host;
29-
}
30-
31-
if (EmulatorRegistry.isRunning(Emulators.HUB)) {
32-
env[Constants.FIREBASE_EMULATOR_HUB] = EmulatorRegistry.url(Emulators.HUB).host;
33-
}
34-
35-
const pubsubEmulator = EmulatorRegistry.isRunning(Emulators.PUBSUB);
36-
if (pubsubEmulator) {
37-
env[Constants.PUBSUB_EMULATOR_HOST] = EmulatorRegistry.url(Emulators.PUBSUB).host;
38-
}
39-
40-
if (EmulatorRegistry.isRunning(Emulators.EVENTARC)) {
41-
// The protocol is required for the Firebase Admin Node.js SDK for Eventarc.
42-
// https://github.com/firebase/firebase-admin-node/blob/ee60cd1acb8722ba4081b9837d2f90101e2b3227/src/eventarc/eventarc-client-internal.ts#L105
43-
env[Constants.CLOUD_EVENTARC_EMULATOR_HOST] = `http://${
44-
EmulatorRegistry.url(Emulators.EVENTARC).host
45-
}`;
10+
export function setEnvVarsForEmulators(
11+
env: Record<string, string | undefined>,
12+
emulators: EmulatorInfo[]
13+
): void {
14+
for (const emu of emulators) {
15+
const host = formatHost(emu);
16+
switch (emu.name) {
17+
case Emulators.FIRESTORE:
18+
env[Constants.FIRESTORE_EMULATOR_HOST] = host;
19+
env[Constants.FIRESTORE_EMULATOR_ENV_ALT] = host;
20+
break;
21+
case Emulators.DATABASE:
22+
env[Constants.FIREBASE_DATABASE_EMULATOR_HOST] = host;
23+
break;
24+
case Emulators.STORAGE:
25+
env[Constants.FIREBASE_STORAGE_EMULATOR_HOST] = host;
26+
// The protocol is required for the Google Cloud Storage Node.js Client SDK.
27+
env[Constants.CLOUD_STORAGE_EMULATOR_HOST] = `http://${host}`;
28+
break;
29+
case Emulators.AUTH:
30+
env[Constants.FIREBASE_AUTH_EMULATOR_HOST] = host;
31+
break;
32+
case Emulators.HUB:
33+
env[Constants.FIREBASE_EMULATOR_HUB] = host;
34+
break;
35+
case Emulators.PUBSUB:
36+
env[Constants.PUBSUB_EMULATOR_HOST] = host;
37+
break;
38+
case Emulators.EVENTARC:
39+
env[Constants.CLOUD_EVENTARC_EMULATOR_HOST] = `http://${host}`;
40+
break;
41+
}
4642
}
4743
}

src/emulator/functionsEmulator.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export interface FunctionsEmulatorArgs {
116116
quiet?: boolean;
117117
disabledRuntimeFeatures?: FunctionsRuntimeFeatures;
118118
debugPort?: number;
119-
remoteEmulators?: { [key: string]: EmulatorInfo };
119+
remoteEmulators?: Record<string, EmulatorInfo>;
120120
adminSdkConfig?: AdminSdkConfig;
121121
projectAlias?: string;
122122
}
@@ -1160,7 +1160,11 @@ export class FunctionsEmulator implements EmulatorInstance {
11601160
enableCors: true,
11611161
});
11621162

1163-
setEnvVarsForEmulators(envs);
1163+
let emulatorInfos = EmulatorRegistry.listRunningWithInfo();
1164+
if (this.args.remoteEmulators) {
1165+
emulatorInfos = emulatorInfos.concat(Object.values(this.args.remoteEmulators));
1166+
}
1167+
setEnvVarsForEmulators(envs, emulatorInfos);
11641168

11651169
if (this.args.debugPort) {
11661170
// Start runtime in debug mode to allow triggers to share single runtime process.

0 commit comments

Comments
 (0)