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

Allow loading environment variables from given path #5723

Open
altugbakan opened this issue Aug 25, 2023 · 7 comments
Open

Allow loading environment variables from given path #5723

altugbakan opened this issue Aug 25, 2023 · 7 comments
Labels
A-config Area: config C-anvil Command: anvil C-cast Command: cast C-forge Command: forge T-feature Type: feature

Comments

@altugbakan
Copy link

altugbakan commented Aug 25, 2023

Component

Forge, Cast, Chisel

Describe the feature you would like

Now, the .env file is only loaded from the project root. A CLI flag can be added to load the .env file from another directory. This will be helpful if Foundry is used within a monorepo that uses a single .env file.

The lines that load the .env file can be seen here:

/// Loads a dotenv file, from the cwd and the project root, ignoring potential failure.
///
/// We could use `warn!` here, but that would imply that the dotenv file can't configure
/// the logging behavior of Foundry.
///
/// Similarly, we could just use `eprintln!`, but colors are off limits otherwise dotenv is implied
/// to not be able to configure the colors. It would also mess up the JSON output.
pub fn load_dotenv() {
let load = |p: &Path| {
dotenvy::from_path(p.join(".env")).ok();
};
// we only want the .env file of the cwd and project root
// `find_project_root_path` calls `current_dir` internally so both paths are either both `Ok` or
// both `Err`
if let (Ok(cwd), Ok(prj_root)) = (std::env::current_dir(), find_project_root_path(None)) {
load(&prj_root);
if cwd != prj_root {
// prj root and cwd can be identical
load(&cwd);
}
};
}

I would also like to work on this.

Additional context

The code can be seen below:

pub fn load_dotenv() {
    let load = |p: &Path| {
        dotenvy::from_path(p.join(".env")).ok();
    };

    // we only want the .env file of the cwd and project root
    // `find_project_root_path` calls `current_dir` internally so both paths are either both `Ok` or
    // both `Err`
    if let (Ok(cwd), Ok(prj_root)) = (std::env::current_dir(), find_project_root_path(None)) {
        load(&prj_root);
        if cwd != prj_root {
            // prj root and cwd can be identical
            load(&cwd);
        }
    };
}
@altugbakan altugbakan added the T-feature Type: feature label Aug 25, 2023
@altugbakan altugbakan changed the title Allow loading environment variables from any path Allow loading environment variables from given path Aug 25, 2023
@pikonha
Copy link

pikonha commented Feb 26, 2024

I'm interested in this solution because I have a monorepo structure with the .env file placed in the root folder. Is there any workaround until this is released?

@zerosnacks zerosnacks added C-forge Command: forge C-anvil Command: anvil C-cast Command: cast A-config Area: config labels Jul 3, 2024
@zerosnacks
Copy link
Member

Hi @altugbakan thanks for your suggestion! Would you still be willing to work on this?

@altugbakan
Copy link
Author

Hey, I would like to!

@zerosnacks
Copy link
Member

Awesome! Assigning to you

@altugbakan
Copy link
Author

Opened #8346 for this

@altugbakan altugbakan removed their assignment Jul 10, 2024
@altugbakan
Copy link
Author

Hey, I tried my hand, but don’t currently have the capacity to work on this. Anyone who wants to solve this can check this out.

@zerosnacks
Copy link
Member

No problem @altugbakan, thanks for your time and effort

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-config Area: config C-anvil Command: anvil C-cast Command: cast C-forge Command: forge T-feature Type: feature
Projects
No open projects
Status: Todo
Development

No branches or pull requests

3 participants