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.
This commit is contained in:
Giancarlo Erra
2026-05-22 16:07:14 +01:00
parent e9ee3ea116
commit 4526ea58ae
2 changed files with 21 additions and 1 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ export function getFileDependencies(graph: CodeGraph, relativePath: string): {
importedBy: string[]; importedBy: string[];
} { } {
const normalized = toForwardSlash(relativePath); 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) { if (!node) {
return { imports: [], importedBy: [] }; return { imports: [], importedBy: [] };
} }
+20
View File
@@ -145,6 +145,26 @@ describe("graph-analysis", () => {
expect(depsMixed.imports).toEqual(deps.imports); expect(depsMixed.imports).toEqual(deps.imports);
expect(depsMixed.importedBy).toEqual(deps.importedBy); 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", () => { describe("findCircularDependencies", () => {