Skip to content

Commit 086a6b8

Browse files
WillTaylorDevCarmenPopoviciu
andauthoredDec 19, 2024··
Provide validation around experimental_serve_directly usage (#7537)
* Provide validation around experimental_serve_directly usage in dev and deploy * Update based on PR feedback * Update packages/wrangler/src/assets.ts * Update packages/wrangler/src/__tests__/dev.test.ts * Update packages/wrangler/src/__tests__/deploy.test.ts * fix --------- Co-authored-by: Carmen Popoviciu <cpopoviciu@cloudflare.com>
1 parent 5ccad7d commit 086a6b8

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed
 

‎.changeset/swift-zebras-guess.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
Provide validation around assets.experimental_serve_directly

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

+61
Original file line numberDiff line numberDiff line change
@@ -4687,6 +4687,67 @@ addEventListener('fetch', event => {});`
46874687
`);
46884688
});
46894689

4690+
it("should warn if experimental_serve_directly=false but no binding is provided", async () => {
4691+
const assets = [
4692+
{ filePath: ".assetsignore", content: "*.bak\nsub-dir" },
4693+
{ filePath: "file-1.txt", content: "Content of file-1" },
4694+
{ filePath: "file-2.bak", content: "Content of file-2" },
4695+
{ filePath: "file-3.txt", content: "Content of file-3" },
4696+
{ filePath: "sub-dir/file-4.bak", content: "Content of file-4" },
4697+
{ filePath: "sub-dir/file-5.txt", content: "Content of file-5" },
4698+
];
4699+
writeAssets(assets, "assets");
4700+
writeWorkerSource({ format: "js" });
4701+
writeWranglerConfig({
4702+
main: "index.js",
4703+
assets: {
4704+
directory: "assets",
4705+
experimental_serve_directly: false,
4706+
},
4707+
});
4708+
const bodies: AssetManifest[] = [];
4709+
await mockAUSRequest(bodies);
4710+
mockSubDomainRequest();
4711+
mockUploadWorkerRequest({
4712+
expectedAssets: {
4713+
jwt: "<<aus-completion-token>>",
4714+
config: {
4715+
serve_directly: false,
4716+
},
4717+
},
4718+
expectedMainModule: "index.js",
4719+
});
4720+
4721+
await runWrangler("deploy");
4722+
4723+
expect(std.warn).toMatchInlineSnapshot(`
4724+
"▲ [WARNING] experimental_serve_directly=false set without an assets binding
4725+
4726+
Setting experimental_serve_directly to false will always invoke your Worker script.
4727+
To fetch your assets from your Worker, please set the [assets.binding] key in your configuration
4728+
file.
4729+
4730+
Read more: https://developers.cloudflare.com/workers/static-assets/binding/#binding
4731+
4732+
"
4733+
`);
4734+
});
4735+
4736+
it("should error if experimental_serve_directly is false and no user Worker is provided", async () => {
4737+
writeWranglerConfig({
4738+
assets: {
4739+
directory: "xyz",
4740+
experimental_serve_directly: false,
4741+
},
4742+
});
4743+
4744+
await expect(runWrangler("deploy")).rejects
4745+
.toThrowErrorMatchingInlineSnapshot(`
4746+
[Error: Cannot set experimental_serve_directly=false without a Worker script.
4747+
Please remove experimental_serve_directly from your configuration file, or provide a Worker script in your configuration file (\`main\`).]
4748+
`);
4749+
});
4750+
46904751
it("should be able to upload files with special characters in filepaths", async () => {
46914752
// NB windows will disallow these characters in file paths anyway < > : " / \ | ? *
46924753
const assets = [

‎packages/wrangler/src/__tests__/dev.test.ts

+41
Original file line numberDiff line numberDiff line change
@@ -1679,6 +1679,47 @@ describe.sequential("wrangler dev", () => {
16791679
);
16801680
});
16811681

1682+
it("should warn if experimental_serve_directly=false but no binding is provided", async () => {
1683+
writeWranglerConfig({
1684+
main: "index.js",
1685+
assets: {
1686+
directory: "assets",
1687+
experimental_serve_directly: false,
1688+
},
1689+
});
1690+
fs.mkdirSync("assets");
1691+
fs.writeFileSync("index.js", `export default {};`);
1692+
1693+
await runWranglerUntilConfig("dev");
1694+
1695+
expect(std.warn).toMatchInlineSnapshot(`
1696+
"▲ [WARNING] experimental_serve_directly=false set without an assets binding
1697+
1698+
Setting experimental_serve_directly to false will always invoke your Worker script.
1699+
To fetch your assets from your Worker, please set the [assets.binding] key in your configuration
1700+
file.
1701+
1702+
Read more: https://developers.cloudflare.com/workers/static-assets/binding/#binding
1703+
1704+
"
1705+
`);
1706+
});
1707+
1708+
it("should error if experimental_serve_directly is false and no user Worker is provided", async () => {
1709+
writeWranglerConfig({
1710+
assets: { directory: "assets", experimental_serve_directly: false },
1711+
});
1712+
fs.mkdirSync("assets");
1713+
await expect(
1714+
runWrangler("dev")
1715+
).rejects.toThrowErrorMatchingInlineSnapshot(
1716+
`
1717+
[Error: Cannot set experimental_serve_directly=false without a Worker script.
1718+
Please remove experimental_serve_directly from your configuration file, or provide a Worker script in your configuration file (\`main\`).]
1719+
`
1720+
);
1721+
});
1722+
16821723
it("should error if directory specified by '--assets' command line argument does not exist", async () => {
16831724
writeWranglerConfig({
16841725
main: "./index.js",

‎packages/wrangler/src/assets.ts

+29
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,35 @@ export function validateAssetsArgsAndConfig(
443443
"Please remove the asset binding from your configuration file, or provide a Worker script in your configuration file (`main`)."
444444
);
445445
}
446+
447+
// User Worker ahead of assets, but no assets binding provided
448+
if (
449+
"legacy" in args
450+
? args.assets?.assetConfig?.serve_directly === false &&
451+
!args.assets?.binding
452+
: config?.assets?.experimental_serve_directly === false &&
453+
!config?.assets?.binding
454+
) {
455+
logger.warn(
456+
"experimental_serve_directly=false set without an assets binding\n" +
457+
"Setting experimental_serve_directly to false will always invoke your Worker script.\n" +
458+
"To fetch your assets from your Worker, please set the [assets.binding] key in your configuration file.\n\n" +
459+
"Read more: https://developers.cloudflare.com/workers/static-assets/binding/#binding"
460+
);
461+
}
462+
463+
// Using serve_directly=false, but didn't provide a Worker script
464+
if (
465+
"legacy" in args
466+
? args.entrypoint === noOpEntrypoint &&
467+
args.assets?.assetConfig?.serve_directly === false
468+
: !config?.main && config?.assets?.experimental_serve_directly === false
469+
) {
470+
throw new UserError(
471+
"Cannot set experimental_serve_directly=false without a Worker script.\n" +
472+
"Please remove experimental_serve_directly from your configuration file, or provide a Worker script in your configuration file (`main`)."
473+
);
474+
}
446475
}
447476

448477
const CF_ASSETS_IGNORE_FILENAME = ".assetsignore";

0 commit comments

Comments
 (0)
Please sign in to comment.