motdotla/dotenv

Source Other env files?

jaidetree opened this issue · 3 comments

After searching I found a similar issue: #256

But I do have a more specific use case: multiple worktrees

Given my project directory is setup as follows:

.git
.env
main [main branch]
dev [jay/support-markdown branch]
pr-review [main branch]

main, dev, and pr-review are separate worktrees

Ideally I'd like it if I could create a .env file in each worktree with something like:

source ../.env

While the original issue mentioned using the api to load multiple files, that is not as ideal:

  1. I would have to make an upstream change to the way we use dotenv to always look for the outer .env file. This essentially means I'm baking how I work with git into how our application works which feels a little unfair to the team.
  2. Being able to require\include another file means I could solve this locally in my own .env file for each worktree.
  3. It could lend itself to establishing defaults and overrides:
NODE_ENV=development
source ../.env
PORT=3003

This lets me set the default NODE_ENV which the outer .env may or may not override while overwriting the PORT for each worktree so I can run them simultaneously.

Happy to discuss further if you think there's a better solution I'm not seeing, and I'm willing to take a stab if you think it's worth exploring.

i see what you are after. i personally don't recommend this. try to complete a worktree as soon as possible with .env changes and merge to main.

that said, you might find dotenvx more composable for your use case as it has built-in multi-environments.

https://github.com/dotenvx/dotenvx

Understandable. Thanks anyway

definitely give dotenvx a go. it has a beta mechanism that lets you commit your encrypted .env.vault with your code. this will essentially allow you to do what you want - tie secrets to a worktree.