Skip to content

Commit 75b454c

Browse files
dario-piotrowiczemily-shenjamesopstad
authored
[wrangler] add validation to redirected configs in regards to environments (#8596)
* [wrangler] add validation to redirected configs in regards to environments * error instead of warning on specified environments * Update packages/wrangler/src/config/validation.ts Co-authored-by: emily-shen <[email protected]> * Update packages/wrangler/src/config/validation.ts Co-authored-by: James Opstad <[email protected]> * update lock file * update fixture tests --------- Co-authored-by: emily-shen <[email protected]> Co-authored-by: James Opstad <[email protected]>
1 parent 30f7317 commit 75b454c

File tree

15 files changed

+241
-60
lines changed

15 files changed

+241
-60
lines changed

.changeset/sour-things-run.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
add validation to redirected configs in regards to environments
6+
7+
add the following validation behaviors to wrangler deploy commands, that relate
8+
to redirected configs (i.e. config files specified by `.wrangler/deploy/config.json` files):
9+
10+
- redirected configs are supposed to be already flattened configurations without any
11+
environment (i.e. a build tool should generate redirected configs already targeting specific
12+
environments), so if wrangler encounters a redirected config with some environments defined
13+
it should error
14+
- given the point above, specifying an environment (`--env=my-env`) when using redirected
15+
configs is incorrect, so these environments should be ignored and a warning should be
16+
presented to the user

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ fixtures/interactive-dev-tests/src/startup-error.ts
4646
# These are generated by the build step
4747
fixtures/pages-redirected-config/build/*
4848
fixtures/redirected-config-worker/build/*
49+
fixtures/redirected-config-worker-with-environments/build/*
4950

5051
# Import attributes generate an error
5152
packages/unenv-preset/src/preset.ts
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist
2+
build
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "redirected-config-worker-with-environments",
3+
"private": true,
4+
"description": "",
5+
"license": "ISC",
6+
"author": "",
7+
"main": "src/index.js",
8+
"scripts": {
9+
"build": "node -r esbuild-register tools/build.ts",
10+
"check:type": "tsc",
11+
"dev": "pnpm run build && wrangler dev",
12+
"test:ci": "pnpm run build && vitest run"
13+
},
14+
"devDependencies": {
15+
"@cloudflare/workers-tsconfig": "workspace:^",
16+
"undici": "catalog:default",
17+
"vitest": "catalog:default",
18+
"wrangler": "workspace:*"
19+
},
20+
"volta": {
21+
"extends": "../../package.json"
22+
}
23+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
async fetch(request, env) {
3+
return new Response("Generated: " + env.generated ?? false);
4+
},
5+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { resolve } from "path";
2+
import { describe, it } from "vitest";
3+
import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived";
4+
5+
const basePath = resolve(__dirname, "..");
6+
7+
describe("'wrangler dev' errors because redirected configs can't include environments", () => {
8+
it("uses the generated config", async ({ expect }) => {
9+
await expect(
10+
runWranglerDev(basePath, ["--port=0", "--inspector-port=0"])
11+
).rejects.toThrowError(
12+
/Redirected configurations cannot include environments but the following have been found: \"staging\"/
13+
);
14+
});
15+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { copyFileSync, mkdirSync, rmSync, writeFileSync } from "fs";
2+
3+
// Create a pseudo build directory
4+
rmSync("build", { recursive: true, force: true });
5+
mkdirSync("build");
6+
const config = {
7+
name: "redirected-config-worker",
8+
compatibility_date: "2024-12-01",
9+
main: "index.js",
10+
vars: { generated: true, environmentName: "top-level" },
11+
env: {
12+
staging: {
13+
vars: {
14+
generated: true,
15+
environmentName: "staging",
16+
},
17+
},
18+
},
19+
};
20+
writeFileSync("build/wrangler.json", JSON.stringify(config, undefined, 2));
21+
copyFileSync("src/index.js", "build/index.js");
22+
23+
// Create the redirect file
24+
rmSync(".wrangler/deploy", { recursive: true, force: true });
25+
mkdirSync(".wrangler/deploy", { recursive: true });
26+
const redirect = { configPath: "../../build/wrangler.json" };
27+
writeFileSync(
28+
".wrangler/deploy/config.json",
29+
JSON.stringify(redirect, undefined, 2)
30+
);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"esModuleInterop": true,
5+
"module": "CommonJS",
6+
"lib": ["ES2020"],
7+
"types": ["node"],
8+
"skipLibCheck": true,
9+
"moduleResolution": "node",
10+
"noEmit": true
11+
},
12+
"include": ["tests", "../../node-types.d.ts"]
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"$schema": "http://turbo.build/schema.json",
3+
"extends": ["//"],
4+
"tasks": {
5+
"build": {
6+
"outputs": ["build/**"]
7+
}
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineProject, mergeConfig } from "vitest/config";
2+
import configShared from "../../vitest.shared";
3+
4+
export default mergeConfig(
5+
configShared,
6+
defineProject({
7+
test: {},
8+
})
9+
);

0 commit comments

Comments
 (0)