Skip to content

Commit b796ec4

Browse files
cdervclaude
andcommitted
preview, project-shared - Normalize paths in fileInformationCache for cross-platform consistency
ensureFileInformationCache now normalizes file paths before using them as cache keys. This ensures cache invalidation works correctly on Windows where paths may have backslash separators. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 3b8e9a3 commit b796ec4

3 files changed

Lines changed: 78 additions & 7 deletions

File tree

‎src/command/preview/preview.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,10 +429,7 @@ export async function renderForPreview(
429429
// fileInformationCache contains file content that needs to be refreshed.
430430
// TODO(#13955): Consider adding a dedicated invalidateForFile() method on ProjectContext
431431
if (project?.fileInformationCache) {
432-
const normalizedFile = normalizePath(file);
433-
if (project.fileInformationCache.has(normalizedFile)) {
434-
project.fileInformationCache.delete(normalizedFile);
435-
}
432+
project.fileInformationCache.delete(normalizePath(file));
436433
}
437434

438435
// render

‎src/project/project-shared.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,17 +505,18 @@ export const ensureFileInformationCache = (
505505
project: ProjectContext,
506506
file: string,
507507
) => {
508+
const normalizedFile = normalizePath(file);
508509
if (!project.fileInformationCache) {
509510
project.fileInformationCache = new Map();
510511
}
511512
assert(
512513
project.fileInformationCache instanceof Map,
513514
JSON.stringify(project.fileInformationCache),
514515
);
515-
if (!project.fileInformationCache.has(file)) {
516-
project.fileInformationCache.set(file, {} as FileInformation);
516+
if (!project.fileInformationCache.has(normalizedFile)) {
517+
project.fileInformationCache.set(normalizedFile, {} as FileInformation);
517518
}
518-
return project.fileInformationCache.get(file)!;
519+
return project.fileInformationCache.get(normalizedFile)!;
519520
};
520521

521522
export async function projectResolveBrand(
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* file-information-cache.test.ts
3+
*
4+
* Copyright (C) 2026 Posit Software, PBC
5+
*
6+
* Tests for ensureFileInformationCache path normalization.
7+
* These tests verify that the cache works correctly with different
8+
* path separator styles on both Windows and Linux.
9+
*/
10+
11+
import { unitTest } from "../../test.ts";
12+
import { assert, assertEquals } from "testing/asserts";
13+
import { ensureFileInformationCache } from "../../../src/project/project-shared.ts";
14+
import { ProjectContext } from "../../../src/project/types.ts";
15+
import { normalizePath } from "../../../src/core/path.ts";
16+
import { join } from "../../../src/deno_ral/path.ts";
17+
18+
// Test 1: Real OS paths work correctly (runs on Windows + Linux)
19+
// This uses actual temp directory paths from the OS to verify
20+
// that the normalization works with real native paths.
21+
// deno-lint-ignore require-await
22+
unitTest("ensureFileInformationCache - works with OS-native paths", async () => {
23+
const tempDir = Deno.makeTempDirSync({ prefix: "quarto-cache-test-" });
24+
try {
25+
const project = { fileInformationCache: new Map() } as unknown as ProjectContext;
26+
27+
// OS gives us native path (backslashes on Windows, forward slashes on Linux)
28+
const osPath = join(tempDir, "test.qmd");
29+
30+
// Cache entry created with OS path
31+
const cache1 = ensureFileInformationCache(project, osPath);
32+
cache1.metadata = { title: "Test" };
33+
34+
// Delete with normalized path (what renderForPreview does)
35+
const normalizedPath = normalizePath(osPath);
36+
project.fileInformationCache.delete(normalizedPath);
37+
38+
// Entry should be gone
39+
assertEquals(project.fileInformationCache.size, 0, "Cache should be empty after delete with normalized path");
40+
} finally {
41+
Deno.removeSync(tempDir, { recursive: true });
42+
}
43+
});
44+
45+
// Test 2: Different separators normalize to same key
46+
// This verifies that paths with forward and backslashes both
47+
// map to the same cache entry.
48+
// deno-lint-ignore require-await
49+
unitTest("ensureFileInformationCache - normalizes path separators", async () => {
50+
const project = { fileInformationCache: new Map() } as unknown as ProjectContext;
51+
52+
const cache1 = ensureFileInformationCache(project, "/test/project/test.qmd");
53+
const cache2 = ensureFileInformationCache(project, "\\test\\project\\test.qmd");
54+
55+
assert(cache1 === cache2, "Same file with different separators should return same cache");
56+
assertEquals(project.fileInformationCache.size, 1, "Cache should have only one entry");
57+
});
58+
59+
// Test 3: Cache invalidation matches cache creation
60+
// This simulates the exact cache create/delete flow used in preview.ts
61+
// to ensure that the normalization is consistent for both operations.
62+
// deno-lint-ignore require-await
63+
unitTest("ensureFileInformationCache - invalidation works with normalized paths", async () => {
64+
const project = { fileInformationCache: new Map() } as unknown as ProjectContext;
65+
66+
// Create cache with backslash path (like Windows might provide)
67+
ensureFileInformationCache(project, "C:\\project\\test.qmd");
68+
assertEquals(project.fileInformationCache.size, 1, "Cache should have one entry after creation");
69+
70+
// Delete with forward slash path (normalized)
71+
project.fileInformationCache.delete(normalizePath("C:\\project\\test.qmd"));
72+
assertEquals(project.fileInformationCache.size, 0, "Cache should be empty after deletion with normalized path");
73+
});

0 commit comments

Comments
 (0)