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

Project-specific .env files aren't loaded when running functions emulator #4239

Closed
larrifax opened this issue Mar 2, 2022 · 30 comments · Fixed by #4273
Closed

Project-specific .env files aren't loaded when running functions emulator #4239

larrifax opened this issue Mar 2, 2022 · 30 comments · Fixed by #4273

Comments

@larrifax
Copy link

larrifax commented Mar 2, 2022

[REQUIRED] Environment info

firebase-tools: 10.2.1

Platform: Ubuntu

[REQUIRED] Test case

The documentation around environment variables using .env-files while using the local functions emulator states the following:

Contents of .env.local take precedence over .env and the project-specific .env file
Source: https://firebase.google.com/docs/functions/config-env#emulator_support

However, when running locally the project-specific .env file isn't even loaded, only the .env and .env.local is.
And indeed, the source code doesn't even attempt to load project-specific files when running in an emulator setting:

if (isEmulator) {
files.push(FUNCTIONS_EMULATOR_DOTENV);
} else {
files.push(`.env.${projectId}`);
if (projectAlias && projectAlias.length) {
files.push(`.env.${projectAlias}`);
}
}

According to my interpretation of the documentation linked above, there's a mismatch between the documentation and the implementation, which should be resolved one way or another.

[REQUIRED] Steps to reproduce

  1. Add a .env, .env.development and .env.local file to your project, filling them with different environment variables
  2. firebase use development
  3. firebase emulators:start --only functions
  4. Trigger a function

[REQUIRED] Expected behavior

Functions emulator is started, and logs should show that all relevant .env* files are loaded:
i functions: Loaded environment variables from .env, .env.development, .env.local.

[REQUIRED] Actual behavior

Functions emulator is started, but logs shows that the project specific .env file isn't loaded (.env.development is missing from output):
i functions: Loaded environment variables from .env, .env.local.

@larrifax larrifax added the bug label Mar 2, 2022
@jaytavares
Copy link

jaytavares commented Mar 2, 2022

I'm seeing this same behavior. I interpreted the documentation the same as you did. Looking at the docs again, it could be interpreted in a way that matches the implementation where they don't intend the project-specific configuration to ever be used in the emulator. That fact isn't super clear, but I think the wording of the section offers a clue: "Deploying multiple sets of environment variables". "Deploying" in this case would suggest they are not talking about the emulator.

I'm thinking that now that I understand this, I can make the current implementation work for my purposes. But, I agree that at a minimum the documentation should probably be updated to clarify this fact.

@taeold taeold self-assigned this Mar 2, 2022
@taeold
Copy link
Contributor

taeold commented Mar 2, 2022

Thanks for a thorough writeup. I think the documentation is right and the implementation is wrong. We will work on a fix to be shipped in the next release.

@larrifax
Copy link
Author

Thanks @taeold. This is maybe another bug/feature request, but I also noticed that .env variables aren't available when the "startup code" is ran, so it's currently not possible to e.g. store the region in an environment variable and use it like so:

functions/index.js:

import * as functions from "firebase-functions";
import * as callable from "./callables";

export const someFunction = functions.region(process.env.REGION).https.onCall(callable.someFunction);

The environment variables aren't available to use at the time when .region() or .runWith() are evaluated. They're only available "within onCall".

Is this something you're aware of/decided not to support? Or should I create a new bug/feature request for it?

@jaytavares
Copy link

Thanks @taeold. This is maybe another bug/feature request, but I also noticed that .env variables aren't available when the "startup code" is ran, so it's currently not possible to e.g. store the region in an environment variable and use it like so:

functions/index.js:


import * as functions from "firebase-functions";

import * as callable from "./callables";



export const someFunction = functions.region(process.env.REGION).https.onCall(callable.someFunction);

The environment variables aren't available to use at the time when .region() or .runWith() are evaluated. They're only available "within onCall".

Is this something you're aware of/decided not to support? Or should I create a new bug/feature request for it?

I've had to remove my dynamic exports because of this. It would be nice to have this functionality back

@pradella
Copy link

In my case, when I start the emulator the message below is not even showing:

i functions: Loaded environment variables from .env, .env.local.

taeold added a commit that referenced this issue Mar 14, 2022
…#4273)

Today, Functions Emulator does not load project-specific environment variables. This contradicts the public documentation which states:

> When using a local Cloud Functions emulator, you can override environment variables for your project by setting up a .env.local file. Contents of .env.local take precedence over .env and the project-specific .env file.

https://firebase.google.com/docs/functions/config-env#emulator_support

The patch fixes the bug by making appropriate changes to load project-specific environment variables (either in `.env.projectId` or `.env.projectAlias`) when emulating functions.

Fixes #4239
@jtfrom9
Copy link

jtfrom9 commented Mar 17, 2022

#4239 (comment)

Hi @taeold, could you comment on the above point?
I’m also hoping .env variables are available on .region() or .runWith() are evaluated

@michaelalhilly
Copy link

michaelalhilly commented May 25, 2022

@taeold Hi. This is still not working for me locally. The variables are loaded from .env if I remove the .env.local file but the variables are not loaded at all if I add a .env.local file. I'm also importing variables from Secrets Manager which is working fine. Is this suppose to be working now?

@sartoshi-foot-dao
Copy link

Not quite the described issue, but may help those who landed here searching for a related issue.

If any key in .env contains any lowercase characters, the entire .env is ignored by emulator, and firebase deploy --only functions will produce this error:

Key X_lowercase_Key must start with an uppercase ASCII letter or underscore, and then consist of uppercase ASCII letters, digits, and underscores.

Please hide/delete if irrelevant.

@erikkjernlie
Copy link

.env variables are only available during application runtime, and not when you are deploying. You have to use Firebase's remote config if you want to use env variables when initializing functions, e.g. functions.config().environment.isDev.

@juane1000
Copy link

juane1000 commented Aug 22, 2022

.env variables are only available during application runtime, and not when you are deploying. You have to use Firebase's remote config if you want to use env variables when initializing functions, e.g. functions.config().environment.isDev.

This is the old method for environment variables and what I used in previous projects. However, though not directly said in the docs, their language about migrating to the new .env method and how "config" is removed from V2 cloud functions makes me worry that it may be deprecated in the not too distant future.

My hope is that this is an oversight as @larrifax mentioned.

EDIT: The docs also suggest that if you have V2 cloud functions alongside V1 functions then deploys will fail if using "config()";

@Wian-TMC
Copy link

Why is this closed ?

You cannot deploy to v2 of functions when using functions.config(), and that seems to be the only solution for accessing any deployment variables. This is a big issue

@NickVanzo
Copy link

Same here, my .env or .env.local files are not loaded when I start the emulator

@erikkjernlie
Copy link

.env variables are loaded into the application runtime (after the configuration in the beginning), so you need to use remoteConfig for using env variables during deploy :)

@Wian-TMC
Copy link

.env variables are loaded into the application runtime (after the configuration in the beginning), so you need to use remoteConfig for using env variables during deploy :)

Please refer here for a rationale of the previous, current and future solution. Please note, Remote Config is deprecated and cannot be used when deploying v2 functions.

firebase/firebase-functions#1084

@yuchenshi yuchenshi reopened this Sep 19, 2022
@michaelalhilly
Copy link

Thanks for reopening this. It would be great if this could work as expected.

@edonosotti
Copy link

edonosotti commented Sep 29, 2022

IMPORTANT NOTE

Please verify that you did not set any variable using a reserved name.

If you do that, the emulator will just fail silently upon loading the functions and not load your .env[.*] files.

Some of those reserved names could be "popular", such as "All keys starting with FIREBASE_".

@Wian-TMC
Copy link

firebase/firebase-functions#1084

You're missing the point of the issue at hand. Please refer to firebase/firebase-functions#1084

@irux
Copy link

irux commented Oct 15, 2022

any news on this ?

@Joebayld
Copy link

Same issue here - how are we supposed to migrate to functions V2 if we can't use .env files in the root?

@bruceharrison1984
Copy link

bruceharrison1984 commented Dec 3, 2022

Just ran into this. Very painful having to deploy my functions just to see if they work, despite having a perfectly good local emulator.

You can kind of hack around it by bringing in dotenv and loading the files if NODE_ENV === development. I actually was doing this first, then read the docs so I removed it, only to find it was broken.

EDIT
Turns out I am an idiot and had a FIREBASE_ setting in my .env file. Removing it fixed the issue, though I still don't get the console message informing me that the env files have been loaded.

@luicfrr
Copy link

luicfrr commented Dec 19, 2022

EDIT Turns out I am an idiot and had a FIREBASE_ setting in my .env file. Removing it fixed the issue, though I still don't get the console message informing me that the env files have been loaded.

If you're having problem with process.env in firebase functions this should help

@myktra
Copy link

myktra commented Dec 19, 2022

I had this same problem - removed a FIREBASE_xxx variable from my .env file and the variables loaded. Unfortunate an exception isn't thrown when the documented policy is unknowingly violated. Variables from the .env file are loaded at runtime, so if you attempt to call your function from the shell for example, you should see the following in the console:

firebase > myFunction({ test: '123' })
Sent request to function.
firebase > i  functions: Loaded environment variables from .env.

@knownasilya
Copy link

Unsure if related, but firebase functions:shell doesn't load .env.local file. It says it loads .env, and the values that are present on process.env confirm it.

@blidd-google
Copy link
Contributor

Hi all, thanks for helping us and others diagnose issues with loading environment variables in the functions emulator. Two things:

  1. Parameterized configuration is now the recommended way to configure your environment variables. Please refer to the docs here for instructions on how to use params. Check out the thread here for context about the params feature (thanks to @Wian-TMC for sharing).
  2. I will be closing this issue, as the original error has been resolved in Load project-specific environment variables in the Functions Emulator #4273. If you are still encountering issues with configuring your environment, please open a new issue and provide instructions on how to reproduce your error.

@ekalosak
Copy link

ekalosak commented Sep 28, 2023

This is still broken @blidd-google , please consider re-opening.
The .env.local method for overriding environment variables on a development workstation is broken.

Details: #6499

@Stf-F
Copy link

Stf-F commented Oct 10, 2023

Currently facing this issue as well and I believe that this started failing after the creation of several aliases. None of my .env files are being read in the emulator, not even in the body of the function. I worked around the issue by installing the dotenv package. Not ideal but couldn't waste anymore time looking into this.

@conor909
Copy link

At the moment my project alias named env files are not being used, I have .env.dev & .env.prod, but at the moment I need to copy the contents into a .env file before deploying to dev or prod

@a7md0
Copy link

a7md0 commented Jan 6, 2024

Same issue here, the deployment is failing due to the code requiring an environment variable to exists. The variable is already defined in the .env and no reserved name is being used in the file. Still the deployment does not print the below line (which is documented in Firebase)

$ firebase deploy --only functions
# ...
# i functions: Loaded environment variables from .env.
# ...

Using "firebase-functions": "^4.6.0" & "firebase-tools": "^13.0.2"

@mustafaekim
Copy link

Hi, I need to setup a custom environmental variable so that a private npm package that we include into the firebase/functions can configure itself accordingly however it seems like there is no single way for configuring environmental variables in gen2?

@rzfang
Copy link

rzfang commented May 2, 2024

Hi, here
Not sure if this can help. I don't run emulator, I run deploy directly, and what I found is working for me.

Move the .env* files out of functions directly to the root of your directory, and everything works.

In my case, I have 3 .env files: .env, .env.beta, .env.prod
with the command firebase use beta && ... && firebase deploy ..., the log will show the line:

i  functions: Loaded environment variables from .env, .env.beta.

instead of

i  functions: Loaded environment variables from .env.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.