diff --git a/packages/share/src/projectSnapshot.mjs b/packages/share/src/projectSnapshot.mjs index 1ba3caca308..586a7c8a367 100644 --- a/packages/share/src/projectSnapshot.mjs +++ b/packages/share/src/projectSnapshot.mjs @@ -137,7 +137,7 @@ export async function snapshotProject( if (kind?.isDirectory()) await walk(full, parents); else if (kind?.isFile()) { const key = await snapshotKey(full, byContent, fileTick); - if (key !== undefined) result.set(path.relative(dir, full), key); + if (key !== undefined) result.set(snapshotRelativePath(dir, full), key); } } } @@ -173,11 +173,11 @@ export async function snapshotFiles(dir, files, { lenientIgnore = false } = {}) const result = new Map(); const ignored = await ignoreFor(dir, lenientIgnore); for (const file of files) { - const full = path.join(dir, file); + const full = path.join(dir, file.replaceAll("\\", "/")); const stat = await fs.lstat(full).catch(() => null); if (stat?.isDirectory()) return null; const holds = stat?.isFile() && !excludedFromSnapshot(dir, full, ignored); - result.set(file, holds ? await snapshotKey(full, false) : undefined); + result.set(snapshotRelativePath(dir, full), holds ? await snapshotKey(full, false) : undefined); } return result; } @@ -204,8 +204,11 @@ export const projectFingerprint = async (projectPath, options) => /** @param {string} dir @param {boolean} lenient */ const ignoreFor = (dir, lenient) => (lenient ? readChangeIgnore(dir) : readSourceIgnore(dir)); +/** @param {string} dir @param {string} full */ +const snapshotRelativePath = (dir, full) => path.relative(dir, full).replaceAll("\\", "/"); + /** @param {string} dir @param {string} full @param {(path: string) => boolean} ignored */ function excludedFromSnapshot(dir, full, ignored) { - const relative = path.relative(dir, full); - return isOutsideFilm(relative) || ignored(relative.split(path.sep).join("/")); + const relative = snapshotRelativePath(dir, full); + return isOutsideFilm(relative) || ignored(relative); } diff --git a/packages/share/tests/projectSnapshotPosix.test.mjs b/packages/share/tests/projectSnapshotPosix.test.mjs new file mode 100644 index 00000000000..4ff15b8ee0c --- /dev/null +++ b/packages/share/tests/projectSnapshotPosix.test.mjs @@ -0,0 +1,49 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; +import { syncBuiltinESMExports } from "node:module"; +import { tmpdir } from "node:os"; +import path, { join } from "node:path"; +import { diffChangedFiles, snapshotFiles, snapshotProject } from "../src/projectSnapshot.mjs"; + +// fails without the fix: snapshotProject keyed entries with raw path.relative(), which emits backslashes on Windows, +// while excludedFromSnapshot normalized with path.sep. +test("snapshotProject, snapshotFiles, and ignore matching share forward-slash keys with Windows-style relative paths", async (t) => { + const dir = await mkdtemp(join(tmpdir(), "hf-share-win-snapshot-")); + const originalRelative = path.relative; + path.relative = (...args) => path.win32.relative(...args); + syncBuiltinESMExports(); + t.after(async () => { + path.relative = originalRelative; + syncBuiltinESMExports(); + await rm(dir, { recursive: true, force: true }); + }); + + await writeFile(join(dir, "index.html"), "