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

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

Open
lanesawyer opened this issue Jul 18, 2021 · 3 comments

Comments

@lanesawyer
Copy link

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
Copy link

dbstratta commented Aug 4, 2021

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
Copy link

mathroc commented Dec 6, 2021

@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
Copy link
Author

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.

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