Skip to content

Commit f1a64ef

Browse files
thymikeeclaude
andcommitted
fix(depgraph): give each file pair one unambiguous edge id
`importEdgeId` joined paths with " -> ", which a path may contain, while `collapseEdges` keyed the same pair with a private NUL-separated helper. Both now use `importEdgeId`, joined with NUL, which no file path can contain. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 03f1b92 commit f1a64ef

3 files changed

Lines changed: 16 additions & 7 deletions

File tree

‎scripts/depgraph/import-graph.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,13 @@ test('importGraph adds explicit files that no kept edge touches', () => {
4343
['src/core/a.ts', 'src/core/b.ts', 'src/core/c.ts'],
4444
);
4545
});
46+
47+
test('importGraph keeps distinct file pairs distinct even when a path contains the arrow', () => {
48+
const pair = (from: string, to: string) => ({ ...edges[0]!, from, to });
49+
const graph = importGraph(
50+
[pair('src/a -> b.ts', 'src/c.ts'), pair('src/a', 'b.ts -> src/c.ts')],
51+
VALUE_EDGES,
52+
);
53+
54+
assert.equal(new Set(graph.edges.map((edge) => edge.id)).size, 2);
55+
});

‎scripts/depgraph/import-graph.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ export const VALUE_EDGES: ReadonlySet<EdgeKind> = new Set(['value']);
1515
*/
1616
export const EXECUTABLE_EDGES: ReadonlySet<EdgeKind> = new Set(['value', 'dynamic']);
1717

18-
/** Graph edge id for a collapsed file pair; `collapseEdges` keeps one edge per pair. */
18+
/**
19+
* Identity of one file pair, shared by `collapseEdges` and the graph's edge ids. NUL cannot occur
20+
* in a file path, so distinct pairs never share an id.
21+
*/
1922
export function importEdgeId(from: string, to: string): string {
20-
return `${from} -> ${to}`;
23+
return `${from}\u0000${to}`;
2124
}
2225

2326
/**

‎scripts/depgraph/model.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,6 @@ export function authorityLabelsForEdge(edge: ResolvedImportEdge): AuthorityLabel
148148
return authorityLabelsForDeclared(declaredAuthorities(edge));
149149
}
150150

151-
function edgePair(from: string, to: string): string {
152-
return `${from}\u0000${to}`;
153-
}
154-
155151
function countAuthorityLabels(edgeAuthorities: readonly AuthorityLabel[][]): AuthorityCounts {
156152
const counts = Object.fromEntries(AUTHORITY_LABELS.map((label) => [label, 0])) as AuthorityCounts;
157153
for (const labels of edgeAuthorities) {
@@ -184,7 +180,7 @@ export function collapseEdges(edges: readonly ResolvedImportEdge[]): GraphEdge[]
184180
const byPair = new Map<string, GraphEdge>();
185181
for (const edge of edges) {
186182
if (edge.file === edge.target) continue;
187-
const key = edgePair(edge.file, edge.target);
183+
const key = importEdgeId(edge.file, edge.target);
188184
const kind = edgeKind(edge);
189185
const existing = byPair.get(key);
190186
const authorities = orderedDeclaredAuthorities([

0 commit comments

Comments
 (0)