Skip to content

Commit d488d86

Browse files
authored
fix: exclude .env.local files for test mode (#228)
1 parent 0c45b0b commit d488d86

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

.changeset/bright-parents-juggle.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
fix: exclude `.env.local` files for `test` mode
6+
7+
Aligns with the Next.js behavior of not extracting variables from the `.env.local` file in test environments.

packages/cloudflare/src/cli/build/utils/extract-project-env-vars.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ describe("extractProjectEnvVars", () => {
1313
mockFs({
1414
".env": "ENV_VAR=value",
1515
".env.local": "ENV_LOCAL_VAR=value",
16+
".env.test": "ENV_TEST_VAR=value",
17+
".env.test.local": "ENV_TEST_LOCAL_VAR=value",
1618
".env.development": "ENV_DEV_VAR=value",
1719
".env.development.local": "ENV_DEV_LOCAL_VAR=value",
1820
".env.production": "ENV_PROD_VAR=value",
@@ -67,4 +69,13 @@ describe("extractProjectEnvVars", () => {
6769
ENV_VAR: "value",
6870
});
6971
});
72+
73+
it("should exclude .env.local files when extracting test env vars", () => {
74+
const result = extractProjectEnvVars("test", options);
75+
expect(result).toEqual({
76+
ENV_TEST_LOCAL_VAR: "value",
77+
ENV_TEST_VAR: "value",
78+
ENV_VAR: "value",
79+
});
80+
});
7081
});

packages/cloudflare/src/cli/build/utils/extract-project-env-vars.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function readEnvFile(filePath: string) {
1717
*
1818
* Merged variables respect the following priority order.
1919
* 1. `.env.{mode}.local`
20-
* 2. `.env.local`
20+
* 2. `.env.local` (when mode is not equal to `test`)
2121
* 3. `.env.{mode}`
2222
* 4. `.env`
2323
*
@@ -27,7 +27,7 @@ function readEnvFile(filePath: string) {
2727
* the env files at the root of the monorepo.
2828
*/
2929
export function extractProjectEnvVars(mode: string, { monorepoRoot, appPath }: BuildOptions) {
30-
return [".env", `.env.${mode}`, ".env.local", `.env.${mode}.local`]
30+
return [".env", `.env.${mode}`, ...(mode !== "test" ? [".env.local"] : []), `.env.${mode}.local`]
3131
.flatMap((fileName) => [
3232
...(monorepoRoot !== appPath ? [readEnvFile(path.join(monorepoRoot, fileName))] : []),
3333
readEnvFile(path.join(appPath, fileName)),

0 commit comments

Comments
 (0)