Skip to content

Commit

Permalink
Clearer error message when trying to use Workers Sites with `wrangler…
Browse files Browse the repository at this point in the history
… versions upload` (#6379)

* throw error when using Workers Sites or Legacy Assets with `wrangler versions upload`

* extract and reuse experimentalAssets logic from `wrangler deploy` in `wrangler versions upload`

* add tests

* add changeset

* add back --assets experimental warning
mistakenly thought it was a duplicate

* move processExperimentalAssetsArg to experimental-assets.ts
  • Loading branch information
RamIdeas authored Jul 31, 2024
1 parent 0ffe17d commit 31aa15c
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 57 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-pans-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: clearer error message when trying to use Workers Sites or Legacy Assets with `wrangler versions upload`
67 changes: 67 additions & 0 deletions packages/wrangler/e2e/versions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,73 @@ describe("versions deploy", { timeout: TIMEOUT }, () => {
expect(countOccurrences(deploymentsList.stdout, versionId2)).toBe(1); // once for versions deploy, only
});

it("fails to upload if using Legacy Assets", async () => {
await helper.seed({
"wrangler.toml": dedent`
name = "${workerName}"
main = "src/index.ts"
compatibility_date = "2023-01-01"
`,
"src/index.ts": dedent`
export default {
fetch(request) {
return new Response("Hello World!")
}
}
`,
"package.json": dedent`
{
"name": "${workerName}",
"version": "0.0.0",
"private": true
}
`,
});

const upload = await helper.run(
`wrangler versions upload --assets='./public' --x-versions`
);

expect(normalize(upload.output)).toMatchInlineSnapshot(`
"X [ERROR] Legacy Assets are not supported in Gradual Deployments.
🪵 Logs were written to "<LOG>""
`);
});

it("fails to upload if using Workers Sites", async () => {
await helper.seed({
"wrangler.toml": dedent`
name = "${workerName}"
main = "src/index.ts"
compatibility_date = "2023-01-01"
[site]
bucket = "./public"
`,
"src/index.ts": dedent`
export default {
fetch(request) {
return new Response("Hello World!")
}
}
`,
"package.json": dedent`
{
"name": "${workerName}",
"version": "0.0.0",
"private": true
}
`,
});

const upload = await helper.run(`wrangler versions upload --x-versions`);

expect(normalize(upload.output)).toMatchInlineSnapshot(`
"X [ERROR] Workers Sites are not supported in Gradual Deployments.
🪵 Logs were written to "<LOG>""
`);
});

it("should delete Worker", async () => {
const { stdout } = await helper.run(`wrangler delete`);

Expand Down
32 changes: 3 additions & 29 deletions packages/wrangler/src/deploy/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { existsSync } from "node:fs";
import path from "node:path";
import { findWranglerToml, readConfig } from "../config";
import { getEntry } from "../deployment-bundle/entry";
import { UserError } from "../errors";
import { getExperimentalAssetsBasePath } from "../experimental-assets";
import { processExperimentalAssetsArg } from "../experimental-assets";
import {
getRules,
getScriptName,
Expand Down Expand Up @@ -290,39 +289,14 @@ export async function deployHandler(
);
}

const experimentalAssets = args.experimentalAssets
? { directory: args.experimentalAssets }
: config.experimental_assets;
if (experimentalAssets) {
const experimentalAssetsBasePath = getExperimentalAssetsBasePath(
config,
args.experimentalAssets
);
const resolvedExperimentalAssetsPath = path.resolve(
experimentalAssetsBasePath,
experimentalAssets.directory
);

if (!existsSync(resolvedExperimentalAssetsPath)) {
const sourceOfTruthMessage = args.experimentalAssets
? '"--experimental-assets" command line argument'
: '"experimental_assets.directory" field in your configuration file';

throw new UserError(
`The directory specified by the ${sourceOfTruthMessage} does not exist:\n` +
`${resolvedExperimentalAssetsPath}`
);
}

experimentalAssets.directory = resolvedExperimentalAssetsPath;
}

if (args.assets) {
logger.warn(
"The --assets argument is experimental and may change or break at any time"
);
}

const experimentalAssets = processExperimentalAssetsArg(args, config);

if (args.latest) {
logger.warn(
"Using the latest version of the Workers runtime. To silence this warning, please choose a specific version of the runtime with --compatibility-date, or add a compatibility_date to your wrangler.toml.\n"
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/deployment-bundle/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export async function getEntry(
experimentalAssets?: string;
},
config: Config,
command: "dev" | "deploy" | "types"
command: "dev" | "deploy" | "versions upload" | "types"
): Promise<Entry> {
let file: string;
let directory = process.cwd();
Expand Down
28 changes: 2 additions & 26 deletions packages/wrangler/src/dev.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import assert from "node:assert";
import events from "node:events";
import { existsSync } from "node:fs";
import path from "node:path";
import util from "node:util";
import { isWebContainer } from "@webcontainer/env";
Expand All @@ -22,7 +21,7 @@ import registerDevHotKeys from "./dev/hotkeys";
import { maybeRegisterLocalWorker } from "./dev/local";
import { startDevServer } from "./dev/start-server";
import { UserError } from "./errors";
import { getExperimentalAssetsBasePath } from "./experimental-assets";
import { processExperimentalAssetsArg } from "./experimental-assets";
import { run } from "./experimental-flags";
import isInteractive from "./is-interactive";
import { logger } from "./logger";
Expand Down Expand Up @@ -804,31 +803,8 @@ export async function startDev(args: StartDevOptions) {
});
}

const experimentalAssets = args.experimentalAssets
? { directory: args.experimentalAssets }
: config.experimental_assets;
const experimentalAssets = processExperimentalAssetsArg(args, config);
if (experimentalAssets) {
const experimentalAssetsBasePath = getExperimentalAssetsBasePath(
config,
args.experimentalAssets
);
const resolvedExperimentalAssetsPath = path.resolve(
experimentalAssetsBasePath,
experimentalAssets.directory
);

if (!existsSync(resolvedExperimentalAssetsPath)) {
const sourceOfTruthMessage = args.experimentalAssets
? '"--experimental-assets" command line argument'
: '"experimental_assets.directory" field in your configuration file';

throw new UserError(
`The directory specified by the ${sourceOfTruthMessage} does not exist:\n` +
`${resolvedExperimentalAssetsPath}`
);
}

experimentalAssets.directory = resolvedExperimentalAssetsPath;
args.forceLocal = true;
}

Expand Down
36 changes: 36 additions & 0 deletions packages/wrangler/src/experimental-assets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { existsSync } from "node:fs";
import path from "node:path";
import { UserError } from "./errors";
import type { Config } from "./config";

/**
Expand All @@ -13,3 +15,37 @@ export function getExperimentalAssetsBasePath(
? process.cwd()
: path.resolve(path.dirname(config.configPath ?? "wrangler.toml"));
}

export function processExperimentalAssetsArg(
args: { experimentalAssets: string | undefined },
config: Config
) {
const experimentalAssets = args.experimentalAssets
? { directory: args.experimentalAssets }
: config.experimental_assets;
if (experimentalAssets) {
const experimentalAssetsBasePath = getExperimentalAssetsBasePath(
config,
args.experimentalAssets
);
const resolvedExperimentalAssetsPath = path.resolve(
experimentalAssetsBasePath,
experimentalAssets.directory
);

if (!existsSync(resolvedExperimentalAssetsPath)) {
const sourceOfTruthMessage = args.experimentalAssets
? '"--experimental-assets" command line argument'
: '"experimental_assets.directory" field in your configuration file';

throw new UserError(
`The directory specified by the ${sourceOfTruthMessage} does not exist:\n` +
`${resolvedExperimentalAssetsPath}`
);
}

experimentalAssets.directory = resolvedExperimentalAssetsPath;
}

return experimentalAssets;
}
42 changes: 41 additions & 1 deletion packages/wrangler/src/versions/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import path from "node:path";
import { findWranglerToml, readConfig } from "../config";
import { getEntry } from "../deployment-bundle/entry";
import { UserError } from "../errors";
import { processExperimentalAssetsArg } from "../experimental-assets";
import {
getRules,
getScriptName,
Expand Down Expand Up @@ -86,24 +88,46 @@ export function versionsUploadOptions(yargs: CommonYargsArgv) {
type: "boolean",
default: false,
})
.option("legacy-assets", {
describe: "(Experimental) Static assets to be served",
type: "string",
requiresArg: true,
hidden: true,
})
.option("assets", {
describe: "(Experimental) Static assets to be served",
type: "string",
requiresArg: true,
hidden: true,
})
.option("experimental-assets", {
describe: "Static assets to be served",
type: "string",
alias: "x-assets",
requiresArg: true,
hidden: true,
})
.option("site", {
describe: "Root folder of static assets for Workers Sites",
type: "string",
requiresArg: true,
hidden: true,
})
.option("site-include", {
describe:
"Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded.",
type: "string",
requiresArg: true,
array: true,
hidden: true,
})
.option("site-exclude", {
describe:
"Array of .gitignore-style patterns that match file or directory names from the sites directory. Matched items will not be uploaded.",
type: "string",
requiresArg: true,
array: true,
hidden: true,
})
.option("var", {
describe:
Expand Down Expand Up @@ -180,7 +204,7 @@ export async function versionsUploadHandler(
args.config || (args.script && findWranglerToml(path.dirname(args.script)));
const projectRoot = configPath && path.dirname(configPath);
const config = readConfig(configPath, args);
const entry = await getEntry(args, config, "deploy");
const entry = await getEntry(args, config, "versions upload");
await metrics.sendMetricsEvent(
"upload worker version",
{
Expand All @@ -191,6 +215,21 @@ export async function versionsUploadHandler(
}
);

args.legacyAssets = args.legacyAssets ?? args.assets;

if (args.site || config.site) {
throw new UserError(
"Workers Sites are not supported in Gradual Deployments."
);
}
if (args.legacyAssets || config.legacy_assets) {
throw new UserError(
"Legacy Assets are not supported in Gradual Deployments."
);
}

const experimentalAssets = processExperimentalAssetsArg(args, config);

if (args.latest) {
logger.warn(
"Using the latest version of the Workers runtime. To silence this warning, please choose a specific version of the runtime with --compatibility-date, or add a compatibility_date to your wrangler.toml.\n"
Expand Down Expand Up @@ -222,6 +261,7 @@ export async function versionsUploadHandler(
jsxFactory: args.jsxFactory,
jsxFragment: args.jsxFragment,
tsconfig: args.tsconfig,
experimentalAssets: experimentalAssets?.directory,
minify: args.minify,
uploadSourceMaps: args.uploadSourceMaps,
nodeCompat: args.nodeCompat,
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/versions/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Props = {
env: string | undefined;
compatibilityDate: string | undefined;
compatibilityFlags: string[] | undefined;
experimentalAssets: string | undefined;
vars: Record<string, string> | undefined;
defines: Record<string, string> | undefined;
alias: Record<string, string> | undefined;
Expand Down

0 comments on commit 31aa15c

Please sign in to comment.