Skip to content

allan2/dotenvy-workspace-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dotenvy-workspace-example

Minimal example to investigate dotenvy #74

  • crate a uses dotenvy_macro. It reproduces the error.
  • crate b uses dotenvy. It shows a solution.

Issue

The problem is that the user expects the .env inside the crate folder to be read. Instead, the workspace .env is being read.

Diagnosis

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.

Solution

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"))?;

Will this be adopted?

No, because not all users use Cargo. Some just use the rustc compiler.

However, an example will be added to the repo.

About

Env loading in workspaces

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages