Skip to content

Commit 2be85d7

Browse files
authored
Clarify custom build logging (#7423)
* Skip double custom build * Fix logging * update snapshots * Create thirty-bears-appear.md
1 parent 9c15b6e commit 2be85d7

6 files changed

Lines changed: 50 additions & 23 deletions

File tree

.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
@@ -1118,9 +1118,9 @@ describe("custom builds", () => {
11181118
const worker = helper.runLongLived("wrangler dev");
11191119

11201120
// first build on startup
1121-
await worker.readUntil(/Running custom build/, 5_000);
1121+
await worker.readUntil(/\[custom build\] Running/, 5_000);
11221122
// second build for first watcher notification (can be optimised away, leaving as-is for now)
1123-
await worker.readUntil(/Running custom build/, 5_000);
1123+
await worker.readUntil(/\[custom build\] Running/, 5_000);
11241124

11251125
// Need to get the url in this order because waitForReady calls readUntil
11261126
// which keeps track of where it's read up to so far,
@@ -1132,7 +1132,7 @@ describe("custom builds", () => {
11321132
// assert no more custom builds happen
11331133
// regression: https://github.com/cloudflare/workers-sdk/issues/6876
11341134
await expect(
1135-
worker.readUntil(/Running custom build:/, 5_000)
1135+
worker.readUntil(/\[custom build\] Running/, 5_000)
11361136
).rejects.toThrowError();
11371137

11381138
// 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
@@ -6381,7 +6381,7 @@ addEventListener('fetch', event => {});`
63816381

63826382
await runWrangler("deploy index.js");
63836383
expect(std.out).toMatchInlineSnapshot(`
6384-
"Running custom build: node -e \\"4+4; require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\\"
6384+
"[custom build] Running: node -e \\"4+4; require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\\"
63856385
Total Upload: xx KiB / gzip: xx KiB
63866386
Worker Startup Time: 100 ms
63876387
No bindings found.
@@ -6409,7 +6409,7 @@ addEventListener('fetch', event => {});`
64096409

64106410
await runWrangler("deploy index.js");
64116411
expect(std.out).toMatchInlineSnapshot(`
6412-
"Running custom build: echo \\"export default { fetch(){ return new Response(123) } }\\" > index.js
6412+
"[custom build] Running: echo \\"export default { fetch(){ return new Response(123) } }\\" > index.js
64136413
Total Upload: xx KiB / gzip: xx KiB
64146414
Worker Startup Time: 100 ms
64156415
No bindings found.
@@ -6437,9 +6437,9 @@ addEventListener('fetch', event => {});`
64376437
The \`main\` property in your wrangler.toml file should point to the file generated by the custom build.]
64386438
`);
64396439
expect(std.out).toMatchInlineSnapshot(`
6440-
"Running custom build: node -e \\"4+4;\\"
6441-
"
6442-
`);
6440+
"[custom build] Running: node -e \\"4+4;\\"
6441+
"
6442+
`);
64436443
expect(std.err).toMatchInlineSnapshot(`
64446444
"X [ERROR] The expected output file at \\"index.js\\" was not found after running custom build: node -e \\"4+4;\\".
64456445
@@ -6476,9 +6476,9 @@ addEventListener('fetch', event => {});`
64766476
\`\`\`]
64776477
`);
64786478
expect(std.out).toMatchInlineSnapshot(`
6479-
"Running custom build: node -e \\"4+4;\\"
6480-
"
6481-
`);
6479+
"[custom build] Running: node -e \\"4+4;\\"
6480+
"
6481+
`);
64826482
expect(std.err).toMatchInlineSnapshot(`
64836483
"X [ERROR] The expected output file at \\".\\" was not found after running custom build: node -e \\"4+4;\\".
64846484

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
@@ -197,7 +197,7 @@ export class BundlerController extends Controller<BundlerControllerEventMap> {
197197

198198
this.#customBuildWatcher = watch(pathsToWatch, {
199199
persistent: true,
200-
// TODO: add comments re this ans ready
200+
// The initial custom build is always done in getEntry()
201201
ignoreInitial: true,
202202
});
203203
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)