From 628832af0f6693d54a0f934f53bdf34c739a5613 Mon Sep 17 00:00:00 2001 From: Christian Svensson Date: Tue, 15 Oct 2024 11:29:35 +0200 Subject: [PATCH] fix: ensure file paths in snapshot.json uses / (#2704) Avoids `\\\\` paths ending up in `snapshot.json` when using `deno task build` on Windows. Backslashes do not which pathnames in URLs, so these assets are not returned. Closes #2703 --- src/dev/builder_test.ts | 27 +++++++++++++++++++++++++++ src/dev/dev_build_cache.ts | 3 ++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/dev/builder_test.ts b/src/dev/builder_test.ts index ae0e3692..d2b4896b 100644 --- a/src/dev/builder_test.ts +++ b/src/dev/builder_test.ts @@ -44,6 +44,33 @@ Deno.test({ sanitizeResources: false, }); +Deno.test({ + name: "Builder - handles Windows paths", + fn: async () => { + const builder = new Builder(); + const tmp = await Deno.makeTempDir(); + await Deno.mkdir(path.join(tmp, "images")); + await Deno.writeTextFile( + path.join(tmp, "images", "batman.svg"), + "", + ); + const app = new App({ + staticDir: tmp, + build: { + outDir: path.join(tmp, "dist"), + }, + }); + await builder.build(app); + + const snapshotJson = await Deno.readTextFile( + path.join(tmp, "dist", "snapshot.json"), + ); + expect(snapshotJson).toContain("/images/batman.svg"); + }, + sanitizeOps: false, + sanitizeResources: false, +}); + Deno.test({ name: "Builder - hashes CSS urls by default", fn: async () => { diff --git a/src/dev/dev_build_cache.ts b/src/dev/dev_build_cache.ts index a53e38b3..2a08c6cb 100644 --- a/src/dev/dev_build_cache.ts +++ b/src/dev/dev_build_cache.ts @@ -1,5 +1,6 @@ import type { BuildCache, StaticFile } from "../build_cache.ts"; import * as path from "@std/path"; +import { SEPARATOR as WINDOWS_SEPARATOR } from "@std/path/windows/constants"; import type { ResolvedFreshConfig } from "../config.ts"; import type { BuildSnapshot } from "../build_cache.ts"; import { encodeHex } from "@std/encoding/hex"; @@ -174,7 +175,7 @@ export class DiskBuildCache implements DevBuildCache { addUnprocessedFile(pathname: string): void { this.#unprocessedFiles.set( - pathname, + pathname.replaceAll(WINDOWS_SEPARATOR, "/"), path.join(this.config.staticDir, pathname), ); }