trigger.dev icon indicating copy to clipboard operation
trigger.dev copied to clipboard

bug: .env.local doesn't override .env values in dev command

Open isshaddad opened this issue 9 months ago • 3 comments

Provide environment information

v4.3.2

Describe the bug

When running trigger dev, .env.local should override values from .env, but it doesn't. The .env values are used instead

Reproduction repo

n/a

To reproduce

To reproduce:

  1. Create .env with TEST_VAR=from-env
  2. Create .env.local with TEST_VAR=from-local
  3. Run npx trigger.dev dev
  4. In your task, check process.env.TEST_VAR
  5. It shows from-env instead of from-local

Expected: .env.local should override .env, so TEST_VAR should be from-local.

Additional information

The issue is most probably in packages/cli-v3/src/utilities/dotEnv.ts. The code passes an array to dotenv.config({ path: envFilePath }), but dotenv.config() only accepts a string path, not an array. This means only the first file (.env) is loaded.

Potential fix: Loop through each file in ENVVAR_FILES and call dotenv.config() for each one in order, so later files override earlier ones

isshaddad avatar Feb 04 '26 17:02 isshaddad

I’d like to work on this issue. I believe .env.local should take precedence over .env in the dev command. I’ve identified where the env loading order happens.

KanhaiyaPandey avatar Feb 23 '26 09:02 KanhaiyaPandey

@isshaddad I'm running into this issue locally as well. I've applied the patch that @deepshekhardas authored in #3157 and it seems to resolve the ordering problem. I can kick off the vouch process for myself if that's the fastest path to resolution, but I see that you're already a collaborator. Would it be quicker for you to put that commit up for PR?

eatspaint avatar Apr 17 '26 17:04 eatspaint

Ran into this on 4.5.0 (still repros identically on 4.5.9). Wanted to flag that the root-cause diagnosis in the issue is slightly off, which may be why #3157 stalled.

dotenv does support array paths — the issue isn't that only .env loads. dotenv 17.x accepts path: string[] and loads all of them (their README: "Pass in multiple files as an array, and they will be parsed in order and combined... The first value set for a variable will win, unless the options.override flag is set"). So all five ENVVAR_FILES do load — the bug is purely precedence.

The real cause is in resolveDotEnvVars (utilities/dotEnv.ts):

dotenv.config({
    processEnv: result,
    path: envFilePath,   // array of all ENVVAR_FILES
    // no `override` -> first-wins -> .env beats .env.local
});

Because override is not set, dotenv keeps the first value it sees, and .env is first in ENVVAR_FILES. So .env wins over .env.local, which is the reported symptom.

One-line fix, matching the documented intent ("later files override"):

dotenv.config({ processEnv: result, path: envFilePath, override: true });

There's a second, compounding surprise worth being aware of while touching this code: resolveLocalEnvVars (utilities/localEnvVars.ts) spreads the file vars last over process.env:

return {
    ...sanitizeEnvVars(processEnv),
    ...sanitizeEnvVars(additionalVariables ?? {}),
    ...sanitizeEnvVars(dotEnvVars),   // files override injected env
};

so file values also override anything injected via the shell / a wrapper. Combined with the first-wins ordering, a stray root .env outranks both the more-specific files and injected env. Adding override: true fixes the file-vs-file ordering; the file-vs-process.env inversion is a separate design question, but worth a comment either way.

Happy to open a PR with the one-liner + a test if that's useful.

petrbela avatar Jul 30 '26 10:07 petrbela