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

Allow loading multiple dotenv files #1748

Open
evandam opened this issue Nov 27, 2023 · 4 comments
Open

Allow loading multiple dotenv files #1748

evandam opened this issue Nov 27, 2023 · 4 comments

Comments

@evandam
Copy link

evandam commented Nov 27, 2023

Related: #945 (comment)

It would be great to be able to source multiple dotenv files in a "cascading" manner, similar to https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use. This allows per-environment overrides, and a .env to provide defaults to fall back on (meaning .env.test, .env.qa, .env.prod don't need to repeat the same common env vars that can be defined once in .env).

I think there may be some related work that would need to be done to be able to interpolate filenames in dotenv-files, though.

ENV := "prod"
set dotenv-files := [".env.{{ENV}}", ".env"]

And call with just ENV=prod to load env vars from .env.prod, and then .env (assuming dotenv files don't overwrite existing environment variables).

As far as behavior goes, it probably makes sense to fail silently (or log it) if a file in the array isn't found since they can just be "candidates" for dotenv files to load. For example, .env.local may not always be present in an environment, but we'll try to load it if it does happen to exist.

@azriel49
Copy link

it would be super useful to be able to override the default values ​​of the .env using a .env.local !!
Like the OP, i usually put my .env into the repository with default values, and override some confs with an environment specific .env.local

@r3nor
Copy link

r3nor commented Feb 22, 2024

This would be great, many project use multiple dotenvs and I have to use bash scripts every time

@sbeckeriv
Copy link

sbeckeriv commented Mar 6, 2024

how about allowing more then on dotenv-filename config?

just --dotenv-filename=.env --dotenv-filename=.env.local command

and

      ENV := "prod"
      set dotenv-load
      set dotenv-filename := ".env"
      set dotenv-filename := ".env.local"
      set dotenv-filename := ".env.{{ENV}}"
      set dotenv-path := "path"

if they can be evaluated in order, last one wins. I was reading the code and I think updating the config to allow an vec of filenames and then modifying the current dotenv load process might be a smaller change. The code path would always be used making it less likely to have diverging behaviors. one could even skip supporting the command line part and make it a set config only.

[edit]
there are a number of things that would need to be cleaned up, however, this is a diff of it working with arguments and settings. I would want to know if this was an acceptable way forward before spending more time on it.

https://gist.github.com/sbeckeriv/664c3dd8a38c84c877a45f50d44a0f20

While making this diff I learned that [] syntax is not supported currently. I believe there is other issues around supporting 'arrays' of values.

@nk9
Copy link
Contributor

nk9 commented Apr 28, 2024

If we're going to allow multiple dotenv files, it seems like requiring them all to be in a single dotenv-path is an unnecessary constraint. I would agree with OP that we should have a new setting called dotenv-files which would take an array of paths including the filename, e.g.:

set dotenv-files := [".env", "src/dir1/.env2"]

Question, though: how should this setting work with fallback justfiles?

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

No branches or pull requests

5 participants