|
| 1 | +/** |
| 2 | + * Integration test for #1781: `codegraph exports` did not credit consumers |
| 3 | + * reached via a dynamic `import()` expression whose destructured result is |
| 4 | + * wrapped in redundant parentheses and/or a TypeScript `as {...}` type |
| 5 | + * assertion — the exact shape used throughout |
| 6 | + * `src/domain/graph/builder/stages/native-orchestrator.ts`: |
| 7 | + * |
| 8 | + * const { buildDataflowVerticesFromMap, ... } = |
| 9 | + * (await import('../../../../features/dataflow.js')) as {...}; |
| 10 | + * |
| 11 | + * Root cause: `extractDynamicImportNames` (TS) / `extract_dynamic_import_names` |
| 12 | + * (Rust) walked up from the `import()` call through at most one optional |
| 13 | + * `await_expression` before requiring the immediate parent to be a |
| 14 | + * `variable_declarator`. The extra `parenthesized_expression` and/or |
| 15 | + * `as_expression` wrapper layers introduced by parens and a type assertion |
| 16 | + * broke that walk-up, so no destructured names were ever extracted — the |
| 17 | + * import was recorded with `names: []`, `importedNames` never gained an |
| 18 | + * entry for the destructured bindings, and the later call through the |
| 19 | + * destructured local name never resolved to a `calls` edge. Both `codegraph |
| 20 | + * exports` (consumer count) and `codegraph roles --role dead` therefore |
| 21 | + * treated a genuinely-consumed export as unreferenced. |
| 22 | + * |
| 23 | + * Fix: the walk-up now skips any nesting/combination of `await_expression`, |
| 24 | + * `parenthesized_expression`, and `as_expression` wrappers before checking |
| 25 | + * for the enclosing `variable_declarator`, in both engines. |
| 26 | + * |
| 27 | + * Two consumer shapes are exercised against the same target module: |
| 28 | + * - `usesPlain`: bare `const { X } = await import(...)` (no cast) — the |
| 29 | + * pre-existing baseline that must keep working. |
| 30 | + * - `usesCast`: `const { Y } = (await import(...)) as {...}` — the exact |
| 31 | + * regression shape from native-orchestrator.ts. |
| 32 | + * |
| 33 | + * The target module and its consumer are deliberately placed in *different* |
| 34 | + * directories (`features/` vs `domain/stages/`, mirroring the real repo |
| 35 | + * layout) rather than side by side. A same-directory fixture does not |
| 36 | + * discriminate this bug at all: the resolver's directory-scoped fallback |
| 37 | + * tier (a same-directory, name-only match — one of several confidence tiers |
| 38 | + * below the import-aware match) coincidentally "rescues" the call even when |
| 39 | + * `extractDynamicImportNames` returns no names, producing a `calls` edge |
| 40 | + * regardless of whether the fix is present. Cross-directory placement is |
| 41 | + * required so the only way to resolve `usesCast`'s call is through the |
| 42 | + * import-aware path this bug actually breaks. |
| 43 | + */ |
| 44 | + |
| 45 | +import fs from 'node:fs'; |
| 46 | +import os from 'node:os'; |
| 47 | +import path from 'node:path'; |
| 48 | +import Database from 'better-sqlite3'; |
| 49 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 50 | +import { buildGraph } from '../../src/domain/graph/builder.js'; |
| 51 | +import { exportsData } from '../../src/domain/queries.js'; |
| 52 | +import { isNativeAvailable } from '../../src/infrastructure/native.js'; |
| 53 | + |
| 54 | +const DEAD_ROLES = new Set(['dead-unresolved', 'dead-leaf', 'dead-entry', 'dead-ffi']); |
| 55 | + |
| 56 | +const TARGET_FILE = 'features/dataflow.ts'; |
| 57 | + |
| 58 | +const FIXTURE = { |
| 59 | + [TARGET_FILE]: ` |
| 60 | +export function buildDataflowVerticesFromMap(): number { |
| 61 | + return 42; |
| 62 | +} |
| 63 | +
|
| 64 | +export function buildDataflowP4ForNative(): number { |
| 65 | + return 7; |
| 66 | +} |
| 67 | +`, |
| 68 | + 'domain/stages/consumer.ts': ` |
| 69 | +export async function usesPlain() { |
| 70 | + const { buildDataflowVerticesFromMap } = await import('../../features/dataflow.js'); |
| 71 | + return buildDataflowVerticesFromMap(); |
| 72 | +} |
| 73 | +
|
| 74 | +export async function usesCast() { |
| 75 | + const { buildDataflowP4ForNative } = (await import('../../features/dataflow.js')) as { |
| 76 | + buildDataflowP4ForNative: () => number; |
| 77 | + }; |
| 78 | + return buildDataflowP4ForNative(); |
| 79 | +} |
| 80 | +`, |
| 81 | +}; |
| 82 | + |
| 83 | +function readNodesWithRoles(dbPath: string) { |
| 84 | + const db = new Database(dbPath, { readonly: true }); |
| 85 | + try { |
| 86 | + return db.prepare('SELECT name, kind, role FROM nodes ORDER BY name').all() as Array<{ |
| 87 | + name: string; |
| 88 | + kind: string; |
| 89 | + role: string | null; |
| 90 | + }>; |
| 91 | + } finally { |
| 92 | + db.close(); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +function getCallEdges(dbPath: string) { |
| 97 | + const db = new Database(dbPath, { readonly: true }); |
| 98 | + try { |
| 99 | + return db |
| 100 | + .prepare( |
| 101 | + `SELECT n1.name AS src, n2.name AS tgt, n2.file AS tgt_file |
| 102 | + FROM edges e |
| 103 | + JOIN nodes n1 ON e.source_id = n1.id |
| 104 | + JOIN nodes n2 ON e.target_id = n2.id |
| 105 | + WHERE e.kind = 'calls' |
| 106 | + ORDER BY n1.name, n2.name`, |
| 107 | + ) |
| 108 | + .all() as Array<{ src: string; tgt: string; tgt_file: string }>; |
| 109 | + } finally { |
| 110 | + db.close(); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +function writeFixture(rootDir: string) { |
| 115 | + for (const [rel, content] of Object.entries(FIXTURE)) { |
| 116 | + const abs = path.join(rootDir, rel); |
| 117 | + fs.mkdirSync(path.dirname(abs), { recursive: true }); |
| 118 | + fs.writeFileSync(abs, content); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +describe('dynamic import() + destructure consumer crediting (#1781) — WASM', () => { |
| 123 | + let tmpDir: string; |
| 124 | + |
| 125 | + beforeAll(async () => { |
| 126 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-1781-wasm-')); |
| 127 | + writeFixture(tmpDir); |
| 128 | + await buildGraph(tmpDir, { engine: 'wasm', incremental: false, skipRegistry: true }); |
| 129 | + }); |
| 130 | + |
| 131 | + afterAll(() => { |
| 132 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 133 | + }); |
| 134 | + |
| 135 | + it('creates a calls edge for the bare (uncast) destructured binding', () => { |
| 136 | + const dbPath = path.join(tmpDir, '.codegraph', 'graph.db'); |
| 137 | + const edges = getCallEdges(dbPath); |
| 138 | + const edge = edges.find( |
| 139 | + (e) => e.src === 'usesPlain' && e.tgt === 'buildDataflowVerticesFromMap', |
| 140 | + ); |
| 141 | + expect( |
| 142 | + edge, |
| 143 | + `Expected usesPlain -> buildDataflowVerticesFromMap; got: ${JSON.stringify(edges)}`, |
| 144 | + ).toBeDefined(); |
| 145 | + expect(edge?.tgt_file).toBe(TARGET_FILE); |
| 146 | + }); |
| 147 | + |
| 148 | + it('creates a calls edge for the parenthesized + `as`-cast destructured binding', () => { |
| 149 | + const dbPath = path.join(tmpDir, '.codegraph', 'graph.db'); |
| 150 | + const edges = getCallEdges(dbPath); |
| 151 | + const edge = edges.find((e) => e.src === 'usesCast' && e.tgt === 'buildDataflowP4ForNative'); |
| 152 | + expect( |
| 153 | + edge, |
| 154 | + `Expected usesCast -> buildDataflowP4ForNative; got: ${JSON.stringify(edges)}`, |
| 155 | + ).toBeDefined(); |
| 156 | + expect(edge?.tgt_file).toBe(TARGET_FILE); |
| 157 | + }); |
| 158 | + |
| 159 | + it('codegraph exports credits both dynamically-imported functions with real consumers', () => { |
| 160 | + const dbPath = path.join(tmpDir, '.codegraph', 'graph.db'); |
| 161 | + const data = exportsData(TARGET_FILE, dbPath); |
| 162 | + |
| 163 | + const vertices = data.results.find( |
| 164 | + (r: { name: string }) => r.name === 'buildDataflowVerticesFromMap', |
| 165 | + ); |
| 166 | + expect(vertices).toBeDefined(); |
| 167 | + expect(vertices.consumerCount).toBeGreaterThanOrEqual(1); |
| 168 | + expect(vertices.consumers.map((c: { name: string }) => c.name)).toContain('usesPlain'); |
| 169 | + |
| 170 | + const p4 = data.results.find((r: { name: string }) => r.name === 'buildDataflowP4ForNative'); |
| 171 | + expect(p4).toBeDefined(); |
| 172 | + expect(p4.consumerCount).toBeGreaterThanOrEqual(1); |
| 173 | + expect(p4.consumers.map((c: { name: string }) => c.name)).toContain('usesCast'); |
| 174 | + }); |
| 175 | + |
| 176 | + it('does not classify either dynamically-imported export as dead', () => { |
| 177 | + const dbPath = path.join(tmpDir, '.codegraph', 'graph.db'); |
| 178 | + const nodes = readNodesWithRoles(dbPath); |
| 179 | + for (const name of ['buildDataflowVerticesFromMap', 'buildDataflowP4ForNative']) { |
| 180 | + const node = nodes.find((n) => n.name === name && n.kind === 'function'); |
| 181 | + expect(node, `${name} node not found`).toBeDefined(); |
| 182 | + expect( |
| 183 | + DEAD_ROLES.has(node!.role ?? ''), |
| 184 | + `${name} was classified as ${node!.role} — expected a non-dead role now that a real edge exists`, |
| 185 | + ).toBe(false); |
| 186 | + } |
| 187 | + }); |
| 188 | +}); |
| 189 | + |
| 190 | +// ── Native engine parity ──────────────────────────────────────────────────── |
| 191 | +// Skipped when the native addon is not installed. |
| 192 | + |
| 193 | +describe.skipIf(!isNativeAvailable())( |
| 194 | + 'dynamic import() + destructure consumer crediting (#1781) — native', |
| 195 | + () => { |
| 196 | + let nativeTmpDir: string; |
| 197 | + |
| 198 | + beforeAll(async () => { |
| 199 | + nativeTmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-1781-native-')); |
| 200 | + writeFixture(nativeTmpDir); |
| 201 | + await buildGraph(nativeTmpDir, { engine: 'native', incremental: false, skipRegistry: true }); |
| 202 | + }, 60_000); |
| 203 | + |
| 204 | + afterAll(() => { |
| 205 | + fs.rmSync(nativeTmpDir, { recursive: true, force: true }); |
| 206 | + }); |
| 207 | + |
| 208 | + it('creates a calls edge for the bare (uncast) destructured binding', () => { |
| 209 | + const dbPath = path.join(nativeTmpDir, '.codegraph', 'graph.db'); |
| 210 | + const edges = getCallEdges(dbPath); |
| 211 | + const edge = edges.find( |
| 212 | + (e) => e.src === 'usesPlain' && e.tgt === 'buildDataflowVerticesFromMap', |
| 213 | + ); |
| 214 | + expect( |
| 215 | + edge, |
| 216 | + `Expected native usesPlain -> buildDataflowVerticesFromMap; got: ${JSON.stringify(edges)}`, |
| 217 | + ).toBeDefined(); |
| 218 | + expect(edge?.tgt_file).toBe(TARGET_FILE); |
| 219 | + }); |
| 220 | + |
| 221 | + it('creates a calls edge for the parenthesized + `as`-cast destructured binding', () => { |
| 222 | + const dbPath = path.join(nativeTmpDir, '.codegraph', 'graph.db'); |
| 223 | + const edges = getCallEdges(dbPath); |
| 224 | + const edge = edges.find((e) => e.src === 'usesCast' && e.tgt === 'buildDataflowP4ForNative'); |
| 225 | + expect( |
| 226 | + edge, |
| 227 | + `Expected native usesCast -> buildDataflowP4ForNative; got: ${JSON.stringify(edges)}`, |
| 228 | + ).toBeDefined(); |
| 229 | + expect(edge?.tgt_file).toBe(TARGET_FILE); |
| 230 | + }); |
| 231 | + |
| 232 | + it('codegraph exports credits both dynamically-imported functions with real consumers', () => { |
| 233 | + const dbPath = path.join(nativeTmpDir, '.codegraph', 'graph.db'); |
| 234 | + const data = exportsData(TARGET_FILE, dbPath); |
| 235 | + |
| 236 | + const vertices = data.results.find( |
| 237 | + (r: { name: string }) => r.name === 'buildDataflowVerticesFromMap', |
| 238 | + ); |
| 239 | + expect(vertices).toBeDefined(); |
| 240 | + expect(vertices.consumerCount).toBeGreaterThanOrEqual(1); |
| 241 | + |
| 242 | + const p4 = data.results.find((r: { name: string }) => r.name === 'buildDataflowP4ForNative'); |
| 243 | + expect(p4).toBeDefined(); |
| 244 | + expect(p4.consumerCount).toBeGreaterThanOrEqual(1); |
| 245 | + }); |
| 246 | + |
| 247 | + it('does not classify either dynamically-imported export as dead', () => { |
| 248 | + const dbPath = path.join(nativeTmpDir, '.codegraph', 'graph.db'); |
| 249 | + const nodes = readNodesWithRoles(dbPath); |
| 250 | + for (const name of ['buildDataflowVerticesFromMap', 'buildDataflowP4ForNative']) { |
| 251 | + const node = nodes.find((n) => n.name === name && n.kind === 'function'); |
| 252 | + expect(node, `${name} node not found (native)`).toBeDefined(); |
| 253 | + expect(DEAD_ROLES.has(node!.role ?? '')).toBe(false); |
| 254 | + } |
| 255 | + }); |
| 256 | + }, |
| 257 | +); |
0 commit comments