Skip to content

Commit ee78d1c

Browse files
cdervclaude
andcommitted
Normalize paths in fileInformationCache for consistent cache keys
The fileInformationCache was using inconsistent path formats as keys, causing cache misses during preview. On Windows, paths could be stored with forward slashes but looked up with backslashes (or vice versa). Apply normalizePath() at all cache access points: - ensureFileInformationCache() in project-shared.ts (entry point) - renderForPreview() deletion in preview.ts - populateFileInformation() and inspectDocumentConfig() in inspect.ts - cleanupNotebook() in jupyter.ts Add unit tests for fileInformationCache behavior with reusable createMockProjectContext() factory in tests/unit/project/utils.ts. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent b3edfe3 commit ee78d1c

7 files changed

Lines changed: 153 additions & 13 deletions

File tree

‎src/command/preview/preview.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,8 @@ 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-
debug(`[renderForPreview] Invalidating file information cache for ${file}`);
433-
project.fileInformationCache.delete(file);
432+
const normalizedFile = normalizePath(file);
433+
project.fileInformationCache.delete(normalizedFile);
434434
}
435435

436436
// render

‎src/execute/jupyter/jupyter.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { basename, dirname, join, relative } from "../../deno_ral/path.ts";
88
import { satisfies } from "semver/mod.ts";
99

1010
import { existsSync } from "../../deno_ral/fs.ts";
11+
import { normalizePath } from "../../core/path.ts";
1112

1213
import { error, info } from "../../deno_ral/log.ts";
1314

@@ -865,7 +866,8 @@ function cleanupNotebook(
865866
) {
866867
// Make notebook non-transient when keep-ipynb is set
867868
const data = target.data as JupyterTargetData;
868-
const cached = project.fileInformationCache.get(target.source);
869+
const normalizedSource = normalizePath(target.source);
870+
const cached = project.fileInformationCache.get(normalizedSource);
869871
if (cached && data.transient && format.execute[kKeepIpynb]) {
870872
if (cached.target && cached.target.data) {
871873
(cached.target.data as JupyterTargetData).transient = false;

‎src/inspect/inspect.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ const populateFileInformation = async (
125125
fileInformation: Record<string, InspectedFile>,
126126
file: string,
127127
) => {
128+
const normalizedFile = normalizePath(file);
128129
const engine = await fileExecutionEngine(file, undefined, context);
129130
const src = await context.resolveFullMarkdownForFile(engine, file);
130131
if (engine) {
@@ -139,11 +140,13 @@ const populateFileInformation = async (
139140
}
140141
await projectResolveCodeCellsForFile(context, engine, file);
141142
await projectFileMetadata(context, file);
142-
fileInformation[file] = {
143-
includeMap: context.fileInformationCache.get(file)?.includeMap ??
144-
[],
145-
codeCells: context.fileInformationCache.get(file)?.codeCells ?? [],
146-
metadata: context.fileInformationCache.get(file)?.metadata ?? {},
143+
const cacheEntry = context.fileInformationCache.get(normalizedFile);
144+
// Output key: project-relative for portability
145+
const outputKey = relative(context.dir, normalizedFile);
146+
fileInformation[outputKey] = {
147+
includeMap: cacheEntry?.includeMap ?? [],
148+
codeCells: cacheEntry?.codeCells ?? [],
149+
metadata: cacheEntry?.metadata ?? {},
147150
};
148151
};
149152

@@ -216,7 +219,8 @@ const inspectDocumentConfig = async (path: string) => {
216219
await context.resolveFullMarkdownForFile(engine, path);
217220
await projectResolveCodeCellsForFile(context, engine, path);
218221
await projectFileMetadata(context, path);
219-
const fileInformation = context.fileInformationCache.get(path);
222+
const normalizedPath = normalizePath(path);
223+
const fileInformation = context.fileInformationCache.get(normalizedPath);
220224

221225
// data to write
222226
const config: InspectedDocumentConfig = {

‎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(

‎src/project/types/single-file/single-file.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ export async function singleFileProjectContext(
4545
const temp = globalTempContext();
4646
const projectCacheBaseDir = temp.createDir();
4747

48+
const normalizedDir = normalizePath(dirname(source));
49+
4850
const result: ProjectContext = {
4951
clone: () => result,
5052
resolveBrand: (fileName?: string) => projectResolveBrand(result, fileName),
51-
dir: normalizePath(dirname(source)),
53+
dir: normalizedDir,
5254
engines: [],
5355
files: {
5456
input: [],
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* file-information-cache.test.ts
3+
*
4+
* Tests for fileInformationCache path normalization
5+
* Related to issue #13955
6+
*
7+
* Copyright (C) 2020-2022 Posit Software, PBC
8+
*/
9+
10+
import { unitTest } from "../../test.ts";
11+
import { assert } from "testing/asserts";
12+
import { join } from "../../../src/deno_ral/path.ts";
13+
import { ensureFileInformationCache } from "../../../src/project/project-shared.ts";
14+
import { createMockProjectContext } from "./utils.ts";
15+
16+
// deno-lint-ignore require-await
17+
unitTest(
18+
"fileInformationCache - same path returns same entry",
19+
async () => {
20+
const project = createMockProjectContext();
21+
22+
// Use cross-platform absolute path (backslashes on Windows, forward on Linux)
23+
const path1 = join(project.dir, "doc.qmd");
24+
const path2 = join(project.dir, "doc.qmd");
25+
26+
const entry1 = ensureFileInformationCache(project, path1);
27+
const entry2 = ensureFileInformationCache(project, path2);
28+
29+
assert(
30+
entry1 === entry2,
31+
"Same path should return same cache entry",
32+
);
33+
assert(
34+
project.fileInformationCache.size === 1,
35+
"Should have exactly one cache entry",
36+
);
37+
},
38+
);
39+
40+
// deno-lint-ignore require-await
41+
unitTest(
42+
"fileInformationCache - different paths create different entries",
43+
async () => {
44+
const project = createMockProjectContext();
45+
46+
const path1 = join(project.dir, "doc1.qmd");
47+
const path2 = join(project.dir, "doc2.qmd");
48+
49+
const entry1 = ensureFileInformationCache(project, path1);
50+
const entry2 = ensureFileInformationCache(project, path2);
51+
52+
assert(
53+
entry1 !== entry2,
54+
"Different paths should return different cache entries",
55+
);
56+
assert(
57+
project.fileInformationCache.size === 2,
58+
"Should have two cache entries for different paths",
59+
);
60+
},
61+
);
62+
63+
// deno-lint-ignore require-await
64+
unitTest(
65+
"fileInformationCache - cache entry persists across calls",
66+
async () => {
67+
const project = createMockProjectContext();
68+
69+
const path = join(project.dir, "doc.qmd");
70+
71+
// First call creates entry
72+
const entry1 = ensureFileInformationCache(project, path);
73+
// Modify the entry
74+
entry1.metadata = { title: "Test" };
75+
76+
// Second call should return same entry with our modification
77+
const entry2 = ensureFileInformationCache(project, path);
78+
79+
assert(
80+
entry2.metadata?.title === "Test",
81+
"Cache entry should persist modifications",
82+
);
83+
assert(
84+
entry1 === entry2,
85+
"Should return same cache entry object",
86+
);
87+
},
88+
);

‎tests/unit/project/utils.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* utils.ts
3+
*
4+
* Test utilities for project-related unit tests
5+
*
6+
* Copyright (C) 2020-2022 Posit Software, PBC
7+
*/
8+
9+
import { ProjectContext } from "../../../src/project/types.ts";
10+
11+
/**
12+
* Create a minimal mock ProjectContext for testing.
13+
* Only provides the essential properties needed for cache-related tests.
14+
*
15+
* @param dir - The project directory (defaults to a temp directory)
16+
* @returns A mock ProjectContext suitable for unit testing
17+
*/
18+
export function createMockProjectContext(
19+
dir?: string,
20+
): ProjectContext {
21+
const projectDir = dir ?? Deno.makeTempDirSync({ prefix: "quarto-test" });
22+
23+
return {
24+
dir: projectDir,
25+
engines: [],
26+
files: { input: [] },
27+
notebookContext: {} as ProjectContext["notebookContext"],
28+
fileInformationCache: new Map(),
29+
resolveBrand: () => Promise.resolve(undefined),
30+
resolveFullMarkdownForFile: () => Promise.resolve({} as never),
31+
fileExecutionEngineAndTarget: () => Promise.resolve({} as never),
32+
fileMetadata: () => Promise.resolve({}),
33+
environment: () => Promise.resolve({} as never),
34+
renderFormats: () => Promise.resolve({}),
35+
clone: function () {
36+
return this;
37+
},
38+
isSingleFile: false,
39+
diskCache: {} as ProjectContext["diskCache"],
40+
temp: {} as ProjectContext["temp"],
41+
cleanup: () => {},
42+
} as ProjectContext;
43+
}

0 commit comments

Comments
 (0)