dotenv-rs / dotenv

Library to help supply environment variables in testing and development
MIT License
566 stars 45 forks source link

dotenv_codegen only works with a .env file at the root when using workspaces #68

Open lanesawyer opened 3 years ago

lanesawyer commented 3 years ago

Hi 👋🏻,

I'm working with a project using a Cargo workspace and am trying to set it up to use a .env file for each project in the workspace instead of a single .env file at the root of the repository.

Unfortunately, the dotenv_codegen macro is failing when the .env file is not at the root of the workspace.

This seems at odds with the dotenv code and documentation, which says the library "will load environment variables from a file named .env in the current directory or any of its parents".

I started a new project with a workspace holding a single project whose only job is to print the environment variable embedded with the dotenv_codegen macro and was able to reproduce the issue, so I'm fairly certain this is a problem with the macro and not with anything in my existing project.

dbstratta commented 3 years ago

Happening the same issue in my setup but with the non macro version. I have multiple projects in a workspace each with a different .env, but dotenv::dotenv() tries to load the (non-existing) one at the workspace root.

As a temporal solution I changed

dotenv::dotenv().ok();

to

dotenv::from_filename("./my_project/.env").ok();
mathroc commented 2 years ago

@lanesawyer how are you started your app ? if you're running cargo run ... from the root of the projet, then it normal for dotenv to check for a .env file there and not at the crate level. the directory where you run the command starting your app is the "current directory"

lanesawyer commented 2 years ago

I start it different ways depending on what I'm working on.

It's a front-end and a web server, each of which has a ..env file. Things work fine when I run each individually, meaning I'm in the ui or server folder, but fails when I run the entire workspace at the root of the repository.

I believe I got around the issue with the suggestion of the other commenter on this thread, but it has been a few months since I've worked on that project so I'm not entirely sure and I don't currently have access to that project to check.