Repository: promptfoo/promptfoo
Issue: `--env-file` support has regressed
Root cause: Configuration Error

**Describe the bug**
The `--env-file` option seems to have stopped working in the latest version of promptfoo's `eval` command.

**To Reproduce**
1. Create a `.env` file that contains `NODE_ENV=development`, as a default
1. Create a `.env.production` file or something of that nature, and set the `NODE_ENV=production`
2. Add a custom prompt function file so you can access the `process.env.NODE_ENV` and log it `(console.log(process.env.NODE_ENV))`
3. Run `npx promptfoo@latest --env-file=.env.production --verbose`
4. Observe the default `.env` file is loaded instead because `development` is logged instead of `production`

**Expected behavior**
I expect the specified env file to load through the `--env-file` option.

- Promptfoo version: 0.107.4

**Additional context**
I tried using npx to test prior versions quickly, but it looks like only verbose output works on the latest version when testing this way.

Using `npx promptfoo@latest eval --verbose` was the only version where any logging was available in the CLI output. Prior versions fail silently when you have a function that throws any error due to unauthorized access due to this env file issue.


I know for certain that the path is passed, because I see the feedback from the logger: https://github.com/promptfoo/promptfoo/blob/fdb2524753bb85a86dd34ff477727e87b64abefd/src/util/index.ts#L330

```
Loading environment variables from .env.production
```

I wonder if perhaps the pathing is very fincky.

Nope, I know for certain the path is correct, because `dotenv` under the hood will throw an error if the path doesn't exist:

```
promptfoo eval --env-file=.env.FAIL_DOESNT_EXIST
```

Results in:

```
.env.FAIL_DOESNT_EXIST: not found
```

And promptfoo fails immediately. So the problem is deeper, the env-file is definitely loaded initially but something afterwards is reverting the change.

Yup confirmed, I had a `.env` file in my repository. The `.env.production` file is loaded initially, but then it reverts to the `.env` file if present.

I don't think it's a bug in the underlying `dotenv` lib since it hasn't been updated since [2024-12-03](https://www.npmjs.com/package/dotenv?activeTab=versions).

So, somewhere in the `eval` command _after_ `setupEnv` is called, there's another process overriding the user's defined env-file.



Hey @ctrlaltdylan, thanks for flagging this. I’m not sure we ever explicitly supported custom env files based on the NODE_ENV. As a quick workaround, could you try explicitly passing `--env-file=.env.production` (with the .env. prefix included)? I’ll take a look at whether something changed unintentionally here or if we can clarify expected behavior.

@mldangelo thanks for getting back to me so quickly.

Just to clarify, I'm not expecting to control the current env-file based on a specific `NODE_ENV` variable. I just used that variable as an example of showing how the default `.env` file (containing `NODE_ENV=development` is _overwriting_ the `.env.production`'s `NODE_ENV`.

You can reproduce by placing a `.env` in your root, then defining a `--env-file` path to another file to `promptfoo eval`.

I updated my repro steps in the original post with the exact cause I found.

@ctrlaltdylan I think this will solve it: https://github.com/promptfoo/promptfoo/pull/3502 If you have a moment would you mind pulling the branch and verifying on your end?