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

Add automatic .env load #845

Merged
merged 5 commits into from
Dec 23, 2021
Merged

Add automatic .env load #845

merged 5 commits into from
Dec 23, 2021

Conversation

felipecrs
Copy link
Contributor

This PR is a continuation of #438, made by @aude.

@felipecrs
Copy link
Contributor Author

This should be ready to review.

felipecrs added a commit to felipecrs/dotfiles that referenced this pull request Nov 26, 2021
Mainly because it has a better compatibility with P10k. For now, it does
not read `.env` files automatically, but will probably do in the future:

direnv/direnv#845
@Kaligule
Copy link

I am not a maintainer but I read throught the changes (especially the tests) and it looks very reasonable to me.

@zimbatm
Copy link
Member

zimbatm commented Dec 23, 2021

Thanks for your PR. I should have left a note before; before I can merge this, I want to give it a try and see how it feels.

@zimbatm zimbatm merged commit 6e78b90 into direnv:master Dec 23, 2021
@felipecrs felipecrs deleted the auto-dotenv branch December 23, 2021 21:37
@felipecrs
Copy link
Contributor Author

felipecrs commented Dec 23, 2021

Whoa, what a good day to live! Thank you!

felipecrs added a commit to felipecrs/dotfiles that referenced this pull request Dec 23, 2021
As now direnv/direnv#845 was merged and we
finally have first-class support for .env files. The move to
chezmoiexternal was due to 2 things: because I want the new version fast
and because I make it indenpendent of the OS, allowing it to be used on
devcontainers as well.
@hacfi
Copy link

hacfi commented Jan 1, 2022

As much as I like this addition I would like to disable this by default if there is a way to do so. Is there?

@felipecrs
Copy link
Contributor Author

You can disable it by creating an .envrc file right beside your .env file. Even if it's empty.

@awarrenlove
Copy link
Contributor

Can this be disabled without requiring a .envrc file? I'm now getting prompts to allow loading any .env files on my machine, even in places that are completely unrelated to anything I want running under direnv.

I tried creating an empty .envrc file in my user directory just to see if that would override it in all subdirectories, but it still wants to load any .env files in the current directory, which is understandable given the file search behavior, but it is in this case unfortunate.

@mfriedenhagen
Copy link

mfriedenhagen commented Jan 4, 2022

@felipecrs: but even if you create an empty .envrc and allow it, the variables defined in a parent directory are removed.

I use .envrc to import specific password-manager commands to expose a token on demand for accessing a gitlab instance with lab and suddenly parsing .env completely breaks this for me :-(

@felipecrs
Copy link
Contributor Author

@awarrenlove

Can this be disabled without requiring a .envrc file?

Not with the current implementation. But I also agree that it would be nice to be configurable.

I tried creating an empty .envrc file in my user directory just to see if that would override it in all subdirectories, but it still wants to load any .env files in the current directory, which is understandable given the file search behavior, but it is in this case unfortunate.

Indeed, this is the current behavior. That's why I said: You can disable it by creating an .envrc file right beside your .env file.

@mfriedenhagen

but even if you create an empty .envrc and allow it, the variables defined in a parent directory are removed.

Variables removed? That's a weird, I don't think I understood, but you may try to create an .envrc (empty or not) in every folder that has a .env which you don't want it to be loaded.

@awarrenlove
Copy link
Contributor

Indeed, this is the current behavior. That's why I said: You can disable it by creating an .envrc file right beside your .env file.

Yes, I understand that. However, that now requires me to litter my machine with empty .envrc files anywhere I have a .env file. And in fact, I have more .env files on my machine that I don't want direnv to load than those that I do (currently using an explicit dotenv_if_exists).

@felipecrs
Copy link
Contributor Author

Right, so let's focus on making it configurable then. I would suggest you to first open a new issue so it can be discussed with the maintainers.

@mfriedenhagen
Copy link

Hello @felipecrs,

  • I have a file .envrc like:
❯ cat ~/ghq/git.example.com/.envrc
LC_HOST=git.example.com
export LAB_CORE_HOST=https://$LC_HOST
export LAB_CORE_USER=theuser
export LAB_CORE_TOKEN_GENERATOR="security find-internet-password -s $LC_HOST -a $LAB_CORE_USER -g -w"
export GIT_AUTHOR_NAME="Name"
export GIT_AUTHOR_EMAIL=email@example.com
export GIT_COMMITTER_EMAIL=email@example.com

resp:

❯ cat ~/ghq/github.com/.envrc
export GITHUB_TOKEN_GENERATOR="security find-internet-password -s github.com -a mfriedenhagen -g -w"
export GIT_AUTHOR_NAME="Mirko Friedenhagen"
export GIT_AUTHOR_EMAIL=mfriedenhagen-at-...
export GIT_COMMITTER_EMAIL=mfriedenhagen-at-...
  • All projects from are checked out beneath these directory (using ghq BTW).
  • I may now use aforementioned lab tool or gh using aliases like:
    lab='env LAB_CORE_TOKEN=`eval $LAB_CORE_TOKEN_GENERATOR` VISUAL=vi /usr/local/bin/lab'

And I now have quite some projects where I created files named .env for other reasons (secrets for local docker-compose setups, e.g.) but would like to still use my settings for lab resp. gh.

@felipecrs
Copy link
Contributor Author

@mfriedenhagen if I understood well, all you have to do is to create an empty .envrc right beside your .env files, as I suggested first.

@mfriedenhagen
Copy link

Hi @felipecrs, that I tried firstly. I touched .envrc and allowed the empty file. Changing out of the project directory immediately exported e.g. LAB_CORE_HOST. But when I cd'd to the project directory with now both .env and .envrc again, the variable disappears from the environment as before.

@felipecrs
Copy link
Contributor Author

Wow, okay... that was not supposed to be how direnv works. Unless I'm wrong, direnv keeps collecting the environment for all the .envrc files up to your home, so the blank .envrc file should not make it stop doing that.

But I'm not as experienced in direnv as some other guys here, so... can someone confirm if what I said correct?

/cc @zimbatm

@lucaspiller
Copy link

I've found this feature has completely broken direnv for the way I use it. A few projects I work on have different components (in sub-folders, e.g. frontend and backend) that each need their own component-specific environmental variables.

I use direnv to populate project-wide settings that I need in the shell (e.g. AWS credentials) and also setup the development environment (e.g. initialising nvm).

Then I use .env files with dotenv (or something similar) to automatically load them in the development environment when the app is booted - but I don't care about accessing the contents of the .env files in the shell.

@felipecrs
Copy link
Contributor Author

Ok guys, I created #880. Would that address your concerns?

zimbatm added a commit that referenced this pull request Jan 4, 2022
The feature to automatically load .env files in #845 is nice, but it's a little too greedy in certain situations. Allow skipping that behavior by default (going back to explicit dotenv calls in .envrc files) in the configuration toml file.

Co-authored-by: zimbatm <zimbatm@zimbatm.com>
doctaphred added a commit to doctaphred/direnv that referenced this pull request May 4, 2022
Follow-up on direnv#845 and direnv#911: resolves direnv#916

This commit simply updates the error messages and documentation to more
accurately reflect the current opt-in configurable handling of `.env` files.
zimbatm pushed a commit that referenced this pull request May 11, 2022
Follow-up on #845 and #911: resolves #916

This commit simply updates the error messages and documentation to more
accurately reflect the current opt-in configurable handling of `.env` files.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants