Skip to content

Commit d1f0f14

Browse files
cdervclaude
andcommitted
Refactor path normalization into FileInformationCacheMap class
Move normalization logic from call sites into the cache class itself. The class now overrides get/has/set/delete to normalize keys automatically, ensuring consistent cache behavior without requiring callers to remember to normalize paths. Also fix ensureFileInformationCache fallback to create FileInformationCacheMap (not plain Map) and add test coverage for this case. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 302065b commit d1f0f14

6 files changed

Lines changed: 60 additions & 18 deletions

File tree

‎src/command/preview/preview.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +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-
project.fileInformationCache.delete(normalizedFile);
432+
project.fileInformationCache.delete(file);
434433
}
435434

436435
// render

‎src/execute/jupyter/jupyter.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ 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";
1211

1312
import { error, info } from "../../deno_ral/log.ts";
1413

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

‎src/inspect/inspect.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ const populateFileInformation = async (
125125
fileInformation: Record<string, InspectedFile>,
126126
file: string,
127127
) => {
128-
const normalizedFile = normalizePath(file);
129128
const engine = await fileExecutionEngine(file, undefined, context);
130129
const src = await context.resolveFullMarkdownForFile(engine, file);
131130
if (engine) {
@@ -140,9 +139,9 @@ const populateFileInformation = async (
140139
}
141140
await projectResolveCodeCellsForFile(context, engine, file);
142141
await projectFileMetadata(context, file);
143-
const cacheEntry = context.fileInformationCache.get(normalizedFile);
142+
const cacheEntry = context.fileInformationCache.get(file);
144143
// Output key: project-relative for portability
145-
const outputKey = relative(context.dir, normalizedFile);
144+
const outputKey = relative(context.dir, normalizePath(file));
146145
fileInformation[outputKey] = {
147146
includeMap: cacheEntry?.includeMap ?? [],
148147
codeCells: cacheEntry?.codeCells ?? [],
@@ -219,8 +218,7 @@ const inspectDocumentConfig = async (path: string) => {
219218
await context.resolveFullMarkdownForFile(engine, path);
220219
await projectResolveCodeCellsForFile(context, engine, path);
221220
await projectFileMetadata(context, path);
222-
const normalizedPath = normalizePath(path);
223-
const fileInformation = context.fileInformationCache.get(normalizedPath);
221+
const fileInformation = context.fileInformationCache.get(path);
224222

225223
// data to write
226224
const config: InspectedDocumentConfig = {

‎src/project/project-shared.ts‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -505,18 +505,17 @@ export const ensureFileInformationCache = (
505505
project: ProjectContext,
506506
file: string,
507507
) => {
508-
const normalizedFile = normalizePath(file);
509508
if (!project.fileInformationCache) {
510-
project.fileInformationCache = new Map();
509+
project.fileInformationCache = new FileInformationCacheMap();
511510
}
512511
assert(
513512
project.fileInformationCache instanceof Map,
514513
JSON.stringify(project.fileInformationCache),
515514
);
516-
if (!project.fileInformationCache.has(normalizedFile)) {
517-
project.fileInformationCache.set(normalizedFile, {} as FileInformation);
515+
if (!project.fileInformationCache.has(file)) {
516+
project.fileInformationCache.set(file, {} as FileInformation);
518517
}
519-
return project.fileInformationCache.get(normalizedFile)!;
518+
return project.fileInformationCache.get(file)!;
520519
};
521520

522521
export async function projectResolveBrand(
@@ -657,8 +656,25 @@ export async function projectResolveBrand(
657656
}
658657

659658
// Create a class that extends Map and implements Cloneable
659+
// All operations normalize keys for cross-platform consistency
660660
export class FileInformationCacheMap extends Map<string, FileInformation>
661661
implements Cloneable<Map<string, FileInformation>> {
662+
override get(key: string): FileInformation | undefined {
663+
return super.get(normalizePath(key));
664+
}
665+
666+
override has(key: string): boolean {
667+
return super.has(normalizePath(key));
668+
}
669+
670+
override set(key: string, value: FileInformation): this {
671+
return super.set(normalizePath(key), value);
672+
}
673+
674+
override delete(key: string): boolean {
675+
return super.delete(normalizePath(key));
676+
}
677+
662678
clone(): Map<string, FileInformation> {
663679
// Return the same instance (reference) instead of creating a clone
664680
return this;

‎tests/unit/project/file-information-cache.test.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
import { unitTest } from "../../test.ts";
1111
import { assert } from "testing/asserts";
1212
import { join } from "../../../src/deno_ral/path.ts";
13-
import { ensureFileInformationCache } from "../../../src/project/project-shared.ts";
13+
import {
14+
ensureFileInformationCache,
15+
FileInformationCacheMap,
16+
} from "../../../src/project/project-shared.ts";
1417
import { createMockProjectContext } from "./utils.ts";
1518

1619
// deno-lint-ignore require-await
@@ -86,3 +89,21 @@ unitTest(
8689
);
8790
},
8891
);
92+
93+
// deno-lint-ignore require-await
94+
unitTest(
95+
"ensureFileInformationCache - creates FileInformationCacheMap when cache is missing",
96+
async () => {
97+
const project = createMockProjectContext();
98+
// Simulate minimal ProjectContext without cache (as in command-utils.ts)
99+
// deno-lint-ignore no-explicit-any
100+
(project as any).fileInformationCache = undefined;
101+
102+
ensureFileInformationCache(project, join(project.dir, "doc.qmd"));
103+
104+
assert(
105+
project.fileInformationCache instanceof FileInformationCacheMap,
106+
"Should create FileInformationCacheMap, not plain Map",
107+
);
108+
},
109+
);

‎tests/unit/project/utils.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import { ProjectContext } from "../../../src/project/types.ts";
10+
import { FileInformationCacheMap } from "../../../src/project/project-shared.ts";
1011

1112
/**
1213
* Create a minimal mock ProjectContext for testing.
@@ -19,13 +20,14 @@ export function createMockProjectContext(
1920
dir?: string,
2021
): ProjectContext {
2122
const projectDir = dir ?? Deno.makeTempDirSync({ prefix: "quarto-test" });
23+
const ownsDir = dir === undefined;
2224

2325
return {
2426
dir: projectDir,
2527
engines: [],
2628
files: { input: [] },
2729
notebookContext: {} as ProjectContext["notebookContext"],
28-
fileInformationCache: new Map(),
30+
fileInformationCache: new FileInformationCacheMap(),
2931
resolveBrand: () => Promise.resolve(undefined),
3032
resolveFullMarkdownForFile: () => Promise.resolve({} as never),
3133
fileExecutionEngineAndTarget: () => Promise.resolve({} as never),
@@ -38,6 +40,14 @@ export function createMockProjectContext(
3840
isSingleFile: false,
3941
diskCache: {} as ProjectContext["diskCache"],
4042
temp: {} as ProjectContext["temp"],
41-
cleanup: () => {},
43+
cleanup: () => {
44+
if (ownsDir) {
45+
try {
46+
Deno.removeSync(projectDir, { recursive: true });
47+
} catch {
48+
// Ignore cleanup errors in tests
49+
}
50+
}
51+
},
4252
} as ProjectContext;
4353
}

0 commit comments

Comments
 (0)