Skip to content

Commit

Permalink
Inject FIREBASE_CONFIG environment variable for scripts executed vi…
Browse files Browse the repository at this point in the history
…a emulators:exec command. (#5544)

As discussed in #5536 (comment), we learned that old versions of Firebase Functions SDK always made `process.env.FIREBASE_CONFIG` available to the underlying script that imported the Firebase Functions SDK.

While making `process.env.FIREBASE_CONFIG` available wasn't intentional, we'll consider this a case of [Hyrum's Law](https://www.hyrumslaw.com/) and will try our best to fix the regression by making `process.env.FIREBASE_CONFIG` environment variable available if not set in the parent process.

Fixes #5536
  • Loading branch information
taeold committed Feb 23, 2023
1 parent 07cc578 commit cc889a5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
- Fixes bug where passing `--port` flag in `functions:shell` does not set which port to emulate functions (#5521)
- Improve error message during deploy when given invalid hosting rewrite rule (#5533)
- Generate ESM-compatible SSR function for web frameworks (#5540)
- Fix bug emulators:exec script didn't populate FIREBASE_CONFIG environment variable (#5544)
16 changes: 14 additions & 2 deletions src/emulator/commandUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,18 @@ async function runScript(script: string, extraEnv: Record<string, string>): Prom
utils.logBullet(`Running script: ${clc.bold(script)}`);

const env: NodeJS.ProcessEnv = { ...process.env, ...extraEnv };
// Hyrum's Law strikes here:
// Scripts that imported older versions of Firebase Functions SDK accidentally made
// the FIREBASE_CONFIG environment variable always available to the script.
// Many users ended up depending on this behavior, so we conditionally inject the env var
// if the FIREBASE_CONFIG env var isn't explicitly set in the parent process.
if (env.GCLOUD_PROJECT && !env.FIREBASE_CONFIG) {
env.FIREBASE_CONFIG = JSON.stringify({
projectId: env.GCLOUD_PROJECT,
storageBucket: `${env.GCLOUD_PROJECT}.appspot.com`,
databaseURL: `https://${env.GCLOUD_PROJECT}.firebaseio.com`,
});
}

const emulatorInfos = EmulatorRegistry.listRunningWithInfo();
setEnvVarsForEmulators(env, emulatorInfos);
Expand Down Expand Up @@ -448,7 +460,7 @@ const JAVA_HINT = "Please make sure Java is installed and on your system PATH.";
/**
* Return whether Java major verion is supported. Throws if Java not available.
*
* @returns Java major version (for Java >= 9) or -1 otherwise
* @return Java major version (for Java >= 9) or -1 otherwise
*/
export async function checkJavaMajorVersion(): Promise<number> {
return new Promise<string>((resolve, reject) => {
Expand Down Expand Up @@ -510,7 +522,7 @@ export async function checkJavaMajorVersion(): Promise<number> {
});
}).then((output) => {
let versionInt = -1;
const match = output.match(JAVA_VERSION_REGEX);
const match = JAVA_VERSION_REGEX.exec(output);
if (match) {
const version = match[1];
versionInt = parseInt(version, 10);
Expand Down

0 comments on commit cc889a5

Please sign in to comment.