Skip to content

Commit 6f8e892

Browse files
Backport #8534 to Wrangler v3 (#8535)
* Improve the error messaging when the user provides neither an entry point nor an asset directory * copy edits --------- Co-authored-by: Peter Bacon Darwin <[email protected]>
1 parent 1f5c58e commit 6f8e892

File tree

4 files changed

+168
-11
lines changed

4 files changed

+168
-11
lines changed

.changeset/rude-socks-glow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
improve the error messaging when the user provides neither an entry point nor an asset directory

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

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
printOffendingDependencies,
1616
} from "../deployment-bundle/bundle-reporter";
1717
import { clearOutputFilePath } from "../output";
18+
import { sniffUserAgent } from "../package-manager";
1819
import { writeAuthConfigFile } from "../user";
1920
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
2021
import { mockAuthDomain } from "./helpers/mock-auth-domain";
@@ -2629,22 +2630,70 @@ addEventListener('fetch', event => {});`
26292630
});
26302631

26312632
it("should error if there is no entry-point specified", async () => {
2633+
vi.mocked(sniffUserAgent).mockReturnValue("npm");
26322634
writeWranglerConfig();
26332635
writeWorkerSource();
26342636
mockUploadWorkerRequest();
26352637
mockSubDomainRequest();
26362638
await expect(
26372639
runWrangler("deploy")
26382640
).rejects.toThrowErrorMatchingInlineSnapshot(
2639-
`[Error: Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler deploy path/to/script\`) or the \`main\` config field.]`
2641+
`
2642+
[Error: Missing entry-point to Worker script or to assets directory
2643+
2644+
If there is code to deploy, you can either:
2645+
- Specify an entry-point to your Worker script via the command line (ex: \`npx wrangler deploy src/index.ts\`)
2646+
- Or add the following to your "wrangler.toml" file:
2647+
2648+
\`\`\`
2649+
main = "src/index.ts"
2650+
2651+
\`\`\`
2652+
2653+
2654+
If are uploading a directory of assets, you can either:
2655+
- Specify the path to the directory of assets via the command line: (ex: \`npx wrangler deploy --assets=./dist\`)
2656+
- Or add the following to your "wrangler.toml" file:
2657+
2658+
\`\`\`
2659+
[assets]
2660+
directory = "./dist"
2661+
2662+
\`\`\`
2663+
]
2664+
`
26402665
);
26412666

26422667
expect(std.out).toMatchInlineSnapshot(`""`);
26432668
expect(std.err).toMatchInlineSnapshot(`
2644-
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mMissing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler deploy path/to/script\`) or the \`main\` config field.[0m
2669+
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mMissing entry-point to Worker script or to assets directory[0m
26452670
2646-
"
2647-
`);
2671+
2672+
If there is code to deploy, you can either:
2673+
- Specify an entry-point to your Worker script via the command line (ex: \`npx wrangler deploy
2674+
src/index.ts\`)
2675+
- Or add the following to your \\"wrangler.toml\\" file:
2676+
2677+
\`\`\`
2678+
main = \\"src/index.ts\\"
2679+
2680+
\`\`\`
2681+
2682+
2683+
If are uploading a directory of assets, you can either:
2684+
- Specify the path to the directory of assets via the command line: (ex: \`npx wrangler deploy
2685+
--assets=./dist\`)
2686+
- Or add the following to your \\"wrangler.toml\\" file:
2687+
2688+
\`\`\`
2689+
[assets]
2690+
directory = \\"./dist\\"
2691+
2692+
\`\`\`
2693+
2694+
2695+
"
2696+
`);
26482697
});
26492698

26502699
it("should not require an explicit entry point when using --legacy-assets", async () => {

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

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { getWorkerAccountAndContext } from "../dev/remote";
1212
import { FatalError } from "../errors";
1313
import { CI } from "../is-ci";
1414
import { logger } from "../logger";
15+
import { sniffUserAgent } from "../package-manager";
1516
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
1617
import { mockConsoleMethods } from "./helpers/mock-console";
1718
import { useMockIsTTY } from "./helpers/mock-istty";
@@ -298,20 +299,68 @@ describe.sequential("wrangler dev", () => {
298299

299300
describe("entry-points", () => {
300301
it("should error if there is no entry-point specified", async () => {
302+
vi.mocked(sniffUserAgent).mockReturnValue("npm");
301303
writeWranglerConfig();
302304

303305
await expect(
304306
runWrangler("dev")
305307
).rejects.toThrowErrorMatchingInlineSnapshot(
306-
`[Error: Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler dev path/to/script\`) or the \`main\` config field.]`
308+
`
309+
[Error: Missing entry-point to Worker script or to assets directory
310+
311+
If there is code to deploy, you can either:
312+
- Specify an entry-point to your Worker script via the command line (ex: \`npx wrangler dev src/index.ts\`)
313+
- Or add the following to your "wrangler.toml" file:
314+
315+
\`\`\`
316+
main = "src/index.ts"
317+
318+
\`\`\`
319+
320+
321+
If are uploading a directory of assets, you can either:
322+
- Specify the path to the directory of assets via the command line: (ex: \`npx wrangler dev --assets=./dist\`)
323+
- Or add the following to your "wrangler.toml" file:
324+
325+
\`\`\`
326+
[assets]
327+
directory = "./dist"
328+
329+
\`\`\`
330+
]
331+
`
307332
);
308333

309334
expect(std.out).toMatchInlineSnapshot(`""`);
310335
expect(std.err).toMatchInlineSnapshot(`
311-
"X [ERROR] Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler dev path/to/script\`) or the \`main\` config field.
336+
"X [ERROR] Missing entry-point to Worker script or to assets directory
337+
312338
313-
"
314-
`);
339+
If there is code to deploy, you can either:
340+
- Specify an entry-point to your Worker script via the command line (ex: \`npx wrangler dev
341+
src/index.ts\`)
342+
- Or add the following to your \\"wrangler.toml\\" file:
343+
344+
\`\`\`
345+
main = \\"src/index.ts\\"
346+
347+
\`\`\`
348+
349+
350+
If are uploading a directory of assets, you can either:
351+
- Specify the path to the directory of assets via the command line: (ex: \`npx wrangler dev
352+
--assets=./dist\`)
353+
- Or add the following to your \\"wrangler.toml\\" file:
354+
355+
\`\`\`
356+
[assets]
357+
directory = \\"./dist\\"
358+
359+
\`\`\`
360+
361+
362+
"
363+
`);
315364
});
316365

317366
it("should use `main` from the top-level environment", async () => {

packages/wrangler/src/deployment-bundle/entry.ts

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import path from "node:path";
2-
import { configFileName } from "../config";
2+
import dedent from "ts-dedent";
3+
import { configFileName, formatConfigSnippet } from "../config";
34
import { UserError } from "../errors";
45
import { logger } from "../logger";
6+
import { sniffUserAgent } from "../package-manager";
57
import guessWorkerFormat from "./guess-worker-format";
68
import {
79
resolveEntryWithAssets,
@@ -10,7 +12,7 @@ import {
1012
resolveEntryWithScript,
1113
} from "./resolve-entry";
1214
import { runCustomBuild } from "./run-custom-build";
13-
import type { Config } from "../config";
15+
import type { Config, RawConfig } from "../config";
1416
import type { DurableObjectBindings } from "../config/environment";
1517
import type { CfScriptFormat } from "./worker";
1618

@@ -77,8 +79,48 @@ export async function getEntry(
7779
"For Pages, please run `wrangler pages dev` instead."
7880
);
7981
}
82+
83+
const compatibilityDateStr = [
84+
new Date().getFullYear(),
85+
(new Date().getMonth() + 1 + "").padStart(2, "0"),
86+
(new Date().getDate() + "").padStart(2, "0"),
87+
].join("-");
88+
89+
const updateConfigMessage = (snippet: RawConfig) => dedent`
90+
${
91+
config.configPath
92+
? `add the following to your "${configFileName(config.configPath)}" file:`
93+
: `create a "wrangler.jsonc" file containing:`
94+
}
95+
96+
\`\`\`
97+
${formatConfigSnippet(
98+
{
99+
...(config.name ? {} : { name: "worker-name" }),
100+
...(config.compatibility_date
101+
? {}
102+
: { compatibility_date: compatibilityDateStr }),
103+
...snippet,
104+
},
105+
config.configPath
106+
)}
107+
\`\`\`
108+
109+
`;
110+
111+
const fullCommand = `${getNpxEquivalent()} wrangler ${command}`;
80112
throw new UserError(
81-
`Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler ${command} path/to/script\`) or the \`main\` config field.`
113+
dedent`
114+
Missing entry-point to Worker script or to assets directory
115+
116+
If there is code to deploy, you can either:
117+
- Specify an entry-point to your Worker script via the command line (ex: \`${fullCommand} src/index.ts\`)
118+
- Or ${updateConfigMessage({ main: "src/index.ts" })}
119+
120+
If are uploading a directory of assets, you can either:
121+
- Specify the path to the directory of assets via the command line: (ex: \`${fullCommand} --assets=./dist\`)
122+
- Or ${updateConfigMessage({ assets: { directory: "./dist" } })}`,
123+
{ telemetryMessage: "missing worker entrypoint or assets directory" }
82124
);
83125
}
84126
await runCustomBuild(
@@ -175,3 +217,15 @@ function generateAddScriptNameExamples(
175217
})
176218
.join("\n");
177219
}
220+
221+
export function getNpxEquivalent() {
222+
switch (sniffUserAgent()) {
223+
case "pnpm":
224+
return "pnpm";
225+
case "yarn":
226+
return "yarn";
227+
case "npm":
228+
default:
229+
return "npx";
230+
}
231+
}

0 commit comments

Comments
 (0)