From a9db4cd3eff1b16105e23f98233989983cb5c8d9 Mon Sep 17 00:00:00 2001 From: Reuven Date: Fri, 8 May 2026 09:58:09 -0400 Subject: [PATCH] fix(plugin-code-intelligence@3.0.0-alpha.3): real import edges in GNN bridge MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Followup to #1554 / #1553 (alpha.2 fixed the helper stubs; this addresses the third stub the issue called out). src/bridges/gnn-bridge.ts:500 — extractImports now resolves relative import specifiers via regex against the supplied `allFiles` set instead of returning `[]`. Result: dependency graphs produced by buildCodeGraph() finally have real edges, which architecture-analyze, refactor-impact, and split-suggest all need. Handles two specifier styles users actually write: - bare relative: `./foo`, `../bar/baz` - TS module-specifier convention: `./foo.js` (where the on-disk source is `./foo.ts` or `./foo.tsx`). The resolver tries the bare path, `.`, `/index.` for the standard JS/TS extensions, AND a stripped variant when the spec ends in `.js`/`.cjs`/`.mjs`. This is the common ESM-with-NodeNext pattern in v3 and was the dominant miss before this fix. Live probe (plugin's own src/ — 6 TS files): before: { nodes: 6, edges: 0 } after: { nodes: 6, edges: 11 } sample: gnn-bridge.ts → types.ts (import) index.ts → gnn-bridge.ts (import) index.ts → mincut-bridge.ts (import) Build clean. Published @claude-flow/plugin-code-intelligence@3.0.0-alpha.3 with alpha + latest dist-tags. Co-Authored-By: RuFlo --- v3/plugins/code-intelligence/package.json | 2 +- .../src/bridges/gnn-bridge.ts | 65 +++++++++++++++++-- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/v3/plugins/code-intelligence/package.json b/v3/plugins/code-intelligence/package.json index 16e2c02484..4442e7310d 100644 --- a/v3/plugins/code-intelligence/package.json +++ b/v3/plugins/code-intelligence/package.json @@ -1,6 +1,6 @@ { "name": "@claude-flow/plugin-code-intelligence", - "version": "3.0.0-alpha.2", + "version": "3.0.0-alpha.3", "description": "Advanced code intelligence plugin for Claude Flow V3 - semantic search, architecture analysis, refactoring impact, module splitting, and pattern learning", "type": "module", "main": "./dist/index.js", diff --git a/v3/plugins/code-intelligence/src/bridges/gnn-bridge.ts b/v3/plugins/code-intelligence/src/bridges/gnn-bridge.ts index dc426de699..67bbaf66e2 100644 --- a/v3/plugins/code-intelligence/src/bridges/gnn-bridge.ts +++ b/v3/plugins/code-intelligence/src/bridges/gnn-bridge.ts @@ -495,12 +495,67 @@ export class GNNBridge implements IGNNBridge { } /** - * Extract imports from file (simplified) + * Extract imports from file. Returns the subset of `allFiles` that this + * file imports via relative specifiers — used to build edges in the + * dependency graph. #1554/#1553: previously returned `[]` which produced + * graphs with zero edges and broke architecture-analyze, refactor-impact, + * and split-suggest. Regex-based (no AST parser dep) — handles `import … + * from`, `export … from`, and `require('…')` for relative paths only. */ - private async extractImports(_file: string, _allFiles: string[]): Promise { - // In production, would parse AST - // For now, return empty array - return []; + private async extractImports(file: string, allFiles: string[]): Promise { + const fs = await import('node:fs'); + const path = await import('node:path'); + + let content: string; + try { + content = fs.readFileSync(file, 'utf-8'); + } catch { + return []; + } + + const allFilesSet = new Set(allFiles.map((f) => path.resolve(f))); + const exts = ['.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs']; + const baseDir = path.dirname(path.resolve(file)); + const out = new Set(); + + const importRx = /^\s*(?:import\s+[^'"]+from\s+|import\s+|export\s+\*?\s*from\s+|export\s+\{[^}]*\}\s+from\s+)['"]([^'"]+)['"]|^\s*(?:const|let|var)\s+[^=]+=\s*require\(\s*['"]([^'"]+)['"]\s*\)/gm; + let m: RegExpExecArray | null; + while ((m = importRx.exec(content)) !== null) { + const spec = m[1] ?? m[2]; + if (!spec) continue; + // Only resolve relative/absolute paths — node_modules imports aren't + // graph nodes here. + if (!spec.startsWith('./') && !spec.startsWith('../') && !spec.startsWith('/')) continue; + + const candidates: string[] = []; + const base = spec.startsWith('/') ? spec : path.resolve(baseDir, spec); + // Try the bare path, each extension, and `index.` under a directory. + candidates.push(base); + for (const e of exts) { + candidates.push(base + e); + candidates.push(path.join(base, `index${e}`)); + } + // TS module-specifier convention: imports use `.js` even when the + // actual source is `.ts`/`.tsx`. Strip a trailing `.js`/`.cjs`/`.mjs` + // and re-try with TS extensions so source-level edges resolve. + const jsLikeMatch = base.match(/^(.+)\.(js|cjs|mjs)$/); + if (jsLikeMatch && jsLikeMatch[1]) { + const stripped = jsLikeMatch[1]; + for (const e of ['.ts', '.tsx', '.js', '.jsx']) { + candidates.push(stripped + e); + candidates.push(path.join(stripped, `index${e}`)); + } + } + for (const c of candidates) { + const resolved = path.resolve(c); + if (allFilesSet.has(resolved)) { + out.add(resolved); + break; + } + } + } + + return Array.from(out); } /**