Skip to content

Commit 9d4ff5b

Browse files
V3 Backport [#7423]: Clarify custom build logging (#8957)
* Skip double custom build * Fix logging * update snapshots * Create thirty-bears-appear.md --------- Co-authored-by: Samuel Macleod <[email protected]>
1 parent 56a0d6e commit 9d4ff5b

File tree

6 files changed

+50
-23
lines changed

6 files changed

+50
-23
lines changed

.changeset/thirty-bears-appear.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+
Make sure custom build logging output is more clearly signposted, and make sure it doesn't interfere with the interactive dev session output.

packages/wrangler/e2e/dev.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,9 +1104,9 @@ describe("custom builds", () => {
11041104
const worker = helper.runLongLived("wrangler dev");
11051105

11061106
// first build on startup
1107-
await worker.readUntil(/Running custom build/, 5_000);
1107+
await worker.readUntil(/\[custom build\] Running/, 5_000);
11081108
// second build for first watcher notification (can be optimised away, leaving as-is for now)
1109-
await worker.readUntil(/Running custom build/, 5_000);
1109+
await worker.readUntil(/\[custom build\] Running/, 5_000);
11101110

11111111
// Need to get the url in this order because waitForReady calls readUntil
11121112
// which keeps track of where it's read up to so far,
@@ -1118,7 +1118,7 @@ describe("custom builds", () => {
11181118
// assert no more custom builds happen
11191119
// regression: https://github.com/cloudflare/workers-sdk/issues/6876
11201120
await expect(
1121-
worker.readUntil(/Running custom build:/, 5_000)
1121+
worker.readUntil(/\[custom build\] Running/, 5_000)
11221122
).rejects.toThrowError();
11231123

11241124
// now check assets are still fetchable, even after updates

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

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

70127012
await runWrangler("deploy index.js");
70137013
expect(std.out).toMatchInlineSnapshot(`
7014-
"Running custom build: node -e \\"4+4; require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\\"
7014+
"[custom build] Running: node -e \\"4+4; require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\\"
70157015
Total Upload: xx KiB / gzip: xx KiB
70167016
Worker Startup Time: 100 ms
70177017
No bindings found.
@@ -7039,7 +7039,7 @@ addEventListener('fetch', event => {});`
70397039

70407040
await runWrangler("deploy index.js");
70417041
expect(std.out).toMatchInlineSnapshot(`
7042-
"Running custom build: echo \\"export default { fetch(){ return new Response(123) } }\\" > index.js
7042+
"[custom build] Running: echo \\"export default { fetch(){ return new Response(123) } }\\" > index.js
70437043
Total Upload: xx KiB / gzip: xx KiB
70447044
Worker Startup Time: 100 ms
70457045
No bindings found.
@@ -7067,9 +7067,9 @@ addEventListener('fetch', event => {});`
70677067
The \`main\` property in your wrangler.toml file should point to the file generated by the custom build.]
70687068
`);
70697069
expect(std.out).toMatchInlineSnapshot(`
7070-
"Running custom build: node -e \\"4+4;\\"
7071-
"
7072-
`);
7070+
"[custom build] Running: node -e \\"4+4;\\"
7071+
"
7072+
`);
70737073
expect(std.err).toMatchInlineSnapshot(`
70747074
"X [ERROR] The expected output file at \\"index.js\\" was not found after running custom build: node -e \\"4+4;\\".
70757075
@@ -7106,9 +7106,9 @@ addEventListener('fetch', event => {});`
71067106
\`\`\`]
71077107
`);
71087108
expect(std.out).toMatchInlineSnapshot(`
7109-
"Running custom build: node -e \\"4+4;\\"
7110-
"
7111-
`);
7109+
"[custom build] Running: node -e \\"4+4;\\"
7110+
"
7111+
`);
71127112
expect(std.err).toMatchInlineSnapshot(`
71137113
"X [ERROR] The expected output file at \\".\\" was not found after running custom build: node -e \\"4+4;\\".
71147114

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ describe.sequential("wrangler dev", () => {
887887
});
888888
expect(std.out).toMatchInlineSnapshot(
889889
`
890-
"Running custom build: node -e \\"4+4; require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\\"
890+
"[custom build] Running: node -e \\"4+4; require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\\"
891891
No bindings found.
892892
"
893893
`
@@ -912,7 +912,7 @@ describe.sequential("wrangler dev", () => {
912912

913913
expect(std.out).toMatchInlineSnapshot(
914914
`
915-
"Running custom build: echo \\"export default { fetch(){ return new Response(123) } }\\" > index.js
915+
"[custom build] Running: echo \\"export default { fetch(){ return new Response(123) } }\\" > index.js
916916
No bindings found.
917917
"
918918
`
@@ -934,9 +934,9 @@ describe.sequential("wrangler dev", () => {
934934
The \`main\` property in your wrangler.toml file should point to the file generated by the custom build.]
935935
`);
936936
expect(std.out).toMatchInlineSnapshot(`
937-
"Running custom build: node -e \\"4+4;\\"
938-
"
939-
`);
937+
"[custom build] Running: node -e \\"4+4;\\"
938+
"
939+
`);
940940
expect(std.err).toMatchInlineSnapshot(`
941941
"X [ERROR] The expected output file at \\"index.js\\" was not found after running custom build: node -e \\"4+4;\\".
942942

packages/wrangler/src/api/startDevWorker/BundlerController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export class BundlerController extends Controller<BundlerControllerEventMap> {
204204

205205
this.#customBuildWatcher = watch(pathsToWatch, {
206206
persistent: true,
207-
// TODO: add comments re this ans ready
207+
// The initial custom build is always done in getEntry()
208208
ignoreInitial: true,
209209
});
210210
this.#customBuildWatcher.on("ready", () => {

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { existsSync, statSync } from "node:fs";
22
import path from "node:path";
3+
import { Writable } from "node:stream";
4+
import chalk from "chalk";
35
import { execaCommand } from "execa";
46
import { configFileName } from "../config";
57
import { UserError } from "../errors";
@@ -19,17 +21,37 @@ export async function runCustomBuild(
1921
configPath: string | undefined
2022
) {
2123
if (build.command) {
22-
logger.log("Running custom build:", build.command);
24+
logger.log(chalk.blue("[custom build]"), "Running:", build.command);
2325
try {
24-
await execaCommand(build.command, {
26+
const res = execaCommand(build.command, {
2527
shell: true,
26-
// we keep these two as "inherit" so that
27-
// logs are still visible.
28-
stdout: "inherit",
29-
stderr: "inherit",
3028
...(build.cwd && { cwd: build.cwd }),
3129
});
30+
res.stdout?.pipe(
31+
new Writable({
32+
write(chunk: Buffer, _, callback) {
33+
const lines = chunk.toString().split("\n");
34+
for (const line of lines) {
35+
logger.log(chalk.blue("[custom build]"), line);
36+
}
37+
callback();
38+
},
39+
})
40+
);
41+
res.stderr?.pipe(
42+
new Writable({
43+
write(chunk: Buffer, _, callback) {
44+
const lines = chunk.toString().split("\n");
45+
for (const line of lines) {
46+
logger.log(chalk.red("[custom build]"), line);
47+
}
48+
callback();
49+
},
50+
})
51+
);
52+
await res;
3253
} catch (e) {
54+
logger.error(e);
3355
throw new UserError(
3456
`Running custom build \`${build.command}\` failed. There are likely more logs from your build command above.`,
3557
{

0 commit comments

Comments
 (0)