From 0d03ec273051ef600705a26cf2c319a799924653 Mon Sep 17 00:00:00 2001 From: carlos-alm <127798846+carlos-alm@users.noreply.github.com> Date: Thu, 19 Mar 2026 03:49:20 -0600 Subject: [PATCH 1/3] ci: add dynamic import verification to catch stale paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add scripts/verify-imports.js that scans all await import() calls in src/ and verifies each relative path resolves to an existing file. Runs as a new CI job (verify-imports) in the pipeline gate. Also fixes two stale imports found by the script: - src/mcp/tools/semantic-search.js: ../../embeddings/index.js → ../../domain/search/index.js (embeddings was moved to domain/search) - src/cli/commands/info.js: ../../db.js → ../../db/index.js (db.js was split into db/ directory) Closes roadmap item 10.3. --- .github/workflows/ci.yml | 16 +++- package.json | 1 + scripts/verify-imports.js | 127 +++++++++++++++++++++++++++++++ src/cli/commands/info.js | 2 +- src/mcp/tools/semantic-search.js | 6 +- 5 files changed, 147 insertions(+), 5 deletions(-) create mode 100644 scripts/verify-imports.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 50f2b1fc..a80097ce 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -101,6 +101,20 @@ jobs: - name: Type check run: npm run typecheck + verify-imports: + runs-on: ubuntu-latest + name: Verify dynamic imports + steps: + - uses: actions/checkout@v6 + + - name: Setup Node.js + uses: actions/setup-node@v6 + with: + node-version: 22 + + - name: Verify all dynamic imports resolve + run: node scripts/verify-imports.js + rust-check: runs-on: ubuntu-latest name: Rust compile check @@ -121,7 +135,7 @@ jobs: ci-pipeline: if: always() - needs: [lint, test, typecheck, rust-check] + needs: [lint, test, typecheck, verify-imports, rust-check] runs-on: ubuntu-latest name: CI Testing Pipeline steps: diff --git a/package.json b/package.json index 2b4b03bf..c0ac55ef 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "build": "tsc", "build:wasm": "node scripts/build-wasm.js", "typecheck": "tsc --noEmit", + "verify-imports": "node scripts/verify-imports.js", "test": "vitest run", "test:watch": "vitest", "test:coverage": "vitest run --coverage", diff --git a/scripts/verify-imports.js b/scripts/verify-imports.js new file mode 100644 index 00000000..fb1a4868 --- /dev/null +++ b/scripts/verify-imports.js @@ -0,0 +1,127 @@ +#!/usr/bin/env node + +/** + * Verify that all dynamic import() paths in src/ resolve to existing files. + * + * Catches stale paths left behind after moves/renames — the class of bug + * that caused the ast-command crash (see roadmap 10.3). + * + * Exit codes: + * 0 — all imports resolve + * 1 — one or more broken imports found + */ + +import { readFileSync, existsSync, readdirSync, statSync } from 'node:fs'; +import { resolve, dirname, join, extname } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); +const srcDir = resolve(__dirname, '..', 'src'); + +// ── collect source files ──────────────────────────────────────────────── +function walk(dir) { + const results = []; + for (const entry of readdirSync(dir, { withFileTypes: true })) { + const full = join(dir, entry.name); + if (entry.isDirectory()) { + if (entry.name === 'node_modules') continue; + results.push(...walk(full)); + } else if (/\.[jt]sx?$/.test(entry.name)) { + results.push(full); + } + } + return results; +} + +// ── extract dynamic import specifiers ─────────────────────────────────── +// Matches: await import('...') and await import("...") +const DYNAMIC_IMPORT_RE = /await\s+import\(\s*(['"])(.+?)\1\s*\)/g; + +function extractDynamicImports(filePath) { + const src = readFileSync(filePath, 'utf8'); + const imports = []; + const lines = src.split('\n'); + + let inBlockComment = false; + for (let i = 0; i < lines.length; i++) { + const line = lines[i]; + + // Track block comments (/** ... */ and /* ... */) + if (inBlockComment) { + if (line.includes('*/')) inBlockComment = false; + continue; + } + if (/^\s*\/\*/.test(line)) { + if (!line.includes('*/')) inBlockComment = true; + continue; + } + // Skip single-line comments + if (/^\s*\/\//.test(line)) continue; + + let match; + DYNAMIC_IMPORT_RE.lastIndex = 0; + while ((match = DYNAMIC_IMPORT_RE.exec(line)) !== null) { + // Skip if the match is inside a trailing comment + const before = line.slice(0, match.index); + if (before.includes('//') || before.includes('/*')) continue; + + imports.push({ specifier: match[2], line: i + 1 }); + } + } + return imports; +} + +// ── resolve a specifier to a file on disk ─────────────────────────────── +function resolveSpecifier(specifier, fromFile) { + // Skip bare specifiers (packages): 'node:*', '@scope/pkg', 'pkg' + if (!specifier.startsWith('.') && !specifier.startsWith('/')) return null; + + const base = dirname(fromFile); + const target = resolve(base, specifier); + + // Exact file exists + if (existsSync(target) && statSync(target).isFile()) return null; + + // Try implicit extensions (.js, .ts, .mjs, .cjs) + for (const ext of ['.js', '.ts', '.mjs', '.cjs']) { + if (!extname(target) && existsSync(target + ext)) return null; + } + + // Try index files (directory import) + if (existsSync(target) && statSync(target).isDirectory()) { + for (const idx of ['index.js', 'index.ts', 'index.mjs']) { + if (existsSync(join(target, idx))) return null; + } + } + + // Not resolved — broken + return specifier; +} + +// ── main ──────────────────────────────────────────────────────────────── +const files = walk(srcDir); +const broken = []; + +for (const file of files) { + const imports = extractDynamicImports(file); + for (const { specifier, line } of imports) { + const bad = resolveSpecifier(specifier, file); + if (bad !== null) { + const rel = file.replace(resolve(srcDir, '..') + '/', '').replace(/\\/g, '/'); + broken.push({ file: rel, line, specifier: bad }); + } + } +} + +if (broken.length === 0) { + console.log(`✓ All dynamic imports in src/ resolve (${files.length} files scanned)`); + process.exit(0); +} else { + console.error(`✗ ${broken.length} broken dynamic import(s) found:\n`); + for (const { file, line, specifier } of broken) { + console.error(` ${file}:${line} → ${specifier}`); + } + console.error('\nFix the import paths and re-run.'); + process.exit(1); +} diff --git a/src/cli/commands/info.js b/src/cli/commands/info.js index df8e35a1..10b6ad74 100644 --- a/src/cli/commands/info.js +++ b/src/cli/commands/info.js @@ -36,7 +36,7 @@ export const command = { console.log(); try { - const { findDbPath, getBuildMeta } = await import('../../db.js'); + const { findDbPath, getBuildMeta } = await import('../../db/index.js'); const Database = (await import('better-sqlite3')).default; const dbPath = findDbPath(); const fs = await import('node:fs'); diff --git a/src/mcp/tools/semantic-search.js b/src/mcp/tools/semantic-search.js index 2fa22ea5..6d965b2c 100644 --- a/src/mcp/tools/semantic-search.js +++ b/src/mcp/tools/semantic-search.js @@ -11,7 +11,7 @@ export async function handler(args, ctx) { }; if (mode === 'keyword') { - const { ftsSearchData } = await import('../../embeddings/index.js'); + const { ftsSearchData } = await import('../../domain/search/index.js'); const result = ftsSearchData(args.query, ctx.dbPath, searchOpts); if (result === null) { return { @@ -28,7 +28,7 @@ export async function handler(args, ctx) { } if (mode === 'semantic') { - const { searchData } = await import('../../embeddings/index.js'); + const { searchData } = await import('../../domain/search/index.js'); const result = await searchData(args.query, ctx.dbPath, searchOpts); if (result === null) { return { @@ -45,7 +45,7 @@ export async function handler(args, ctx) { } // hybrid (default) — falls back to semantic if no FTS5 - const { hybridSearchData, searchData } = await import('../../embeddings/index.js'); + const { hybridSearchData, searchData } = await import('../../domain/search/index.js'); let result = await hybridSearchData(args.query, ctx.dbPath, searchOpts); if (result === null) { result = await searchData(args.query, ctx.dbPath, searchOpts); From ab315f7d2f301c380219b932d68f5c5c0d38ee87 Mon Sep 17 00:00:00 2001 From: carlos-alm <127798846+carlos-alm@users.noreply.github.com> Date: Thu, 19 Mar 2026 04:35:07 -0600 Subject: [PATCH 2/3] fix: address Greptile review feedback on verify-imports (#540) - Broaden regex to catch non-awaited import() calls (return, .then()) - Fix block-comment tracking to handle mid-line /* openings - Replace naive before.includes('//') with quote-aware isInsideLineComment to avoid false negatives on URLs in string literals --- scripts/verify-imports.js | 49 +++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/scripts/verify-imports.js b/scripts/verify-imports.js index fb1a4868..c6d5309e 100644 --- a/scripts/verify-imports.js +++ b/scripts/verify-imports.js @@ -35,8 +35,27 @@ function walk(dir) { } // ── extract dynamic import specifiers ─────────────────────────────────── -// Matches: await import('...') and await import("...") -const DYNAMIC_IMPORT_RE = /await\s+import\(\s*(['"])(.+?)\1\s*\)/g; +// Matches: import('...') and import("...") — with or without await +const DYNAMIC_IMPORT_RE = /(?:await\s+)?import\(\s*(['"])(.+?)\1\s*\)/g; + +/** + * Check whether the text contains a `//` line-comment marker that is NOT + * inside a string literal. Walks character-by-character tracking quote state. + */ +function isInsideLineComment(text) { + let inStr = null; // null | "'" | '"' | '`' + for (let i = 0; i < text.length; i++) { + const ch = text[i]; + if (ch === '\\' && inStr) { i++; continue; } // skip escaped char + if (inStr) { + if (ch === inStr) inStr = null; + continue; + } + if (ch === "'" || ch === '"' || ch === '`') { inStr = ch; continue; } + if (ch === '/' && text[i + 1] === '/') return true; + } + return false; +} function extractDynamicImports(filePath) { const src = readFileSync(filePath, 'utf8'); @@ -52,19 +71,29 @@ function extractDynamicImports(filePath) { if (line.includes('*/')) inBlockComment = false; continue; } - if (/^\s*\/\*/.test(line)) { - if (!line.includes('*/')) inBlockComment = true; - continue; - } // Skip single-line comments if (/^\s*\/\//.test(line)) continue; + // Strip block comments from the line before scanning for imports. + // Handles: mid-line /* ... */ (single-line) and opening /* without close. + let scanLine = line; + if (scanLine.includes('/*')) { + // Remove fully closed inline block comments: code /* ... */ more code + scanLine = scanLine.replace(/\/\*.*?\*\//g, ''); + // If an unclosed /* remains, keep only the part before it and enter block mode + const openIdx = scanLine.indexOf('/*'); + if (openIdx !== -1) { + scanLine = scanLine.slice(0, openIdx); + inBlockComment = true; + } + } + let match; DYNAMIC_IMPORT_RE.lastIndex = 0; - while ((match = DYNAMIC_IMPORT_RE.exec(line)) !== null) { - // Skip if the match is inside a trailing comment - const before = line.slice(0, match.index); - if (before.includes('//') || before.includes('/*')) continue; + while ((match = DYNAMIC_IMPORT_RE.exec(scanLine)) !== null) { + // Skip if the match is inside a trailing line comment (// outside quotes) + const before = scanLine.slice(0, match.index); + if (isInsideLineComment(before)) continue; imports.push({ specifier: match[2], line: i + 1 }); } From 3ee1b89399ec795c732e2150f98f8afbdeac3296 Mon Sep 17 00:00:00 2001 From: carlos-alm <127798846+carlos-alm@users.noreply.github.com> Date: Thu, 19 Mar 2026 05:02:30 -0600 Subject: [PATCH 3/3] fix: scan content after block comment close on same line When */ appeared mid-line, the continue statement skipped the entire line including any real code after the comment close. Now extracts and scans the post-close portion of the line. --- scripts/verify-imports.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/scripts/verify-imports.js b/scripts/verify-imports.js index c6d5309e..bfda6f35 100644 --- a/scripts/verify-imports.js +++ b/scripts/verify-imports.js @@ -67,16 +67,15 @@ function extractDynamicImports(filePath) { const line = lines[i]; // Track block comments (/** ... */ and /* ... */) + let scanLine = line; if (inBlockComment) { - if (line.includes('*/')) inBlockComment = false; - continue; + const closeIdx = scanLine.indexOf('*/'); + if (closeIdx === -1) continue; // still fully inside a block comment + inBlockComment = false; + scanLine = scanLine.slice(closeIdx + 2); // scan content after */ } // Skip single-line comments - if (/^\s*\/\//.test(line)) continue; - - // Strip block comments from the line before scanning for imports. - // Handles: mid-line /* ... */ (single-line) and opening /* without close. - let scanLine = line; + if (/^\s*\/\//.test(scanLine)) continue; if (scanLine.includes('/*')) { // Remove fully closed inline block comments: code /* ... */ more code scanLine = scanLine.replace(/\/\*.*?\*\//g, '');