Repository: promptfoo/promptfoo
Issue: Automatically load .env files, searching up in folder hierarchy
Root cause: Unimplemented Feature Gap

**Is your feature request related to a problem? Please describe.**
When using providers, we need to set env variables to be exported in our env to make them work.
In a local dev environment, it's easy to forget to export theses vars in the current shell, and setting them globally is not a good idea as it can leak or conflicts with other projects.

**Describe the solution you'd like**
I'd to be able to create a `.env` file, either in the current dir or at the root of my project and have promptfoo automatically find it and load it.

~~*Bonus:* In addition, a `--env <file>` option so you directly specify which env to load so you can have `.env.dev`, `.env.prod` etc and pick up the one you need.~~

**Describe alternatives you've considered**
Using `dotenvx` before running promptfoo. While it works, it makes the process more complicated, and it become tedious to run the promptfoo CLI.

**Additional context**
There are existing NPM packages that cam help implementing this feature:
- Load env file: https://github.com/motdotla/dotenv
- Find `.env` file up in the folder structure: https://www.npmjs.com/package/find-up


Both of these are already supported!  promptfoo will pick up `.env` automatically, and most commands have an `--env-path` option that lets you set a specific environment file.

That's good to know, though it never worked for me as the `.env` files are located in the project root, while all the tests are in  `test/*` subfolders, that's also which I suggested to search up the folder hierarchy.
If env file loading is already there, then adding the "search up" logic should be quick (if accepted) 🙂 

Hi @sinedied, I recently merged [PR #1654](https://github.com/promptfoo/promptfoo/pull/1654), which normalizes the flags—you can now use both --env-file and --env-path interchangeably in the next release.

Regarding automatically searching up the folder hierarchy for .env files, that's a great idea. As @typpo said, currently, we load .env files from the current working directory. Since the dotenv library supports specifying multiple paths and searching parent directories (as per [dotenv's documentation](https://github.com/motdotla/dotenv#path)), we could enhance this functionality.

We'd be happy to welcome a PR implementing this feature. Thanks again for your suggestions and for trying promptfoo. 

My 2c: I think walking parent directories in search of a .env may have
unintended consequences for some users. I wonder if a better solution could
be to load a .env from the promptfoo global dir (~/.promptfoo) if present?
That way you can have envars for all your configs without having to pass an
env path each time.

On Thu, Sep 12, 2024, 7:54 PM Michael ***@***.***> wrote:

> Hi @sinedied <https://github.com/sinedied>, I recently merged PR #1654
> <https://github.com/promptfoo/promptfoo/pull/1654>, which normalizes the
> flags—you can now use both --env-file and --env-path interchangeably in the
> next release.
>
> Regarding automatically searching up the folder hierarchy for .env files,
> that's a great idea. As @typpo <https://github.com/typpo> said,
> currently, we load .env files from the current working directory. Since the
> dotenv library supports specifying multiple paths and searching parent
> directories (as per dotenv's documentation
> <https://github.com/motdotla/dotenv#path>), we could enhance this
> functionality.
>
> We'd be happy to welcome a PR implementing this feature. Thanks again for
> your suggestions and for trying promptfoo.
>
> —
> Reply to this email directly, view it on GitHub
> <https://github.com/promptfoo/promptfoo/issues/1623#issuecomment-2347939711>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AACLYJQGXMQPU455KZ4LCL3ZWJHXPAVCNFSM6AAAAABN6CUL5CVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNBXHEZTSNZRGE>
> .
> You are receiving this because you were mentioned.Message ID:
> ***@***.***>
>


We have a usage where we would like to be able to provide an array of env vars to the promptfoo command. This pattern is very common in nextjs. For example, `.env` and `.env.local`. This does not seem possible with promptfoo currently, but it seems feasible with `dotenv`.