Skip to content

Commit c7164b0

Browse files
fix: improve error message when the entrypoint is incorrect
1 parent ffc30d3 commit c7164b0

File tree

5 files changed

+46
-28
lines changed

5 files changed

+46
-28
lines changed

.changeset/brave-geckos-juggle.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+
fix: improve error message when the entrypoint is incorrect

fixtures/start-worker-node-test/src/config-errors.test.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import assert from "node:assert";
22
import test, { describe } from "node:test";
3-
import { setTimeout } from "node:timers/promises";
43
import getPort from "get-port";
54
import { unstable_startWorker } from "wrangler";
65

@@ -20,7 +19,7 @@ describe("startWorker - configuration errors", () => {
2019
assert(cause instanceof Error);
2120
assert.match(
2221
cause.message,
23-
/The entry-point file at "not a real entrypoint" was not found./
22+
/The entry-point file at "not a real entrypoint" was not found\./
2423
);
2524
return true;
2625
}
@@ -88,9 +87,9 @@ describe("startWorker - configuration errors", () => {
8887
worker.setConfig({ entrypoint: "not a real entrypoint" }, true),
8988
(err) => {
9089
assert(err instanceof Error);
91-
assert.strictEqual(
90+
assert.match(
9291
err.message,
93-
'The entry-point file at "not a real entrypoint" was not found.'
92+
/The entry-point file at "not a real entrypoint" was not found\./
9493
);
9594
return true;
9695
}

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3762,8 +3762,7 @@ addEventListener('fetch', event => {});`
37623762

37633763
await expect(runWrangler("deploy ./assets")).rejects
37643764
.toThrowErrorMatchingInlineSnapshot(`
3765-
[Error: The entry-point file at "assets" was not found.
3766-
The provided entry-point path, "assets", points to a directory, rather than a file.
3765+
[Error: The provided entry-point path, "assets", points to a directory, rather than a file.
37673766
37683767
If you want to deploy a directory of static assets, you can do so by using the \`--assets\` flag. For example:
37693768
@@ -8091,9 +8090,7 @@ addEventListener('fetch', event => {});`
80918090

80928091
await expect(runWrangler("deploy")).rejects
80938092
.toThrowErrorMatchingInlineSnapshot(`
8094-
[Error: The expected output file at "." was not found after running custom build: node -e "4+4;".
8095-
The \`main\` property in your wrangler.toml file should point to the file generated by the custom build.
8096-
The provided entry-point path, ".", points to a directory, rather than a file.
8093+
[Error: The provided entry-point path, ".", points to a directory, rather than a file.
80978094
80988095
Did you mean to set the main field to one of:
80998096
\`\`\`
@@ -8109,11 +8106,8 @@ addEventListener('fetch', event => {});`
81098106
"
81108107
`);
81118108
expect(std.err).toMatchInlineSnapshot(`
8112-
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mThe expected output file at \\".\\" was not found after running custom build: node -e \\"4+4;\\".[0m
8109+
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mThe provided entry-point path, \\".\\", points to a directory, rather than a file.[0m
81138110
8114-
The \`main\` property in your wrangler.toml file should point to the file generated by the custom
8115-
build.
8116-
The provided entry-point path, \\".\\", points to a directory, rather than a file.
81178111
81188112
Did you mean to set the main field to one of:
81198113
\`\`\`

packages/wrangler/src/__tests__/startup-profiling.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ describe("wrangler check startup", () => {
6161
await expect(
6262
runWrangler("check startup --args 'abc'")
6363
).rejects.toThrowErrorMatchingInlineSnapshot(
64-
`[Error: The entry-point file at "abc" was not found.]`
64+
`
65+
[Error: The entry-point file at "abc" was not found.
66+
67+
This might mean that your entry-point file needs to be generated (which is the general case when a framework is being used).
68+
If that's the case please run your project's build command and try again.]
69+
`
6570
);
6671
});
6772

packages/wrangler/src/deployment-bundle/run-custom-build.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Writable } from "node:stream";
44
import { configFileName, UserError } from "@cloudflare/workers-utils";
55
import chalk from "chalk";
66
import { execaCommand } from "execa";
7+
import dedent from "ts-dedent";
78
import { logger } from "../logger";
89
import type { Config } from "@cloudflare/workers-utils";
910

@@ -69,15 +70,11 @@ export async function runCustomBuild(
6970
assertEntryPointExists(
7071
expectedEntryAbsolute,
7172
expectedEntryRelative,
72-
`The expected output file at "${expectedEntryRelative}" was not found after running custom build: ${build.command}.\n` +
73-
`The \`main\` property in your ${configFileName(configPath)} file should point to the file generated by the custom build.`
73+
build.command,
74+
configPath
7475
);
7576
} else {
76-
assertEntryPointExists(
77-
expectedEntryAbsolute,
78-
expectedEntryRelative,
79-
`The entry-point file at "${expectedEntryRelative}" was not found.`
80-
);
77+
assertEntryPointExists(expectedEntryAbsolute, expectedEntryRelative);
8178
}
8279
}
8380

@@ -87,14 +84,16 @@ export async function runCustomBuild(
8784
function assertEntryPointExists(
8885
expectedEntryAbsolute: string,
8986
expectedEntryRelative: string,
90-
errorMessage: string
87+
customBuildCommand?: string,
88+
configPath?: string
9189
) {
9290
if (!fileExists(expectedEntryAbsolute)) {
9391
throw new UserError(
9492
getMissingEntryPointMessage(
95-
errorMessage,
9693
expectedEntryAbsolute,
97-
expectedEntryRelative
94+
expectedEntryRelative,
95+
customBuildCommand,
96+
configPath
9897
)
9998
);
10099
}
@@ -107,16 +106,17 @@ function assertEntryPointExists(
107106
* nearby to the expected file path.
108107
*/
109108
function getMissingEntryPointMessage(
110-
message: string,
111109
absoluteEntryPointPath: string,
112-
relativeEntryPointPath: string
110+
relativeEntryPointPath: string,
111+
customBuildCommand?: string,
112+
configPath?: string
113113
): string {
114114
if (
115115
existsSync(absoluteEntryPointPath) &&
116116
statSync(absoluteEntryPointPath).isDirectory()
117117
) {
118118
// The expected entry-point is a directory, so offer further guidance.
119-
message += `\nThe provided entry-point path, "${relativeEntryPointPath}", points to a directory, rather than a file.\n`;
119+
let message = `The provided entry-point path, "${relativeEntryPointPath}", points to a directory, rather than a file.\n`;
120120

121121
// Perhaps we can even guess what the correct path should be...
122122
const possiblePaths: string[] = [];
@@ -147,8 +147,23 @@ function getMissingEntryPointMessage(
147147
`\n If you want to deploy a directory of static assets, you can do so by using the \`--assets\` flag. For example:\n\n` +
148148
`wrangler deploy --assets=./${relativeEntryPointPath}\n`;
149149
}
150+
151+
return message;
150152
}
151-
return message;
153+
154+
if (customBuildCommand) {
155+
return dedent`
156+
The expected output file at "${relativeEntryPointPath}" was not found after running custom build: ${customBuildCommand}.
157+
The \`main\` property in your ${configFileName(configPath)} file should point to the file generated by the custom build.
158+
`;
159+
}
160+
161+
return dedent`
162+
The entry-point file at "${relativeEntryPointPath}" was not found.
163+
164+
This might mean that your entry-point file needs to be generated (which is the general case when a framework is being used).
165+
If that's the case please run your project's build command and try again.
166+
`;
152167
}
153168

154169
/**

0 commit comments

Comments
 (0)