From 4526ea58ae332b3839019eb3f46a0eff08801bd6 Mon Sep 17 00:00:00 2001 From: Giancarlo Erra Date: Fri, 22 May 2026 16:07:14 +0100 Subject: [PATCH] fix(graph): normalize stored node keys during lookup for legacy cache compat Address CodeRabbit review: also normalize stored graph node keys when comparing, not just the query input. Handles pre-fix Windows caches where node keys still contain backslashes until the graph is rebuilt. --- src/services/graph-analysis.ts | 2 +- tests/unit/graph-analysis.test.ts | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/services/graph-analysis.ts b/src/services/graph-analysis.ts index bc4354e..1650092 100644 --- a/src/services/graph-analysis.ts +++ b/src/services/graph-analysis.ts @@ -14,7 +14,7 @@ export function getFileDependencies(graph: CodeGraph, relativePath: string): { importedBy: string[]; } { const normalized = toForwardSlash(relativePath); - const node = graph.nodes.find((n) => n.relativePath === normalized); + const node = graph.nodes.find((n) => toForwardSlash(n.relativePath) === normalized); if (!node) { return { imports: [], importedBy: [] }; } diff --git a/tests/unit/graph-analysis.test.ts b/tests/unit/graph-analysis.test.ts index 8757da4..89f7034 100644 --- a/tests/unit/graph-analysis.test.ts +++ b/tests/unit/graph-analysis.test.ts @@ -145,6 +145,26 @@ describe("graph-analysis", () => { expect(depsMixed.imports).toEqual(deps.imports); expect(depsMixed.importedBy).toEqual(deps.importedBy); }); + + it("finds nodes in a legacy cached graph with backslash keys", () => { + // Simulate a graph built on Windows before the fix: stored keys have backslashes + const nodes: CodeGraphNode[] = [ + makeNode("src\\services\\api.ts", ["src\\types.ts"], []), + makeNode("src\\types.ts", [], ["src\\services\\api.ts"]), + ]; + // Fix relativePath (makeNode sets it from the argument) + nodes[0].relativePath = "src\\services\\api.ts"; + nodes[1].relativePath = "src\\types.ts"; + + const edges: CodeGraphEdge[] = [ + makeEdge("src\\services\\api.ts", "src\\types.ts"), + ]; + const graph = makeGraph(nodes, edges); + + // Query with forward slashes should still find the node + const deps = getFileDependencies(graph, "src/services/api.ts"); + expect(deps.imports).toContain("src\\types.ts"); + }); }); describe("findCircularDependencies", () => {