-
Notifications
You must be signed in to change notification settings - Fork 903
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
Firebase emulator does not pick up environment variables even though it reports they have been loaded (Follow-up for #5219) #5669
Comments
I am experiencing the same exact issues as described above. I am totally new to firebase/functions so I wasn't sure if I was doing something wrong, but I've now invested a couple of hours into this, and it's looking like this has been an issue for over a year. I think I may seek to use another library. |
Hi @sceee sorry it's taken so long for us to respond, thanks for your patience. So I read the closed thread with your comments about the setup that you have and I'm unable to reproduce the bug on my end. Here's what my setup looks like: (maybe you can spot something that you are doing differently)
.env:
.env-local:
enviroment.ts:
index.ts:
my-test.sh:
Then I'm running
|
Hi @colerogers , thanks for your reply. Maybe the difference between using v1 vs. v2 functions could be the issue? Are the environment variables maybe only supported with v2 functions and not with v1 functions? |
@sceee I just tried using a v1 function and I'm still able to correctly obtain the environment variable. Are you using a monorepo setup? |
@colerogers yes, it is a monorepo setup. |
Okay this is making a bit more sense to me. When you invoke the emulator or deployment process, we try to read your .env files using a function called loadUserEnvs(). In the emulator, that functions source string is populated at start time here. My guess right now is that your .env files aren't in the path that the emulator is expecting b/c of how monorepos structure the project. Officially, we don't support monorepo setups, only standard npm workflows (here's the feature request for deployment: #653). What monorepo are you using? Does this only happen in the emulator or in GCP too? Can we try to move the .env files to unblock you? |
@colerogers when mentioning that I am using a monorepo setup, I only meant that I use one git repository that contains multiple npm projects in multiple root directories like this:
The projects itself (e.g. Anyway, I tried to further trace it down by debugging into the code of But for Is there anything line you can think of that I can further debug to further trace this down? |
Hmm interesting... I think the best path forward is if you could create a minimal reproducible example that I could use to debug on my machine, otherwise we'd just go back-and-forth on this thread. I made https://github.com/colerogers/issue-5669 which works shows |
@colerogers thanks, I forked your repo here https://github.com/sceee/issue-5669 and made some changes to adjust it to the way my project is structured. By this I can demo the behavior that fails for me. The changes basically are:
Running
inside the The log shows the environment files are loaded but the environment variable
The Also, the test fails (it just asserts on the return value of the Function that is I hope this simple repro helps so you might possibly also be able to reproduce the failing behavior? |
@sceee so I believe this setup is actually WAI. Basically, you are just calling the raw imported function as a normal js/ts function from mocha without going through the emulator, so the emulated envs do not exist. If you changed up the function invocation to HTTP (or probably more correct to use the Firebase JS SDK to invoke the callable, not sure if you can do that in mocha), you'd go through the emulator and pickup all the correct runtime properties. Also, the firebase-functions-test SDK is meant for unit testing your functions. If you wanted to strictly unit test them, a common pattern in mocha is to directly define your env vars in your test suite or pass in a .env file with the Sorry about the headache but I'm gonna mark this as closed. Let me know If you think I made a mistake |
@colerogers ah I see. Handing over the env variables additionally directly mocha was exactly what I was already doing before trying to migrate to the new params possibility, I just thought this would now become obsolete since the emulators now "natively" support the environment variables but I understand the unit tests do not run "through" the emulator. So as maybe someone else might benefit from this: I will now try to get it running again for all environments by doing both:
This should make it working
😃 Thanks for your help! |
[REQUIRED] Environment info
firebase-tools: 11.25.3
Platform: Windows
[REQUIRED] Test case
See #5219 (comment)
[REQUIRED] Steps to reproduce
The following issue was closed but the behavior described in #5219 (comment) still occurs and the environment variables are not loaded.
[REQUIRED] Expected behavior
Firebase emulator does pick up the environment variables from the
.env
files correctly.[REQUIRED] Actual behavior
#5219 (comment)
The text was updated successfully, but these errors were encountered: