You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I commonly work in a workspace with multiple binary crates that each have their own .env file. However it seems the dotenv!() macro is looking for the .env file in the workspace root instead of the crate root dir.
Any idea why this would be happening?
The text was updated successfully, but these errors were encountered:
stevepryde
changed the title
The dotenvy! macro looks for the .env in the workspace root dir instead of crate root dir
The dotenv! macro looks for the .env in the workspace root dir instead of crate root dir
May 10, 2023
The issue is that dotenvy::dotenv and the dotenvy! macro both start finding from std::env::current_dir.
If the user runs from the workspace root, then the current dir is the workspace root.
So your crate .env would only get picked up if you run from the crate root.
One solution is to use CARGO_MANIFEST_DIR to get the crate root.
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")?;
dotenvy::from_path(format!("{manifest_dir}/.env"))?;
std::env::var("FOO")?;
Of course, this loads at runtime instead of compile time. Let me know if that is acceptable for you.
I was hoping to use the dotenv!() macro to have the best of both worlds between env!() and having a .env file and I don't think this solution works for that. Any chance of a toml file or some way to provide the path at compile time?
Has anyone found a reasonable solution to this problem? In my case the .env file needs to be in the parent directory as other language projects will use it.
I commonly work in a workspace with multiple binary crates that each have their own .env file. However it seems the
dotenv!()
macro is looking for the .env file in the workspace root instead of the crate root dir.Any idea why this would be happening?
The text was updated successfully, but these errors were encountered: