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

Make automatic .env load an opt-in feature rather than automatic #880

Closed
felipecrs opened this issue Jan 4, 2022 · 19 comments
Closed

Make automatic .env load an opt-in feature rather than automatic #880

felipecrs opened this issue Jan 4, 2022 · 19 comments
Labels

Comments

@felipecrs
Copy link
Contributor

felipecrs commented Jan 4, 2022

Is your feature request related to a problem? Please describe.

So, as the conversation goes on in #845, it turns out to be very obvious that #845 was a breaking change which didn't receive a treatment of such (no major version bump, for example).

Describe the solution you'd like

So, in order to keep 2.x version tree clean, I would suggest we make the automatic .env load an opt-in feature, rather than on by default.

The user could optionally opt-in by setting a global configuration for direnv (which I'm not sure how that could be achieved, perhaps ~/.direnvrc?

Describe alternatives you've considered

We could simply bump direnv version to 3, but... in fact I think it would be a very welcome addition to let users decide whether they want to use this feature (which is super useful for me) or not (which seems to be breaking some users cases).

Additional context

None.

@hacfi
Copy link

hacfi commented Jan 4, 2022

Thanks @felipecrs .. that sounds great!!

Maybe instead of a global configuration file it would be easier to introduce an environment variable that determines whether .env is used. Could be DIRENV_DOTENV_ENABLE=1 (opt-in) for 2.x and then DIRENV_DOTENV_DISABLE=1 (opt-out) in 3.x?

@piotrb
Copy link

piotrb commented Jan 4, 2022

yea this jumped out as a surprise for me .. I use other tools for working with .env files .. I'd rather not have something as low level as direnv automatically do this .. there is multiple problems with this .. since other tools .. like the dotenv gem for ruby has an override order, and env that comes from "outside" is considered HIGHEST .. so it will lead to incorrect behavior ..

@zimbatm
Copy link
Member

zimbatm commented Jan 4, 2022

Sounds good. I already merged #878 that makes it opt-out, but having it opt-in might be better to keep back-compat.

@phongnh
Copy link

phongnh commented Jan 5, 2022

Thanks for raising this issue @felipecrs . I have similar issue with the newest version. In my case, I use shared .envrc for repos under Github organization, something as follows. In each repo, we have Docker setup, therefor there is a .env file in each repo. When I cd to repo1, it is no longer load .envrc in parent folder. Need some way to ignore loading .env automatically.

/Users/phong/projects/org/
├── repo1
├── repo2
├── repo3
└── .envrc

@glensc
Copy link
Contributor

glensc commented Jan 5, 2022

maybe a better approach of this is to add syntactic sugar to .envrc that loads .env if present:

# the .envrc file
load_dotenv

@felipecrs
Copy link
Contributor Author

maybe a better approach of this is to add syntactic sugar to .envrc that loads .env if present:

# the .envrc file
load_dotenv

This already exist :)

@zimbatm
Copy link
Member

zimbatm commented Jan 5, 2022

This already exist :)

See https://direnv.net/man/direnv-stdlib.1.html#codedotenv-ltdotenvpathgtcode

Alright, so I decided to release v2.30.3 where you can set skip_dotenv = true.

@zimbatm zimbatm closed this as completed Jan 5, 2022
@Jayfrown
Copy link

Jayfrown commented Jan 9, 2022

@zimbatm so where can I set skip_dotenv = true to stop direnv from loading .env files everywhere?

edit: found it

❯ cat ~/.config/direnv/direnv.toml
[global]
skip_dotenv = true

@mrmachine
Copy link

Please make a new release where an opt-in is required for .env handling. I just brew upgraded direnv and suddenly direnv wants to load all my .env files. Great to see this behaviour will be opt-in, but I am currently on the latest version so I'll have to downgrade and wait.

@Jayfrown
Copy link

@mrmachine you can opt-out globally so you don't have to downgrade. See my comment before yours.

@henrik242
Copy link

@Jayfrown @zimbatm Is it possible to put skip_dotenv = true in the .envrc? Or somewhere else in a project folder that I can commit? (not in $HOME/.config)

@phongnh
Copy link

phongnh commented Mar 11, 2022

@Jayfrown @zimbatm Is it possible to put skip_dotenv = true in the .envrc? Or somewhere else in a project folder that I can commit? (not in $HOME/.config)

You can set in ~/.config/direnv/direnv.toml as follows:

[global]
skip_dotenv = true

For projects that you need to load .env, just put dotenv in .envrc.

@henrik242
Copy link

As I said, not in the ~/. config folder. I need it to be set in my project folder (git repo), so that all users of that repo doesn't load .env files.

@felipecrs
Copy link
Contributor Author

@Jayfrown @zimbatm Is it possible to put skip_dotenv = true in the .envrc? Or somewhere else in a project folder that I can commit? (not in $HOME/.config)

If you have a .envrc, direnv won't read .env.

@henrik242
Copy link

If you have a .envrc, direnv won't read .env.

True, but direnv will try to read .env files in subfolders in the project. We're using .env for configuring docker-compose in various subfolders of the project, and don't want to have to do a direnv allow (or deny) for all of them.

@felipecrs
Copy link
Contributor Author

Got it. In this case we either make .env load not enabled by default, or we allow .direnvrc to be defined in the project root so that one can put skip_dotenv = true in it.

The first was already extensively discussed, and it was decided against it. So, I suggest you open a new issue for the second approach.

@henrik242
Copy link

OK, will do. Thanks for spending time on this!

@henrik242
Copy link

@felipecrs Done: #908

@henrik242
Copy link

henrik242 commented Mar 14, 2022

By the way, there's no DIRENV_DOTENV_DISABLE variable available, right? That automatic .env include is really breaking things for us. We have both docker-compose and nextjs in subprojects in our project, and they use .env files to load variables. Variables in the root project .envrc are now removed because of these sub folder .env files. (We don't control users' ~/.config/direnv/direnv.toml files, so that's not an option for us.)

🤞🏻 for #908

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

No branches or pull requests

9 participants