Skip to content

Commit 62d5471

Browse files
Improve the error messaging when the user provides neither an entry point nor an asset directory (#8534)
* Improve the error messaging when the user provides neither an entry point nor an asset directory * copy edits
1 parent f3db430 commit 62d5471

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";
@@ -2536,22 +2537,70 @@ addEventListener('fetch', event => {});`
25362537
});
25372538

25382539
it("should error if there is no entry-point specified", async () => {
2540+
vi.mocked(sniffUserAgent).mockReturnValue("npm");
25392541
writeWranglerConfig();
25402542
writeWorkerSource();
25412543
mockUploadWorkerRequest();
25422544
mockSubDomainRequest();
25432545
await expect(
25442546
runWrangler("deploy")
25452547
).rejects.toThrowErrorMatchingInlineSnapshot(
2546-
`[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.]`
2548+
`
2549+
[Error: Missing entry-point to Worker script or to assets directory
2550+
2551+
If there is code to deploy, you can either:
2552+
- Specify an entry-point to your Worker script via the command line (ex: \`npx wrangler deploy src/index.ts\`)
2553+
- Or add the following to your "wrangler.toml" file:
2554+
2555+
\`\`\`
2556+
main = "src/index.ts"
2557+
2558+
\`\`\`
2559+
2560+
2561+
If are uploading a directory of assets, you can either:
2562+
- Specify the path to the directory of assets via the command line: (ex: \`npx wrangler deploy --assets=./dist\`)
2563+
- Or add the following to your "wrangler.toml" file:
2564+
2565+
\`\`\`
2566+
[assets]
2567+
directory = "./dist"
2568+
2569+
\`\`\`
2570+
]
2571+
`
25472572
);
25482573

25492574
expect(std.out).toMatchInlineSnapshot(`""`);
25502575
expect(std.err).toMatchInlineSnapshot(`
2551-
"[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
2576+
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mMissing entry-point to Worker script or to assets directory[0m
25522577
2553-
"
2554-
`);
2578+
2579+
If there is code to deploy, you can either:
2580+
- Specify an entry-point to your Worker script via the command line (ex: \`npx wrangler deploy
2581+
src/index.ts\`)
2582+
- Or add the following to your \\"wrangler.toml\\" file:
2583+
2584+
\`\`\`
2585+
main = \\"src/index.ts\\"
2586+
2587+
\`\`\`
2588+
2589+
2590+
If are uploading a directory of assets, you can either:
2591+
- Specify the path to the directory of assets via the command line: (ex: \`npx wrangler deploy
2592+
--assets=./dist\`)
2593+
- Or add the following to your \\"wrangler.toml\\" file:
2594+
2595+
\`\`\`
2596+
[assets]
2597+
directory = \\"./dist\\"
2598+
2599+
\`\`\`
2600+
2601+
2602+
"
2603+
`);
25552604
});
25562605

25572606
describe("should source map validation errors", () => {

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

@@ -70,8 +72,48 @@ export async function getEntry(
7072
"For Pages, please run `wrangler pages dev` instead."
7173
);
7274
}
75+
76+
const compatibilityDateStr = [
77+
new Date().getFullYear(),
78+
(new Date().getMonth() + 1 + "").padStart(2, "0"),
79+
(new Date().getDate() + "").padStart(2, "0"),
80+
].join("-");
81+
82+
const updateConfigMessage = (snippet: RawConfig) => dedent`
83+
${
84+
config.configPath
85+
? `add the following to your "${configFileName(config.configPath)}" file:`
86+
: `create a "wrangler.jsonc" file containing:`
87+
}
88+
89+
\`\`\`
90+
${formatConfigSnippet(
91+
{
92+
...(config.name ? {} : { name: "worker-name" }),
93+
...(config.compatibility_date
94+
? {}
95+
: { compatibility_date: compatibilityDateStr }),
96+
...snippet,
97+
},
98+
config.configPath
99+
)}
100+
\`\`\`
101+
102+
`;
103+
104+
const fullCommand = `${getNpxEquivalent()} wrangler ${command}`;
73105
throw new UserError(
74-
`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.`
106+
dedent`
107+
Missing entry-point to Worker script or to assets directory
108+
109+
If there is code to deploy, you can either:
110+
- Specify an entry-point to your Worker script via the command line (ex: \`${fullCommand} src/index.ts\`)
111+
- Or ${updateConfigMessage({ main: "src/index.ts" })}
112+
113+
If are uploading a directory of assets, you can either:
114+
- Specify the path to the directory of assets via the command line: (ex: \`${fullCommand} --assets=./dist\`)
115+
- Or ${updateConfigMessage({ assets: { directory: "./dist" } })}`,
116+
{ telemetryMessage: "missing worker entrypoint or assets directory" }
75117
);
76118
}
77119
await runCustomBuild(
@@ -167,3 +209,15 @@ function generateAddScriptNameExamples(
167209
})
168210
.join("\n");
169211
}
212+
213+
export function getNpxEquivalent() {
214+
switch (sniffUserAgent()) {
215+
case "pnpm":
216+
return "pnpm";
217+
case "yarn":
218+
return "yarn";
219+
case "npm":
220+
default:
221+
return "npx";
222+
}
223+
}

0 commit comments

Comments
 (0)