From 4766c27c3f21c375eecea3d7d1cd9a7e19fff5b1 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Fri, 18 Mar 2022 10:25:03 +0000 Subject: [PATCH] fix: ensure asset keys are relative to the project root Previously, asset file paths were computed relative to the current working directory, even if we had used `-c` to run Wrangler on a project in a different directory to the current one. Now, assets file paths are computed relative to the "project root", which is either the directory containing the wrangler.toml or the current working directory if there is no config specified. --- .changeset/long-cameras-nail.md | 13 +++++++ packages/wrangler/src/dev/local.tsx | 2 +- packages/wrangler/src/sites.tsx | 58 ++++++++++++++++++++--------- 3 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 .changeset/long-cameras-nail.md diff --git a/.changeset/long-cameras-nail.md b/.changeset/long-cameras-nail.md new file mode 100644 index 000000000000..8c594ec11d8d --- /dev/null +++ b/.changeset/long-cameras-nail.md @@ -0,0 +1,13 @@ +--- +"wrangler": patch +--- + +fix: ensure asset keys are relative to the project root + +Previously, asset file paths were computed relative to the current working +directory, even if we had used `-c` to run Wrangler on a project in a different +directory to the current one. + +Now, assets file paths are computed relative to the "project root", which is +either the directory containing the wrangler.toml or the current working directory +if there is no config specified. diff --git a/packages/wrangler/src/dev/local.tsx b/packages/wrangler/src/dev/local.tsx index d7af121509a3..1a6316493ad0 100644 --- a/packages/wrangler/src/dev/local.tsx +++ b/packages/wrangler/src/dev/local.tsx @@ -119,7 +119,7 @@ function useLocalWorker({ ), durableObjectsPersist: enableLocalPersistence, cachePersist: !enableLocalPersistence, - sitePath: assetPaths?.baseDirectory, + sitePath: assetPaths?.assetDirectory, siteInclude: assetPaths?.includePatterns.length ? assetPaths?.includePatterns : undefined, diff --git a/packages/wrangler/src/sites.tsx b/packages/wrangler/src/sites.tsx index c658e456cd99..ebfb0ca2aaac 100644 --- a/packages/wrangler/src/sites.tsx +++ b/packages/wrangler/src/sites.tsx @@ -137,19 +137,24 @@ export async function syncAssets( const exclude = createPatternMatcher(siteAssets.excludePatterns, true); const hasher = await xxhash(); - for await (const file of getFilesInFolder(siteAssets.baseDirectory)) { - if (!include(file)) { + const assetDirectory = path.join( + siteAssets.baseDirectory, + siteAssets.assetDirectory + ); + for await (const absAssetFile of getFilesInFolder(assetDirectory)) { + const assetFile = path.relative(siteAssets.baseDirectory, absAssetFile); + if (!include(assetFile)) { continue; } - if (exclude(file)) { + if (exclude(assetFile)) { continue; } - await validateAssetSize(file); - console.log(`reading ${file}...`); - const content = await readFile(file, "base64"); + await validateAssetSize(absAssetFile, assetFile); + console.log(`reading ${assetFile}...`); + const content = await readFile(absAssetFile, "base64"); - const assetKey = hashAsset(hasher, file, content); + const assetKey = hashAsset(hasher, assetFile, content); validateAssetKey(assetKey); // now put each of the files into kv @@ -165,7 +170,7 @@ export async function syncAssets( } // remove the key from the set so we know we've seen it delete keyMap[assetKey]; - manifest[path.relative(siteAssets.baseDirectory, file)] = assetKey; + manifest[path.relative(siteAssets.assetDirectory, absAssetFile)] = assetKey; } // `keyMap` now contains the assets that we need to expire @@ -215,11 +220,14 @@ function createPatternMatcher( } } -async function validateAssetSize(filePath: string) { - const { size } = await stat(filePath); +async function validateAssetSize( + absFilePath: string, + relativeFilePath: string +) { + const { size } = await stat(absFilePath); if (size > 25 * 1024 * 1024) { throw new Error( - `File ${filePath} is too big, it should be under 25 MiB. See https://developers.cloudflare.com/workers/platform/limits#kv-limits` + `File ${relativeFilePath} is too big, it should be under 25 MiB. See https://developers.cloudflare.com/workers/platform/limits#kv-limits` ); } } @@ -245,8 +253,23 @@ function urlSafe(filePath: string): string { * Information about the assets that should be uploaded */ export interface AssetPaths { + /** + * Absolute path to the root of the project. + * + * This is the directory containing wrangler.toml or cwd if no config. + */ baseDirectory: string; + /** + * The path to the assets directory, relative to the `baseDirectory`. + */ + assetDirectory: string; + /** + * An array of patterns that match files that should be uploaded. + */ includePatterns: string[]; + /** + * An array of patterns that match files that should not be uploaded. + */ excludePatterns: string[]; } @@ -259,16 +282,17 @@ export interface AssetPaths { */ export function getAssetPaths( config: Config, - baseDirectory = config.site?.bucket, + assetDirectory = config.site?.bucket, includePatterns = config.site?.include ?? [], excludePatterns = config.site?.exclude ?? [] ): undefined | AssetPaths { - return baseDirectory + const baseDirectory = path.resolve( + path.dirname(config.configPath ?? "wrangler.toml") + ); + return assetDirectory ? { - baseDirectory: path.resolve( - path.dirname(config.configPath ?? "wrangler.toml"), - baseDirectory - ), + baseDirectory, + assetDirectory, includePatterns, excludePatterns, }