Skip to content

Commit db5caa9

Browse files
add support to Wrangler for setting the path to the .env files
- a global `--env-file` option for Wrangler commands - an option `envFile` for Wrangler API calls
1 parent 48427bd commit db5caa9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+868
-560
lines changed

packages/wrangler/e2e/dev.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,4 +2541,45 @@ describe(".env support in local dev", () => {
25412541
'"WRANGLER_ENV_VAR_1": "env-1"'
25422542
);
25432543
});
2544+
2545+
it("should load environment variables from the .env files pointed to by `--env-file`", async () => {
2546+
const helper = new WranglerE2ETestHelper();
2547+
await helper.seed(seedFiles);
2548+
await helper.seed({
2549+
".env": dedent`
2550+
WRANGLER_ENV_VAR_1=env-1
2551+
WRANGLER_ENV_VAR_2=env-2
2552+
`,
2553+
});
2554+
await helper.seed({
2555+
".env.local": dedent`
2556+
WRANGLER_ENV_VAR_2=local-2
2557+
WRANGLER_ENV_VAR_3=local-3
2558+
`,
2559+
});
2560+
await helper.seed({
2561+
"other/.env": dedent`
2562+
WRANGLER_ENV_VAR_1=other-env-1
2563+
WRANGLER_ENV_VAR_2=other-env-2
2564+
`,
2565+
});
2566+
await helper.seed({
2567+
"other/.env.local": dedent`
2568+
WRANGLER_ENV_VAR_3=other-local-3
2569+
WRANGLER_ENV_VAR_4=other-local-4
2570+
`,
2571+
});
2572+
2573+
const worker = helper.runLongLived("wrangler dev --env-file=other/.env");
2574+
const { url } = await worker.waitForReady();
2575+
expect(await (await fetch(url)).text()).toMatchInlineSnapshot(`
2576+
"{
2577+
"WRANGLER_ENV_VAR_0": "default-0",
2578+
"WRANGLER_ENV_VAR_1": "other-env-1",
2579+
"WRANGLER_ENV_VAR_2": "other-env-2",
2580+
"WRANGLER_ENV_VAR_3": "other-local-3",
2581+
"WRANGLER_ENV_VAR_4": "other-local-4"
2582+
}"
2583+
`);
2584+
});
25442585
});

packages/wrangler/e2e/startWorker.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,5 +756,61 @@ describe("DevEnv", () => {
756756
}
757757
`);
758758
});
759+
760+
it("vars from .env pointed at by `envFile` override vars from Wrangler config file and .env files local to the config file", async (t) => {
761+
t.onTestFinished(() => worker?.dispose());
762+
await helper.seed({
763+
"src/index.ts": dedent`
764+
export default {
765+
fetch(request, env) {
766+
return Response.json(env);
767+
}
768+
}
769+
`,
770+
"wrangler.jsonc": JSON.stringify({
771+
vars: {
772+
WRANGLER_ENV_VAR_0: "default-0",
773+
WRANGLER_ENV_VAR_1: "default-1",
774+
WRANGLER_ENV_VAR_2: "default-2",
775+
WRANGLER_ENV_VAR_3: "default-3",
776+
},
777+
}),
778+
".env": dedent`
779+
WRANGLER_ENV_VAR_1=env-1
780+
WRANGLER_ENV_VAR_2=env-2
781+
`,
782+
".env.local": dedent`
783+
WRANGLER_ENV_VAR_2=local-2
784+
WRANGLER_ENV_VAR_3=local-3
785+
`,
786+
"other/.env": dedent`
787+
WRANGLER_ENV_VAR_3=other-3
788+
WRANGLER_ENV_VAR_4=other-4
789+
`,
790+
"other/.env.local": dedent`
791+
WRANGLER_ENV_VAR_4=other-local-4
792+
WRANGLER_ENV_VAR_5=other-local-5
793+
`,
794+
});
795+
796+
const worker = await startWorker({
797+
config: path.resolve(helper.tmpPath, "wrangler.jsonc"),
798+
name: "test-worker",
799+
entrypoint: path.resolve(helper.tmpPath, "src/index.ts"),
800+
envFile: path.resolve(helper.tmpPath, "other/.env"),
801+
});
802+
803+
const res = await worker.fetch("http://dummy/test/path/1");
804+
expect(await res.json()).toMatchInlineSnapshot(`
805+
{
806+
"WRANGLER_ENV_VAR_0": "default-0",
807+
"WRANGLER_ENV_VAR_1": "default-1",
808+
"WRANGLER_ENV_VAR_2": "default-2",
809+
"WRANGLER_ENV_VAR_3": "other-3",
810+
"WRANGLER_ENV_VAR_4": "other-local-4",
811+
"WRANGLER_ENV_VAR_5": "other-local-5",
812+
}
813+
`);
814+
});
759815
});
760816
});

packages/wrangler/src/__tests__/ai.test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ describe("ai help", () => {
2626
wrangler ai finetune Interact with finetune files
2727
2828
GLOBAL FLAGS
29-
-c, --config Path to Wrangler configuration file [string]
30-
--cwd Run as if Wrangler was started in the specified directory instead of the current working directory [string]
31-
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
32-
-h, --help Show help [boolean]
33-
-v, --version Show version number [boolean]"
29+
-c, --config Path to Wrangler configuration file [string]
30+
--cwd Run as if Wrangler was started in the specified directory instead of the current working directory [string]
31+
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
32+
--env-file Path to the .env file to load [string]
33+
-h, --help Show help [boolean]
34+
-v, --version Show version number [boolean]"
3435
`);
3536
});
3637

@@ -55,11 +56,12 @@ describe("ai help", () => {
5556
wrangler ai finetune Interact with finetune files
5657
5758
GLOBAL FLAGS
58-
-c, --config Path to Wrangler configuration file [string]
59-
--cwd Run as if Wrangler was started in the specified directory instead of the current working directory [string]
60-
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
61-
-h, --help Show help [boolean]
62-
-v, --version Show version number [boolean]"
59+
-c, --config Path to Wrangler configuration file [string]
60+
--cwd Run as if Wrangler was started in the specified directory instead of the current working directory [string]
61+
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
62+
--env-file Path to the .env file to load [string]
63+
-h, --help Show help [boolean]
64+
-v, --version Show version number [boolean]"
6365
`);
6466
});
6567
});

packages/wrangler/src/__tests__/cert.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -484,11 +484,12 @@ describe("wrangler", () => {
484484
wrangler cert delete Delete an mTLS certificate
485485
486486
GLOBAL FLAGS
487-
-c, --config Path to Wrangler configuration file [string]
488-
--cwd Run as if Wrangler was started in the specified directory instead of the current working directory [string]
489-
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
490-
-h, --help Show help [boolean]
491-
-v, --version Show version number [boolean]"
487+
-c, --config Path to Wrangler configuration file [string]
488+
--cwd Run as if Wrangler was started in the specified directory instead of the current working directory [string]
489+
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
490+
--env-file Path to the .env file to load [string]
491+
-h, --help Show help [boolean]
492+
-v, --version Show version number [boolean]"
492493
`);
493494
});
494495
});

packages/wrangler/src/__tests__/cloudchamber/create.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,12 @@ describe("cloudchamber create", () => {
105105
Create a new deployment
106106
107107
GLOBAL FLAGS
108-
-c, --config Path to Wrangler configuration file [string]
109-
--cwd Run as if Wrangler was started in the specified directory instead of the current working directory [string]
110-
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
111-
-h, --help Show help [boolean]
112-
-v, --version Show version number [boolean]
108+
-c, --config Path to Wrangler configuration file [string]
109+
--cwd Run as if Wrangler was started in the specified directory instead of the current working directory [string]
110+
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
111+
--env-file Path to the .env file to load [string]
112+
-h, --help Show help [boolean]
113+
-v, --version Show version number [boolean]
113114
114115
OPTIONS
115116
--image Image to use for your deployment [string]

packages/wrangler/src/__tests__/cloudchamber/curl.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ describe("cloudchamber curl", () => {
3939
path [string] [required] [default: \\"/\\"]
4040
4141
GLOBAL FLAGS
42-
-c, --config Path to Wrangler configuration file [string]
43-
--cwd Run as if Wrangler was started in the specified directory instead of the current working directory [string]
44-
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
45-
-h, --help Show help [boolean]
46-
-v, --version Show version number [boolean]
42+
-c, --config Path to Wrangler configuration file [string]
43+
--cwd Run as if Wrangler was started in the specified directory instead of the current working directory [string]
44+
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
45+
--env-file Path to the .env file to load [string]
46+
-h, --help Show help [boolean]
47+
-v, --version Show version number [boolean]
4748
4849
OPTIONS
4950
-H, --header Add headers in the form of --header <name>:<value> [array]

packages/wrangler/src/__tests__/cloudchamber/delete.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ describe("cloudchamber delete", () => {
3535
deploymentId deployment you want to delete [string]
3636
3737
GLOBAL FLAGS
38-
-c, --config Path to Wrangler configuration file [string]
39-
--cwd Run as if Wrangler was started in the specified directory instead of the current working directory [string]
40-
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
41-
-h, --help Show help [boolean]
42-
-v, --version Show version number [boolean]"
38+
-c, --config Path to Wrangler configuration file [string]
39+
--cwd Run as if Wrangler was started in the specified directory instead of the current working directory [string]
40+
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
41+
--env-file Path to the .env file to load [string]
42+
-h, --help Show help [boolean]
43+
-v, --version Show version number [boolean]"
4344
`);
4445
});
4546

packages/wrangler/src/__tests__/cloudchamber/images.test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ describe("cloudchamber image", () => {
4242
wrangler cloudchamber registries list list registries configured for this account
4343
4444
GLOBAL FLAGS
45-
-c, --config Path to Wrangler configuration file [string]
46-
--cwd Run as if Wrangler was started in the specified directory instead of the current working directory [string]
47-
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
48-
-h, --help Show help [boolean]
49-
-v, --version Show version number [boolean]"
45+
-c, --config Path to Wrangler configuration file [string]
46+
--cwd Run as if Wrangler was started in the specified directory instead of the current working directory [string]
47+
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
48+
--env-file Path to the .env file to load [string]
49+
-h, --help Show help [boolean]
50+
-v, --version Show version number [boolean]"
5051
`);
5152
});
5253

@@ -188,11 +189,12 @@ describe("cloudchamber image list", () => {
188189
List images in the Cloudflare managed registry
189190
190191
GLOBAL FLAGS
191-
-c, --config Path to Wrangler configuration file [string]
192-
--cwd Run as if Wrangler was started in the specified directory instead of the current working directory [string]
193-
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
194-
-h, --help Show help [boolean]
195-
-v, --version Show version number [boolean]
192+
-c, --config Path to Wrangler configuration file [string]
193+
--cwd Run as if Wrangler was started in the specified directory instead of the current working directory [string]
194+
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
195+
--env-file Path to the .env file to load [string]
196+
-h, --help Show help [boolean]
197+
-v, --version Show version number [boolean]
196198
197199
OPTIONS
198200
--filter Regex to filter results [string]

packages/wrangler/src/__tests__/cloudchamber/list.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ describe("cloudchamber list", () => {
3636
This means that 'list' will only showcase deployments that contain this ID prefix [string]
3737
3838
GLOBAL FLAGS
39-
-c, --config Path to Wrangler configuration file [string]
40-
--cwd Run as if Wrangler was started in the specified directory instead of the current working directory [string]
41-
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
42-
-h, --help Show help [boolean]
43-
-v, --version Show version number [boolean]
39+
-c, --config Path to Wrangler configuration file [string]
40+
--cwd Run as if Wrangler was started in the specified directory instead of the current working directory [string]
41+
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
42+
--env-file Path to the .env file to load [string]
43+
-h, --help Show help [boolean]
44+
-v, --version Show version number [boolean]
4445
4546
OPTIONS
4647
--location Filter deployments by location [string]

packages/wrangler/src/__tests__/cloudchamber/modify.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,12 @@ describe("cloudchamber modify", () => {
7373
deploymentId The deployment you want to modify [string]
7474
7575
GLOBAL FLAGS
76-
-c, --config Path to Wrangler configuration file [string]
77-
--cwd Run as if Wrangler was started in the specified directory instead of the current working directory [string]
78-
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
79-
-h, --help Show help [boolean]
80-
-v, --version Show version number [boolean]
76+
-c, --config Path to Wrangler configuration file [string]
77+
--cwd Run as if Wrangler was started in the specified directory instead of the current working directory [string]
78+
-e, --env Environment to use for operations, and for selecting .env and .dev.vars files [string]
79+
--env-file Path to the .env file to load [string]
80+
-h, --help Show help [boolean]
81+
-v, --version Show version number [boolean]
8182
8283
OPTIONS
8384
--var Container environment variables [array]

0 commit comments

Comments
 (0)