Minimal example to investigate dotenvy #74
- crate a uses dotenvy_macro. It reproduces the error.
- crate b uses dotenvy. It shows a solution.
The problem is that the user expects the .env inside the crate folder to be read. Instead, the workspace .env is being read.
dotenvy::dotenv
and the dotenvy!
macro both read from std::env::current_dir
.
If the user runs cargo run --bin b
from the workspace root, then the current dir is the workspace root.
To get the crate dir, we can use CARGO_MANIFEST_DIR
and dotenvy::from_path
.
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")?;
dotenvy::from_path(format!("{manifest_dir}/.env"))?;
No, because not all users use Cargo. Some just use the rustc
compiler.
However, an example will be added to the repo.