Skip to content
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

Populate FIREBASE_CONFIG environment variable for scripts executed via emulators:exec #5536

Closed
ReonaN opened this issue Jan 22, 2023 · 6 comments · Fixed by #5544
Closed

Comments

@ReonaN
Copy link

ReonaN commented Jan 22, 2023

Related issues

no

[REQUIRED] Version info

node: 16.18.0

firebase-functions: 4.2.0

firebase-tools: 11.15.0

firebase-admin: 11.2.0

[REQUIRED] Test case

import * as admin from "firebase-admin";
import 'firebase-functions';

admin.initializeApp()
console.log('config', process.env.FIREBASE_CONFIG)

[REQUIRED] Steps to reproduce

I upgraded firebase-functions from v3.24.1 to v4.2.0.
After upgrading this package, I had an error in testing, and I noticed FIREBASE_CONFIG is missing.
There are console logs after running the above test case.

BEFORE (v3.24.1)
config {"databaseURL":"<my database url>","storageBucket":"<my backet>","projected":"<myproject id>"}

AFTER (v4.2.0)
config undefined

Also, I successfully got the GCLOUD_PROJECT environment variable.

[REQUIRED] Expected behavior

We can get the FIREBASE_CONFIG environment variable after initializing an app.

[REQUIRED] Actual behavior

FIREBASE_CONFIG environment variable is missing.
We got undefined when we referred to process.env.FIREBASE_CONFIG.

Were you able to successfully deploy your functions?

yes

@google-oss-bot
Copy link
Contributor

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@ReonaN ReonaN changed the title FIREBASE_CONFIG environment variables are missing after upgrading to v4 FIREBASE_CONFIG environment variable is missing after upgrading to v4 Jan 22, 2023
@unxavi
Copy link

unxavi commented Feb 13, 2023

Hey, I just updated our dependencies from v3 to v4 and we are seeing the same. Is there any way we can help?

@ReonaN could you solved it?

@ReonaN
Copy link
Author

ReonaN commented Feb 15, 2023

@unxavi Not yet.
I tried again after updating the latest version of some related packages (firebase-admin, firebase-tools), but nothing changed.

@taeold taeold self-assigned this Feb 15, 2023
@taeold
Copy link
Contributor

taeold commented Feb 15, 2023

@ReonaN @unxavi Thanks for surfacing the issue.

Can I ask under what setting you are running your code? I can think of 2 situations where FIREBASE_CONFIG will be populated:

  1. Deployed to Google Cloud Functions
  2. Executing under Firebase Emulator Suite

In other situations, I can see that FIREBASE_CONFIG to be missing. I'll go take a look, but if you can share more information about your use cases in the meantime, I'd appreciate it.

@taeold taeold added Needs: Author Feedback Issues awaiting author feedback and removed needs-triage labels Feb 15, 2023
@unxavi
Copy link

unxavi commented Feb 15, 2023

hi @taeold for me this happening when running my tests on the Firebase emulator suite. however with the version 3 of firebase functions it's there, but when updating to version 4, I have a piece of code complaining that's undefined.

export function isProEnvironment(): boolean {
  if (process.env.FIREBASE_CONFIG != undefined) {
    const projectId = JSON.parse(process.env.FIREBASE_CONFIG).projectId;
    if (projectId != undefined) {
      // We should get this from the project/environment deploy variables
      return projectId === proProjectId;
    } else {
      throw new Error("projectId is undefined");
    }
  } else {
      throw new Error("process.env.FIREBASE_CONFIG is undefined");
  }
}

we are hitting the line

 throw new Error("process.env.FIREBASE_CONFIG is undefined");

i'm running the tests with the following command:

npm run build && firebase emulators:exec --project myProjectID 'npm test'

@taeold
Copy link
Contributor

taeold commented Feb 18, 2023

@unxavi Thanks for sharing more detail.

I inspected the code we removed in v3 → v4 unintentionally populated FIREBASE_CONFIG values.

Now I recognized that this behavior has existed for a long time, and I don't think breaking your setup is okay.

I'm going to propose that Firebase CLI always injects FIREBASE_CONFIG to scripts executed via emulators:exec.

In the meantime, if projectId is the only information you extract from FIREBASE_CONFIG, you'd can retrieve that info from process.env.GCLOUD_PROJECT instead.

Stay tuned on a longer term fix for this issue!

@taeold taeold transferred this issue from firebase/firebase-functions Feb 18, 2023
@taeold taeold changed the title FIREBASE_CONFIG environment variable is missing after upgrading to v4 Populate FIREBASE_CONFIG environment variable for scripts executed via emulators:exec Feb 18, 2023
@taeold taeold closed this as completed in cc889a5 Feb 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants