From 0ddcef85c7009b0171056dc348d3d6dfebf843ef Mon Sep 17 00:00:00 2001 From: Andy Jessop Date: Mon, 14 Oct 2024 18:31:56 +0200 Subject: [PATCH 1/3] chore: fix entrypoint logic and refactor --- packages/wrangler-devtools/.gclient | 10 +++ packages/wrangler-devtools/.gclient_entries | 13 ++++ .../.gclient_previous_sync_commits | 1 + packages/wrangler-devtools/.gcs_entries | 50 +++++++++++++++ .../wrangler/src/deployment-bundle/entry.ts | 62 +++++++++---------- .../src/deployment-bundle/resolve-entry.ts | 48 ++++++++++++++ 6 files changed, 152 insertions(+), 32 deletions(-) create mode 100644 packages/wrangler-devtools/.gclient create mode 100644 packages/wrangler-devtools/.gclient_entries create mode 100644 packages/wrangler-devtools/.gclient_previous_sync_commits create mode 100644 packages/wrangler-devtools/.gcs_entries create mode 100644 packages/wrangler/src/deployment-bundle/resolve-entry.ts diff --git a/packages/wrangler-devtools/.gclient b/packages/wrangler-devtools/.gclient new file mode 100644 index 000000000000..ccd177ee324a --- /dev/null +++ b/packages/wrangler-devtools/.gclient @@ -0,0 +1,10 @@ +solutions = [ + { "name" : 'devtools-frontend', + "url" : 'https://chromium.googlesource.com/devtools/devtools-frontend', + "deps_file" : 'DEPS', + "managed" : False, + "custom_deps" : { + }, + "custom_vars": {}, + }, +] diff --git a/packages/wrangler-devtools/.gclient_entries b/packages/wrangler-devtools/.gclient_entries new file mode 100644 index 000000000000..c43321de7c2b --- /dev/null +++ b/packages/wrangler-devtools/.gclient_entries @@ -0,0 +1,13 @@ +entries = { + 'devtools-frontend': 'https://chromium.googlesource.com/devtools/devtools-frontend', + 'devtools-frontend/build': 'https://chromium.googlesource.com/chromium/src/build.git@a96ee5fdfbad3196c2ae3dcfa1c0c93b4acd0bde', + 'devtools-frontend/buildtools': 'https://chromium.googlesource.com/chromium/src/buildtools.git@9807e11fd066b39cdf9c36db587494521ac300bb', + 'devtools-frontend/buildtools/mac:gn/gn/mac-${arch}': 'https://chrome-infra-packages.appspot.com/gn/gn/mac-${arch}@git_revision:95b0f8fe31a992a33c040bbe3867901335c12762', + 'devtools-frontend/third_party/clang-format/script': 'https://chromium.googlesource.com/external/github.com/llvm/llvm-project/clang/tools/clang-format.git@3c0acd2d4e73dd911309d9e970ba09d58bf23a62', + 'devtools-frontend/third_party/depot_tools': 'https://chromium.googlesource.com/chromium/tools/depot_tools.git@a31b55235fa0e02cdd11e9e311a3da11fd53d473', + 'devtools-frontend/third_party/esbuild:infra/3pp/tools/esbuild/${platform}': 'https://chrome-infra-packages.appspot.com/infra/3pp/tools/esbuild/${platform}@version:2@0.14.13.chromium.2', + 'devtools-frontend/third_party/inspector_protocol': 'https://chromium.googlesource.com/deps/inspector_protocol@ccf6613682a24413db28656da6f9c52948331bad', + 'devtools-frontend/third_party/ninja:infra/3pp/tools/ninja/${platform}': 'https://chrome-infra-packages.appspot.com/infra/3pp/tools/ninja/${platform}@version:3@1.12.1.chromium.4', + 'devtools-frontend/third_party/node/mac_arm64:20.11.0/5b5681e12a21cda986410f69e03e6220a21dd4d2': 'gs://chromium-nodejs/20.11.0/5b5681e12a21cda986410f69e03e6220a21dd4d2', + 'devtools-frontend/buildtools/mac_arm64-format:96c34e77259c4cc1fc7bdf067fc058bfd341ab85': 'gs://chromium-clang-format/96c34e77259c4cc1fc7bdf067fc058bfd341ab85', +} diff --git a/packages/wrangler-devtools/.gclient_previous_sync_commits b/packages/wrangler-devtools/.gclient_previous_sync_commits new file mode 100644 index 000000000000..9ce05698a93d --- /dev/null +++ b/packages/wrangler-devtools/.gclient_previous_sync_commits @@ -0,0 +1 @@ +{"devtools-frontend": "e5652b6f5692ec299bb06502923e37bbda64212d", "devtools-frontend/build": "a96ee5fdfbad3196c2ae3dcfa1c0c93b4acd0bde", "devtools-frontend/buildtools": "9807e11fd066b39cdf9c36db587494521ac300bb", "devtools-frontend/third_party/clang-format/script": "3c0acd2d4e73dd911309d9e970ba09d58bf23a62", "devtools-frontend/third_party/depot_tools": "a31b55235fa0e02cdd11e9e311a3da11fd53d473", "devtools-frontend/third_party/inspector_protocol": "ccf6613682a24413db28656da6f9c52948331bad"} \ No newline at end of file diff --git a/packages/wrangler-devtools/.gcs_entries b/packages/wrangler-devtools/.gcs_entries new file mode 100644 index 000000000000..c6b0af70213a --- /dev/null +++ b/packages/wrangler-devtools/.gcs_entries @@ -0,0 +1,50 @@ +{ + "devtools-frontend": { + "devtools-frontend/third_party/node/linux": [ + "20.11.0/f9a337cfa0e2b92d3e5c671c26b454bd8e99769e" + ], + "devtools-frontend/third_party/node/mac": [ + "20.11.0/e3c0fd53caae857309815f3f8de7c2dce49d7bca" + ], + "devtools-frontend/third_party/node/mac_arm64": [ + "20.11.0/5b5681e12a21cda986410f69e03e6220a21dd4d2" + ], + "devtools-frontend/third_party/node/win": [ + "20.11.0/2cb36010af52bc5e2a2d1e3675c10361c80d8f8d" + ] + }, + "devtools-frontend/build": { + "devtools-frontend/build/linux/debian_bullseye_amd64-sysroot": [ + "dec7a3a0fc5b83b909cba1b6d119077e0429a138eadef6bf5a0f2e03b1904631" + ], + "devtools-frontend/build/linux/debian_bullseye_arm64-sysroot": [ + "308e23faba3174bd01accfe358467b8a40fad4db4c49ef629da30219f65a275f" + ], + "devtools-frontend/build/linux/debian_bullseye_armhf-sysroot": [ + "fe81e7114b97440262bce004caf02c1514732e2fa7f99693b2836932ad1c4626" + ], + "devtools-frontend/build/linux/debian_bullseye_i386-sysroot": [ + "b53933120bb08ffc38140a817e3f0f99782254a6bf9622271574fa004e8783a4" + ], + "devtools-frontend/build/linux/debian_bullseye_mips64el-sysroot": [ + "783cb79f26736c69e8125788d95ffb65a28172349009d75188838a004280a92b" + ], + "devtools-frontend/build/linux/debian_bullseye_mipsel-sysroot": [ + "fcf8c3931476dd097c58f2f5d44621c7090b135e85ab56885aa4b44f4bd6cdb5" + ] + }, + "devtools-frontend/buildtools": { + "devtools-frontend/buildtools/win-format": [ + "49458d4c1e884a38308f8dc6a2c7eb55fc478755" + ], + "devtools-frontend/buildtools/mac-format": [ + "0b4bd257a1f4cd27d27d6919b0f9e52ecdfa8f1e" + ], + "devtools-frontend/buildtools/mac_arm64-format": [ + "96c34e77259c4cc1fc7bdf067fc058bfd341ab85" + ], + "devtools-frontend/buildtools/linux64-format": [ + "b42097ca924d1f1736a5a7806068fed9d7345eb4" + ] + } +} \ No newline at end of file diff --git a/packages/wrangler/src/deployment-bundle/entry.ts b/packages/wrangler/src/deployment-bundle/entry.ts index 6ded3b4f6a8d..8547ff368584 100644 --- a/packages/wrangler/src/deployment-bundle/entry.ts +++ b/packages/wrangler/src/deployment-bundle/entry.ts @@ -1,8 +1,13 @@ import path from "node:path"; import { UserError } from "../errors"; import { logger } from "../logger"; -import { getBasePath } from "../paths"; import guessWorkerFormat from "./guess-worker-format"; +import { + resolveEntryWithAssets, + resolveEntryWithEntryPoint, + resolveEntryWithMain, + resolveEntryWithScript, +} from "./resolve-entry"; import { runCustomBuild } from "./run-custom-build"; import type { Config } from "../config"; import type { DurableObjectBindings } from "../config/environment"; @@ -42,41 +47,33 @@ export async function getEntry( config: Config, command: "dev" | "deploy" | "versions upload" | "types" ): Promise { - let file: string; - let directory = process.cwd(); + const directory = process.cwd(); + const entryPoint = config.site?.["entry-point"]; + + let paths: { absolutePath: string; relativePath: string } | undefined; if (args.script) { - // If the script name comes from the command line it is relative to the current working directory. - file = path.resolve(args.script); - } else if (config.main === undefined) { - if (config.site?.["entry-point"]) { - directory = path.resolve(path.dirname(config.configPath ?? ".")); - file = path.extname(config.site?.["entry-point"]) - ? path.resolve(config.site?.["entry-point"]) - : // site.entry-point could be a directory - path.resolve(config.site?.["entry-point"], "index.js"); - } else if ( - args.legacyAssets || - config.legacy_assets || - args.assets || - config.assets - ) { - file = path.resolve(getBasePath(), "templates/no-op-worker.js"); - } else { - throw new UserError( - `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.` - ); - } + paths = resolveEntryWithScript(args.script); + } else if (config.main !== undefined) { + paths = resolveEntryWithMain(config.main, config.configPath); + } else if (entryPoint) { + paths = resolveEntryWithEntryPoint(entryPoint, config.configPath); + } else if ( + args.legacyAssets || + config.legacy_assets || + args.assets || + config.assets + ) { + paths = resolveEntryWithAssets(); } else { - directory = path.resolve(path.dirname(config.configPath ?? ".")); - file = path.resolve(directory, config.main); + throw new UserError( + `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.` + ); } - - const relativeFile = path.relative(directory, file) || "."; - await runCustomBuild(file, relativeFile, config.build); + await runCustomBuild(paths.absolutePath, paths.relativePath, config.build); const format = await guessWorkerFormat( - file, + paths.absolutePath, directory, args.format ?? config.build?.upload?.format, config.tsconfig @@ -110,10 +107,11 @@ export async function getEntry( } return { - file, + file: paths.absolutePath, directory, format, - moduleRoot: args.moduleRoot ?? config.base_dir ?? path.dirname(file), + moduleRoot: + args.moduleRoot ?? config.base_dir ?? path.dirname(paths.absolutePath), name: config.name ?? "worker", }; } diff --git a/packages/wrangler/src/deployment-bundle/resolve-entry.ts b/packages/wrangler/src/deployment-bundle/resolve-entry.ts new file mode 100644 index 000000000000..5a9efb535194 --- /dev/null +++ b/packages/wrangler/src/deployment-bundle/resolve-entry.ts @@ -0,0 +1,48 @@ +import path from "path"; +import { getBasePath } from "../paths"; + +export function resolveEntryWithScript(script: string): { + absolutePath: string; + relativePath: string; +} { + const file = path.resolve(script); + const relativePath = path.relative(process.cwd(), file) || "."; + return { absolutePath: file, relativePath }; +} + +export function resolveEntryWithMain( + main: string, + configPath?: string +): { + absolutePath: string; + relativePath: string; +} { + const directory = path.resolve(path.dirname(configPath ?? ".")); + const file = path.resolve(directory, main); + const relativePath = path.relative(directory, file) || "."; + return { absolutePath: file, relativePath }; +} + +export function resolveEntryWithEntryPoint( + entryPoint: string, + configPath?: string +): { + absolutePath: string; + relativePath: string; +} { + const directory = path.resolve(path.dirname(configPath ?? ".")); + const file = path.extname(entryPoint) + ? path.resolve(entryPoint) + : path.resolve(entryPoint, "index.js"); + const relativePath = path.relative(directory, file) || "."; + return { absolutePath: file, relativePath }; +} + +export function resolveEntryWithAssets(): { + absolutePath: string; + relativePath: string; +} { + const file = path.resolve(getBasePath(), "templates/no-op-worker.js"); + const relativePath = path.relative(process.cwd(), file) || "."; + return { absolutePath: file, relativePath }; +} From ea19e0b91504d1a1b375e8fdf0a5dd7a937c0d16 Mon Sep 17 00:00:00 2001 From: Andy Jessop Date: Tue, 15 Oct 2024 10:20:08 +0200 Subject: [PATCH 2/3] chore: remove spurious files --- packages/wrangler-devtools/.gclient | 10 ---- packages/wrangler-devtools/.gclient_entries | 13 ----- .../.gclient_previous_sync_commits | 1 - packages/wrangler-devtools/.gcs_entries | 50 ------------------- 4 files changed, 74 deletions(-) delete mode 100644 packages/wrangler-devtools/.gclient delete mode 100644 packages/wrangler-devtools/.gclient_entries delete mode 100644 packages/wrangler-devtools/.gclient_previous_sync_commits delete mode 100644 packages/wrangler-devtools/.gcs_entries diff --git a/packages/wrangler-devtools/.gclient b/packages/wrangler-devtools/.gclient deleted file mode 100644 index ccd177ee324a..000000000000 --- a/packages/wrangler-devtools/.gclient +++ /dev/null @@ -1,10 +0,0 @@ -solutions = [ - { "name" : 'devtools-frontend', - "url" : 'https://chromium.googlesource.com/devtools/devtools-frontend', - "deps_file" : 'DEPS', - "managed" : False, - "custom_deps" : { - }, - "custom_vars": {}, - }, -] diff --git a/packages/wrangler-devtools/.gclient_entries b/packages/wrangler-devtools/.gclient_entries deleted file mode 100644 index c43321de7c2b..000000000000 --- a/packages/wrangler-devtools/.gclient_entries +++ /dev/null @@ -1,13 +0,0 @@ -entries = { - 'devtools-frontend': 'https://chromium.googlesource.com/devtools/devtools-frontend', - 'devtools-frontend/build': 'https://chromium.googlesource.com/chromium/src/build.git@a96ee5fdfbad3196c2ae3dcfa1c0c93b4acd0bde', - 'devtools-frontend/buildtools': 'https://chromium.googlesource.com/chromium/src/buildtools.git@9807e11fd066b39cdf9c36db587494521ac300bb', - 'devtools-frontend/buildtools/mac:gn/gn/mac-${arch}': 'https://chrome-infra-packages.appspot.com/gn/gn/mac-${arch}@git_revision:95b0f8fe31a992a33c040bbe3867901335c12762', - 'devtools-frontend/third_party/clang-format/script': 'https://chromium.googlesource.com/external/github.com/llvm/llvm-project/clang/tools/clang-format.git@3c0acd2d4e73dd911309d9e970ba09d58bf23a62', - 'devtools-frontend/third_party/depot_tools': 'https://chromium.googlesource.com/chromium/tools/depot_tools.git@a31b55235fa0e02cdd11e9e311a3da11fd53d473', - 'devtools-frontend/third_party/esbuild:infra/3pp/tools/esbuild/${platform}': 'https://chrome-infra-packages.appspot.com/infra/3pp/tools/esbuild/${platform}@version:2@0.14.13.chromium.2', - 'devtools-frontend/third_party/inspector_protocol': 'https://chromium.googlesource.com/deps/inspector_protocol@ccf6613682a24413db28656da6f9c52948331bad', - 'devtools-frontend/third_party/ninja:infra/3pp/tools/ninja/${platform}': 'https://chrome-infra-packages.appspot.com/infra/3pp/tools/ninja/${platform}@version:3@1.12.1.chromium.4', - 'devtools-frontend/third_party/node/mac_arm64:20.11.0/5b5681e12a21cda986410f69e03e6220a21dd4d2': 'gs://chromium-nodejs/20.11.0/5b5681e12a21cda986410f69e03e6220a21dd4d2', - 'devtools-frontend/buildtools/mac_arm64-format:96c34e77259c4cc1fc7bdf067fc058bfd341ab85': 'gs://chromium-clang-format/96c34e77259c4cc1fc7bdf067fc058bfd341ab85', -} diff --git a/packages/wrangler-devtools/.gclient_previous_sync_commits b/packages/wrangler-devtools/.gclient_previous_sync_commits deleted file mode 100644 index 9ce05698a93d..000000000000 --- a/packages/wrangler-devtools/.gclient_previous_sync_commits +++ /dev/null @@ -1 +0,0 @@ -{"devtools-frontend": "e5652b6f5692ec299bb06502923e37bbda64212d", "devtools-frontend/build": "a96ee5fdfbad3196c2ae3dcfa1c0c93b4acd0bde", "devtools-frontend/buildtools": "9807e11fd066b39cdf9c36db587494521ac300bb", "devtools-frontend/third_party/clang-format/script": "3c0acd2d4e73dd911309d9e970ba09d58bf23a62", "devtools-frontend/third_party/depot_tools": "a31b55235fa0e02cdd11e9e311a3da11fd53d473", "devtools-frontend/third_party/inspector_protocol": "ccf6613682a24413db28656da6f9c52948331bad"} \ No newline at end of file diff --git a/packages/wrangler-devtools/.gcs_entries b/packages/wrangler-devtools/.gcs_entries deleted file mode 100644 index c6b0af70213a..000000000000 --- a/packages/wrangler-devtools/.gcs_entries +++ /dev/null @@ -1,50 +0,0 @@ -{ - "devtools-frontend": { - "devtools-frontend/third_party/node/linux": [ - "20.11.0/f9a337cfa0e2b92d3e5c671c26b454bd8e99769e" - ], - "devtools-frontend/third_party/node/mac": [ - "20.11.0/e3c0fd53caae857309815f3f8de7c2dce49d7bca" - ], - "devtools-frontend/third_party/node/mac_arm64": [ - "20.11.0/5b5681e12a21cda986410f69e03e6220a21dd4d2" - ], - "devtools-frontend/third_party/node/win": [ - "20.11.0/2cb36010af52bc5e2a2d1e3675c10361c80d8f8d" - ] - }, - "devtools-frontend/build": { - "devtools-frontend/build/linux/debian_bullseye_amd64-sysroot": [ - "dec7a3a0fc5b83b909cba1b6d119077e0429a138eadef6bf5a0f2e03b1904631" - ], - "devtools-frontend/build/linux/debian_bullseye_arm64-sysroot": [ - "308e23faba3174bd01accfe358467b8a40fad4db4c49ef629da30219f65a275f" - ], - "devtools-frontend/build/linux/debian_bullseye_armhf-sysroot": [ - "fe81e7114b97440262bce004caf02c1514732e2fa7f99693b2836932ad1c4626" - ], - "devtools-frontend/build/linux/debian_bullseye_i386-sysroot": [ - "b53933120bb08ffc38140a817e3f0f99782254a6bf9622271574fa004e8783a4" - ], - "devtools-frontend/build/linux/debian_bullseye_mips64el-sysroot": [ - "783cb79f26736c69e8125788d95ffb65a28172349009d75188838a004280a92b" - ], - "devtools-frontend/build/linux/debian_bullseye_mipsel-sysroot": [ - "fcf8c3931476dd097c58f2f5d44621c7090b135e85ab56885aa4b44f4bd6cdb5" - ] - }, - "devtools-frontend/buildtools": { - "devtools-frontend/buildtools/win-format": [ - "49458d4c1e884a38308f8dc6a2c7eb55fc478755" - ], - "devtools-frontend/buildtools/mac-format": [ - "0b4bd257a1f4cd27d27d6919b0f9e52ecdfa8f1e" - ], - "devtools-frontend/buildtools/mac_arm64-format": [ - "96c34e77259c4cc1fc7bdf067fc058bfd341ab85" - ], - "devtools-frontend/buildtools/linux64-format": [ - "b42097ca924d1f1736a5a7806068fed9d7345eb4" - ] - } -} \ No newline at end of file From c70c4eb5d1b37a83a64cdee0e95a88c670f146eb Mon Sep 17 00:00:00 2001 From: Andy Jessop Date: Tue, 15 Oct 2024 11:26:15 +0200 Subject: [PATCH 3/3] Add test case --- .../startDevWorker/ConfigController.test.ts | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/packages/wrangler/src/__tests__/api/startDevWorker/ConfigController.test.ts b/packages/wrangler/src/__tests__/api/startDevWorker/ConfigController.test.ts index ae4abd05adc7..e9cd0f67d1d0 100644 --- a/packages/wrangler/src/__tests__/api/startDevWorker/ConfigController.test.ts +++ b/packages/wrangler/src/__tests__/api/startDevWorker/ConfigController.test.ts @@ -56,6 +56,41 @@ describe("ConfigController", () => { }); }); + it("should apply module root to parent if main is nested from base_dir", async () => { + const controller = new ConfigController(); + const event = waitForConfigUpdate(controller); + await seed({ + "some/base_dir/nested/index.js": dedent/* javascript */ ` + export default { + fetch(request, env, ctx) { + return new Response("hello world") + } + } + `, + "wrangler.toml": dedent` + main = \"./some/base_dir/nested/index.js\" +base_dir = \"./some/base_dir\"`, + }); + + const config: StartDevWorkerInput = {}; + + await controller.set(config); + + await expect(event).resolves.toMatchObject({ + type: "configUpdate", + config: { + build: { + additionalModules: [], + define: {}, + format: "modules", + moduleRoot: path.join(process.cwd(), "./some/base_dir"), + moduleRules: [], + }, + directory: process.cwd(), + entrypoint: path.join(process.cwd(), "./some/base_dir/nested/index.js"), + }, + }); + }); it("should shallow merge patched config", async () => { const controller = new ConfigController(); const event1 = waitForConfigUpdate(controller);