From 2ba258778e8c4f7728d1919b55344a128119d3c4 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/dev/remote.tsx | 1 + packages/wrangler/src/sites.tsx | 58 ++++++++++++++++++++-------- 4 files changed, 56 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 710892c2eb35..30898014abf0 100644 --- a/packages/wrangler/src/dev/local.tsx +++ b/packages/wrangler/src/dev/local.tsx @@ -116,7 +116,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/dev/remote.tsx b/packages/wrangler/src/dev/remote.tsx index c180b86e86f4..333c5160f1b4 100644 --- a/packages/wrangler/src/dev/remote.tsx +++ b/packages/wrangler/src/dev/remote.tsx @@ -122,6 +122,7 @@ export function useWorker(props: { // include it in the kv namespace name regardless (since there's no // concept of service environments for kv namespaces yet). name + (!props.legacyEnv && props.env ? `-${props.env}` : ""), + path.resolve(path.dirname(config.configPath ?? ".")), assetPaths, true ); // TODO: cancellable? diff --git a/packages/wrangler/src/sites.tsx b/packages/wrangler/src/sites.tsx index 36d1304de77e..c0e27dac7850 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 @@ -214,11 +219,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` ); } } @@ -244,8 +252,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[]; } @@ -258,16 +281,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, }