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

Reload .env after updates fail #15

Open
vbakke opened this issue Apr 29, 2024 · 5 comments
Open

Reload .env after updates fail #15

vbakke opened this issue Apr 29, 2024 · 5 comments

Comments

@vbakke
Copy link

vbakke commented Apr 29, 2024

Steps to reproduce:

  • Open Rstudio with a project using dotenv
  • Run the R script that uses library(dotenv)
  • Change a variable in the .env file
  • Rerun the script

The Sys.getenv("TEST_VAR") still reports the old value of TEST_VAR.

@vbakke
Copy link
Author

vbakke commented Apr 29, 2024

A workaround I've found is to manually run detach("package:dotenv", unload=TRUE) every time I update my .env file.

@timoruohomaki
Copy link

A workaround I've found is to manually run detach("package:dotenv", unload=TRUE) every time I update my .env file.

Thanks, that solved it for me as well. Fortunately there are not that many updates but still.

@gaborcsardi
Copy link
Owner

Maybe dotenv could unload itself, so the repeated library() call would load it again, so it could load the updated env vars.

@gaborcsardi
Copy link
Owner

Maybe dotenv could unload itself, so the repeated library() call would load it again, so it could load the updated env vars.

This is actually pretty difficult, so I won't do it.

However, a good workaround is to use

dotenv::load_dot_env()

in your script. Or, if you want to condition it on the existence of .env, then:

if (file.exists(".env")) dotenv::load_dot_env()

With this, the script will re-load .env every time it runs.

@vbakke
Copy link
Author

vbakke commented Nov 9, 2024

Yes, running dotenv::load_dot_env() explicitley is waht I ended up doing as well.

If unloading is difficult, /which I presume is it, I recommend changing the 'Usage' in the README to:

library(dotenv)
dotenv::load_dot_env()

Then I think this issue is solved. :)

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

3 participants