Skip to content

Commit aad7874

Browse files
authored
fix(embed): handle absolute file paths from native engine (#783)
* fix(embed): handle absolute file paths from native engine The native engine stores absolute file paths in the DB. buildEmbeddings unconditionally joined rootDir + file, doubling the path and causing ENOENT for every symbol — producing 0 embeddings silently. Check path.isAbsolute() before joining, and add a regression test that inserts nodes with absolute paths to prevent recurrence. Closes #760 * style: fix biome formatting in test
1 parent 1101d24 commit aad7874

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/domain/search/generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export async function buildEmbeddings(
100100
let overflowCount = 0;
101101

102102
for (const [file, fileNodes] of byFile) {
103-
const fullPath = path.join(rootDir, file);
103+
const fullPath = path.isAbsolute(file) ? file : path.join(rootDir, file);
104104
let lines: string[];
105105
try {
106106
lines = fs.readFileSync(fullPath, 'utf-8').split('\n');

tests/search/embedding-strategy.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,44 @@ describe('FTS5 index built alongside embeddings', () => {
289289
});
290290
});
291291

292+
describe('absolute file paths in DB (#760)', () => {
293+
let absDir: string, absDbPath: string;
294+
295+
beforeAll(() => {
296+
absDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-abspath-test-'));
297+
fs.writeFileSync(path.join(absDir, 'math.js'), 'export function add(a, b) { return a + b; }\n');
298+
299+
const absDbDir = path.join(absDir, '.codegraph');
300+
fs.mkdirSync(absDbDir, { recursive: true });
301+
absDbPath = path.join(absDbDir, 'graph.db');
302+
303+
const db = new Database(absDbPath);
304+
db.pragma('journal_mode = WAL');
305+
initSchema(db);
306+
307+
// Insert node with an absolute file path (as the native engine does)
308+
const absFile = path.join(absDir, 'math.js');
309+
insertNode(db, 'add', 'function', absFile, 1, 1);
310+
db.close();
311+
});
312+
313+
afterAll(() => {
314+
if (absDir) fs.rmSync(absDir, { recursive: true, force: true });
315+
});
316+
317+
test('produces embeddings when DB stores absolute paths', async () => {
318+
EMBEDDED_TEXTS.length = 0;
319+
await buildEmbeddings(absDir, 'minilm', absDbPath);
320+
321+
expect(EMBEDDED_TEXTS.length).toBe(1);
322+
323+
const db = new Database(absDbPath, { readonly: true });
324+
const count = db.prepare('SELECT COUNT(*) as c FROM embeddings').get().c;
325+
db.close();
326+
expect(count).toBe(1);
327+
});
328+
});
329+
292330
describe('context window overflow detection', () => {
293331
let bigDir: string, bigDbPath: string;
294332

0 commit comments

Comments
 (0)