Skip to content

Commit 8bcb257

Browse files
V3 Backport for #8924 (#8928)
* fix redirected config env validation breaking wrangler pages commands * add tests for pages deploy command * remove extra unnecessary check
1 parent 8d70140 commit 8bcb257

File tree

3 files changed

+99
-5
lines changed

3 files changed

+99
-5
lines changed

.changeset/odd-steaks-cheat.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix redirected config env validation breaking wrangler pages commands
6+
7+
a validation check has recently been introduced to make wrangler error on
8+
deploy commands when an environment is specified and a redirected configuration
9+
is in use (the reason being that redirected configurations should not include
10+
any environment), this check is problematic with pages commands where the
11+
"production" environment is anyways set by default, to address this the validation
12+
check is being relaxed here on pages commands

packages/wrangler/src/__tests__/pages/deploy.test.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5649,6 +5649,84 @@ Failed to publish your Function. Got error: Uncaught TypeError: a is not a funct
56495649
expect(std.err).toMatchInlineSnapshot(`""`);
56505650
});
56515651
});
5652+
5653+
describe("deploys using redirected configs", () => {
5654+
let fooProjectDetailsChecked = false;
5655+
5656+
beforeEach(() => {
5657+
fooProjectDetailsChecked = false;
5658+
mkdirSync("public");
5659+
mkdirSync("dist");
5660+
mkdirSync(".wrangler/deploy", { recursive: true });
5661+
writeFileSync(
5662+
".wrangler/deploy/config.json",
5663+
JSON.stringify({ configPath: "../../dist/wrangler.json" })
5664+
);
5665+
writeFileSync(
5666+
"dist/wrangler.json",
5667+
JSON.stringify({
5668+
compatibility_date: "2025-01-01",
5669+
name: "foo",
5670+
pages_build_output_dir: "../public",
5671+
})
5672+
);
5673+
5674+
simulateServer(async () => {});
5675+
5676+
msw.use(
5677+
http.get(
5678+
"*/accounts/:accountId/pages/projects/foo",
5679+
async ({ params }) => {
5680+
expect(params.accountId).toEqual("some-account-id");
5681+
5682+
fooProjectDetailsChecked = true;
5683+
5684+
return HttpResponse.json(
5685+
{
5686+
success: true,
5687+
errors: [],
5688+
messages: [],
5689+
result: {
5690+
production_branch: "main",
5691+
deployment_configs: {
5692+
production: {},
5693+
preview: {},
5694+
},
5695+
} as Partial<Project>,
5696+
},
5697+
{ status: 200 }
5698+
);
5699+
}
5700+
)
5701+
);
5702+
});
5703+
5704+
afterEach(() => {
5705+
expect(fooProjectDetailsChecked).toBe(true);
5706+
});
5707+
5708+
const expectedInfo = dedent`
5709+
Using redirected Wrangler configuration.
5710+
- Configuration being used: "dist/wrangler.json"
5711+
- Original user's configuration: "<no user config found>"
5712+
- Deploy configuration file: ".wrangler/deploy/config.json"
5713+
`;
5714+
5715+
it("should work without a branch specified (i.e. defaulting to the production environment)", async () => {
5716+
await runWrangler("pages deploy");
5717+
expect(std.info).toContain(expectedInfo);
5718+
});
5719+
5720+
it("should work with the main branch (i.e. the production environment)", async () => {
5721+
await runWrangler("pages deploy --branch main");
5722+
expect(std.info).toContain(expectedInfo);
5723+
});
5724+
5725+
it("should work with any branch (i.e. the preview environment)", async () => {
5726+
await runWrangler("pages deploy --branch my-branch");
5727+
expect(std.info).toContain(expectedInfo);
5728+
});
5729+
});
56525730
});
56535731

56545732
function mockGetProjectHandler(

packages/wrangler/src/config/validation.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,15 @@ export function normalizeAndValidateConfig(
212212

213213
if (envName !== undefined) {
214214
if (isRedirectedConfig) {
215-
diagnostics.errors.push(dedent`
216-
You have specified the environment "${envName}", but are using a redirected configuration, produced by a build tool such as Vite.
217-
You need to set the environment in your build tool, rather than via Wrangler.
218-
For example, if you are using Vite, refer to these docs: https://developers.cloudflare.com/workers/vite-plugin/reference/cloudflare-environments/
219-
`);
215+
// Note: we error if the user is specifying an environment, but not for pages
216+
// commands where the environment is always set (to either "preview" or "production")
217+
if (!isPagesConfig(rawConfig)) {
218+
diagnostics.errors.push(dedent`
219+
You have specified the environment "${envName}", but are using a redirected configuration, produced by a build tool such as Vite.
220+
You need to set the environment in your build tool, rather than via Wrangler.
221+
For example, if you are using Vite, refer to these docs: https://developers.cloudflare.com/workers/vite-plugin/reference/cloudflare-environments/
222+
`);
223+
}
220224
} else {
221225
const envDiagnostics = new Diagnostics(
222226
`"env.${envName}" environment configuration`

0 commit comments

Comments
 (0)