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

Documentation on .env files and load_dotenv is still confusing #965

Open
sblask opened this issue Jul 7, 2022 · 5 comments
Open

Documentation on .env files and load_dotenv is still confusing #965

sblask opened this issue Jul 7, 2022 · 5 comments
Labels

Comments

@sblask
Copy link

sblask commented Jul 7, 2022

Describe the bug

I am new to direnv, so correct me if I am wrong, but from what I tried (using the latest binary on Mac) and if I understand it correctly, you can either have a .envrc OR an .env loaded automatically by direnv. For the latter to work you need to set load_dotenv to true. But the documentation makes it sound like both are loaded:

Before each prompt, direnv checks for the existence of a .envrc file (and [optionally](https://github.com/direnv/direnv/blob/master/man/direnv.toml.1.md#load_dotenv) a .env file) in the current and parent directories. If the file exists (and is authorized), it is loaded into a bash sub-shell and all exported variables are then captured by direnv and then made available to the current shell.

This should probably read:

... (or, [if enabled](https://github.com/direnv/direnv/blob/master/man/direnv.toml.1.md#load_dotenv), a .env file) ...

And this:

load_dotenv
Also look for and load .env files on top of the .envrc files. If both .envrc and .env files exist, the .envrc will always be chosen first.

should probably read:

Also look for and load .env files. If both .envrc and .env files exist, only the .envrc will be used.

This seems out of place:

Note that this functionality is not supported in .env files. If the coexistence of both is needed, one can use .envrc for leveraging stdlib and append dotenv at the end of it to instruct direnv to also read the .env file next.

It would probably be good to have an extra section just explaining the precedence of .envrc and .env and how things work when there is something in the current and in parent directories. I'd create a PR, but I am not sure how the latter works.

@sblask sblask added the Bug label Jul 7, 2022
@sblask
Copy link
Author

sblask commented Jul 7, 2022

I wrote "still" because I saw there was an issue before #941

@zimbatm
Copy link
Member

zimbatm commented Jul 11, 2022

The best is to write dotenv in the .envrc so the .envrc loads the .env file. And ignore the load_dotenv config option. It seemed like a good idea at the time, but it broke backward compatibility and is really adding more confusion than necessary.

@sblask
Copy link
Author

sblask commented Jul 18, 2022

@zimbatm Sure, but as long as the feature is there it should be documented properly, potentially saying don't do it?

@donhector
Copy link

I was also confused by the documentation.

If you have a .env in your directory with no accompanying .envrc, you need load_dotenv = true in your direnv.toml for direnv to auto-load the .env file.

If you have both .env and .envrc, direnv will ignore the .env despite having load_dotenv = true in direnv.toml. In this case, you need to explicitly add dotenv inside your .envrc for direnv to auto-load the .env file.

That's my own experience with version 2.32.2

@Ativerc
Copy link
Contributor

Ativerc commented May 10, 2024

Please add which version of direnv added support for load_dotenv.

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

4 participants