foundry-rs / foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
https://getfoundry.sh
Apache License 2.0
8.16k stars 1.69k forks source link

Look for .env files in the parents of cwd, up to root of current git repo #3711

Open adhusson opened 1 year ago

adhusson commented 1 year ago

Component

Forge

Describe the feature you would like

Feature description

Mimic the default behavior of the --root CLI argument for loading a .env file and consider up to the root directory of the current git repository in addition to cwd and project root.

Context

Since #2604, the .env file is only read from 2 places (cwd & project root).

When a foundry project is used as a dependency, invocations that need to use a local .env file will do e.g. forge script --root path/to/dependency. But the .env file may not be in the cwd! It may be located in the root directory of the current git repository.

Example:

.
|- .env
|- .git
|- dir
   |- script.js # invokes forge script
./dir$ script.js # .env is one level above

Here, looking one level above would load the current repo's .env file.

Additional context

No response

mattsse commented 1 year ago

this is implemented here, which loads from cwd + (dir(foundry.toml) | gitroot))

https://github.com/foundry-rs/foundry/blob/32200ac58f579f8bf2fa022a2b45fc3c5a13297b/cli/src/utils.rs#L178-L187

perhaps this should do dir(foundry.tml) + gitroot instead?

adhusson commented 1 year ago

Thank you for pointing me to the code, I was mistaken about what forge does.

For my use case, the current behaviour is enough (the important part is cwd + gitroot). It may be good to do cwd + dir(foundry.toml) + gitroot anyway though -- I can imagine needing that in the future.

Piyushsahu99 commented 1 day ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

I have experience with JavaScript and Python, along with a solid understanding of Solidity. My background in open-source projects equips me to effectively implement features and contribute to documentation. I can leverage my skills to enhance the Foundry toolset, ensuring clear communication and functionality for developers.

How I plan on tackling this issue

To approach the problem, I would first locate the root directory of the current git repository by checking for the presence of the .git folder. Then, I would traverse the parent directories starting from the current working directory to search for a .env file up to the git root. If found, I would load its contents into the environment. Additionally, I would write tests to ensure that this functionality works as expected and update the documentation to clarify the new behavior. This implementation would enhance the Forge tool's ability to load .env files in various project structures.