From 3f56644a055fb053d8acfb3787ac3d420dc3dd2a Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Mon, 23 Feb 2026 20:53:06 -0700 Subject: [PATCH] fix(cli): graceful error for cycles, export, embed when no graph.db exists Use openReadonlyOrFail() instead of raw Database() constructor in the export and cycles command handlers. Add fs.existsSync() guard in buildEmbeddings(). Also export ./package.json in the exports map. Closes #77 Closes #78 Impact: 1 functions changed, 1 affected --- package.json | 3 ++- src/cli.js | 7 +++---- src/embedder.js | 8 ++++++++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 53e378a1c..67c32e07c 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "exports": { ".": { "import": "./src/index.js" - } + }, + "./package.json": "./package.json" }, "bin": { "codegraph": "./src/cli.js" diff --git a/src/cli.js b/src/cli.js index b5a482218..ccf8fbc47 100644 --- a/src/cli.js +++ b/src/cli.js @@ -2,12 +2,11 @@ import fs from 'node:fs'; import path from 'node:path'; -import Database from 'better-sqlite3'; import { Command } from 'commander'; import { buildGraph } from './builder.js'; import { loadConfig } from './config.js'; import { findCycles, formatCycles } from './cycles.js'; -import { findDbPath } from './db.js'; +import { openReadonlyOrFail } from './db.js'; import { buildEmbeddings, EMBEDDING_STRATEGIES, MODELS, search } from './embedder.js'; import { exportDOT, exportJSON, exportMermaid } from './export.js'; import { setVerbose } from './logger.js'; @@ -275,7 +274,7 @@ program .option('--min-confidence ', 'Minimum edge confidence threshold (default: 0.5)', '0.5') .option('-o, --output ', 'Write to file instead of stdout') .action((opts) => { - const db = new Database(findDbPath(opts.db), { readonly: true }); + const db = openReadonlyOrFail(opts.db); const exportOpts = { fileLevel: !opts.functions, noTests: resolveNoTests(opts), @@ -314,7 +313,7 @@ program .option('--include-tests', 'Include test/spec files (overrides excludeTests config)') .option('-j, --json', 'Output as JSON') .action((opts) => { - const db = new Database(findDbPath(opts.db), { readonly: true }); + const db = openReadonlyOrFail(opts.db); const cycles = findCycles(db, { fileLevel: !opts.functions, noTests: resolveNoTests(opts) }); db.close(); diff --git a/src/embedder.js b/src/embedder.js index 67eb39e5d..20882e9bc 100644 --- a/src/embedder.js +++ b/src/embedder.js @@ -324,6 +324,14 @@ export async function buildEmbeddings(rootDir, modelKey, customDbPath, options = const strategy = options.strategy || 'structured'; const dbPath = customDbPath || findDbPath(null); + if (!fs.existsSync(dbPath)) { + console.error( + `No codegraph database found at ${dbPath}.\n` + + `Run "codegraph build" first to analyze your codebase.`, + ); + process.exit(1); + } + const db = new Database(dbPath); initEmbeddingsSchema(db);