Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(wrangler): pages functions build-env should throw if invalid Pages config file #5587

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/giant-pears-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": minor
---

fix: `pages functions build-env` should throw error if invalid Pages config file is found
75 changes: 60 additions & 15 deletions packages/wrangler/src/__tests__/pages/pages-build-env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ describe("pages build env", () => {
);
});

it("should fail with no project dir", async () => {
await expect(
runWrangler("pages functions build-env")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"No Pages project location specified"`
);
});

it("should fail with no outfile", async () => {
await expect(
runWrangler("pages functions build-env .")
).rejects.toThrowErrorMatchingInlineSnapshot(`"No outfile specified"`);
});

it("should exit with specific exit code if no config file is found", async () => {
logger.loggerLevel = "debug";
await runWrangler("pages functions build-env . --outfile out.json");
Expand All @@ -57,20 +71,6 @@ describe("pages build env", () => {
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should fail with no project dir", async () => {
await expect(
runWrangler("pages functions build-env")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"No Pages project location specified"`
);
});

it("should fail with no outfile", async () => {
await expect(
runWrangler("pages functions build-env .")
).rejects.toThrowErrorMatchingInlineSnapshot(`"No outfile specified"`);
});

it("should exit with specific code if a non-pages config file is found", async () => {
logger.loggerLevel = "debug";
writeWranglerToml({
Expand Down Expand Up @@ -112,7 +112,7 @@ describe("pages build env", () => {
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should exit correctly with an unparseable config file", async () => {
it("should exit correctly with an unparseable non-pages config file", async () => {
logger.loggerLevel = "debug";

writeFileSync("./wrangler.toml", 'INVALID "FILE');
Expand Down Expand Up @@ -170,6 +170,51 @@ describe("pages build env", () => {
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should throw an error if an invalid pages confg file is found", async () => {
writeWranglerToml({
pages_build_output_dir: "dist",
vars: {
VAR1: "VALUE1",
},
env: {
staging: {
vars: {
VAR1: "PROD_VALUE1",
},
},
},
});

await expect(runWrangler("pages functions build-env . --outfile data.json"))
.rejects.toThrowErrorMatchingInlineSnapshot(`
"Running configuration file validation for Pages:
- Configuration file contains the following environment names that are not supported by Pages projects:
\\"staging\\".
The supported named-environments for Pages are \\"preview\\" and \\"production\\"."
`);
});

it("should exit if an unparseable pages confg file is found", async () => {
writeFileSync(
"./wrangler.toml",
`
pages_build_output_dir = "./dist"
name = "pages-is-awesome"
compatibility_date = "2024-01-01"

something that fails toml parsing
`
);

await runWrangler("pages functions build-env . --outfile data.json");
expect(process.exitCode).toEqual(EXIT_CODE_INVALID_PAGES_CONFIG);
expect(std.out).toMatchInlineSnapshot(`
"Checking for configuration in a wrangler.toml configuration file (BETA)

Found wrangler.toml file. Reading build configuration..."
`);
});

it("should return top-level by default", async () => {
process.env.PAGES_ENVIRONMENT = "";
writeWranglerToml({
Expand Down
16 changes: 13 additions & 3 deletions packages/wrangler/src/pages/build-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,19 @@ export const Handler = async (args: PagesBuildEnvArgs) => {
true
);
} catch (err) {
logger.debug("wrangler.toml file is invalid. Exiting.");
process.exitCode = EXIT_CODE_INVALID_PAGES_CONFIG;
return;
// found `wrangler.toml` but `pages_build_output_dir` is not specififed
if (
err instanceof FatalError &&
err.code === EXIT_CODE_INVALID_PAGES_CONFIG
) {
logger.debug("wrangler.toml file is invalid. Exiting.");
process.exitCode = EXIT_CODE_INVALID_PAGES_CONFIG;
return;
}

// found `wrangler.toml` with `pages_build_output_dir` specified, but
// file contains invalid configuration
throw err;
}

// Ensure JSON variables are not included
Expand Down