Skip to content

Conversation

@zimirza
Copy link

@zimirza zimirza commented May 9, 2025

What does this PR do?

This pull request fixes an issue that when running bun test, .env.local was not read for the environment variables. This pull request closes #19542.

How did you verify your code works?

  • I checked the lifetime of memory allocated to verify it's (1) freed and (2) only freed when it should be
  • I included a test for the new code, or an existing test covers it
  • JSValue used outside of the stack is either wrapped in a JSC.Strong or is JSValueProtect'ed
  • I updated TypeScript/JavaScript tests and they pass locally (test/cli/run/env.test.ts)

@zimirza
Copy link
Author

zimirza commented May 9, 2025

I am not sure if this is the correct approach, that .env.local is read last of the local environment variables files.

@jakswa
Copy link

jakswa commented May 24, 2025

I think i just ran into this too -- confusing env var loading in my tests. You go to make sure you understand the basic precedence outlined in the bun docs and you get confusing behavior right now.

@zimirza
Copy link
Author

zimirza commented May 30, 2025

I have updated such that .env.local is read last for tests. However, one test does not really work:

  test(".env.local overrides .env.{NODE_ENV}", () => {
    const dir = tempDirWithFiles("dotenv", {
      ".env": "FOO=.env\n",
      ".env.development": "FOO=.env.development\n",
      ".env.production": "FOO=.env.production\n",
      ".env.test": "FOO=.env.test\n",
      ".env.local": "FOO=.env.local\n",
      "index.ts": "console.log(process.env.FOO);",
      "index.test.ts": "console.log(process.env.FOO);",
    });
    const { stdout: stdout_dev } = bunRun(`${dir}/index.ts`, { NODE_ENV: "development" });
    expect(stdout_dev).toBe(".env.local");
    const { stdout: stdout_prod } = bunRun(`${dir}/index.ts`, { NODE_ENV: "production" });
    expect(stdout_prod).toBe(".env.local");
    const { stdout: stdout_test } = bunTest(`${dir}/index.test.ts`, {});
    expect(stdout_test).toBe(`bun test ${Bun.version_with_sha}\n` + ".env.local");
  });

The last test should be .env.local, and returns with .env.test.

@zimirza
Copy link
Author

zimirza commented May 30, 2025

I think i fixed it for tests to use .env.local. Tests are working now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bun test will not load environment variables from .env.local

2 participants