Skip to content

Commit 8346440

Browse files
committed
Add WASM fallbacks for tree-sitter and SQLite, fix installer
Replace native tree-sitter with web-tree-sitter + tree-sitter-wasms for universal cross-platform support. Add node-sqlite3-wasm as a fallback when better-sqlite3 native bindings aren't available. Move better-sqlite3 and sqlite-vss to optionalDependencies so installs never fail. Fix installer to use npx fallback when global npm install fails, so MCP config, hooks, and quick-start instructions all work without the bare codegraph command in PATH. Fix tests: update schema version expectation, fix db test paths and method names, extract MAX_OUTPUT_LENGTH as module constant, normalize Windows path separators in import resolver.
1 parent 429359c commit 8346440

24 files changed

Lines changed: 708 additions & 782 deletions

__tests__/extraction.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@
44
* Tests for the tree-sitter extraction system.
55
*/
66

7-
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
7+
import { describe, it, expect, beforeAll, beforeEach, afterEach } from 'vitest';
88
import * as fs from 'fs';
99
import * as path from 'path';
1010
import * as os from 'os';
1111
import { CodeGraph } from '../src';
1212
import { extractFromSource, scanDirectory, shouldIncludeFile } from '../src/extraction';
13-
import { detectLanguage, isLanguageSupported, getSupportedLanguages } from '../src/extraction/grammars';
13+
import { detectLanguage, isLanguageSupported, getSupportedLanguages, initGrammars } from '../src/extraction/grammars';
1414
import { normalizePath } from '../src/utils';
1515
import { DEFAULT_CONFIG } from '../src/types';
1616

17+
beforeAll(async () => {
18+
await initGrammars();
19+
});
20+
1721
// Create a temporary directory for each test
1822
function createTempDir(): string {
1923
return fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-test-'));

__tests__/foundation.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ describe('Database Connection', () => {
317317

318318
const version = db.getSchemaVersion();
319319
expect(version).not.toBeNull();
320-
expect(version?.version).toBe(1);
320+
expect(version?.version).toBe(2);
321321

322322
db.close();
323323
});

__tests__/pr19-improvements.test.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* - CLI uninit command
1414
*/
1515

16-
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
16+
import { describe, it, expect, beforeAll, beforeEach, afterEach } from 'vitest';
1717
import * as fs from 'fs';
1818
import * as path from 'path';
1919
import * as os from 'os';
@@ -24,8 +24,13 @@ import {
2424
getSupportedLanguages,
2525
clearParserCache,
2626
getUnavailableGrammarErrors,
27+
initGrammars,
2728
} from '../src/extraction/grammars';
2829

30+
beforeAll(async () => {
31+
await initGrammars();
32+
});
33+
2934
// Create a temporary directory for each test
3035
function createTempDir(): string {
3136
return fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-pr19-test-'));
@@ -320,8 +325,9 @@ describe('Database Layer Improvements', () => {
320325
const { DatabaseConnection } = await import('../src/db');
321326
const { QueryBuilder } = await import('../src/db/queries');
322327

323-
const db = DatabaseConnection.initialize(testDir);
324-
const queries = new QueryBuilder(db.getDatabase());
328+
const dbPath = path.join(testDir, 'codegraph.db');
329+
const db = DatabaseConnection.initialize(dbPath);
330+
const queries = new QueryBuilder(db.getDb());
325331

326332
// Insert a node first (needed as foreign key)
327333
queries.insertNode({
@@ -375,8 +381,9 @@ describe('Database Layer Improvements', () => {
375381
const { DatabaseConnection } = await import('../src/db');
376382
const { QueryBuilder } = await import('../src/db/queries');
377383

378-
const db = DatabaseConnection.initialize(testDir);
379-
const queries = new QueryBuilder(db.getDatabase());
384+
const dbPath = path.join(testDir, 'codegraph.db');
385+
const db = DatabaseConnection.initialize(dbPath);
386+
const queries = new QueryBuilder(db.getDb());
380387

381388
// Insert some nodes
382389
for (let i = 0; i < 3; i++) {
@@ -405,8 +412,9 @@ describe('Database Layer Improvements', () => {
405412
it.skipIf(!HAS_SQLITE)('should set performance pragmas on initialization', async () => {
406413
const { DatabaseConnection } = await import('../src/db');
407414

408-
const db = DatabaseConnection.initialize(testDir);
409-
const rawDb = db.getDatabase();
415+
const dbPath = path.join(testDir, 'codegraph.db');
416+
const db = DatabaseConnection.initialize(dbPath);
417+
const rawDb = db.getDb();
410418

411419
// Check pragmas were set
412420
const synchronous = rawDb.pragma('synchronous', { simple: true });
@@ -428,8 +436,9 @@ describe('Database Layer Improvements', () => {
428436
const { DatabaseConnection } = await import('../src/db');
429437
const { QueryBuilder } = await import('../src/db/queries');
430438

431-
const db = DatabaseConnection.initialize(testDir);
432-
const queries = new QueryBuilder(db.getDatabase());
439+
const dbPath = path.join(testDir, 'codegraph.db');
440+
const db = DatabaseConnection.initialize(dbPath);
441+
const queries = new QueryBuilder(db.getDb());
433442

434443
// Should not throw on empty array
435444
expect(() => queries.insertUnresolvedRefsBatch([])).not.toThrow();
@@ -665,28 +674,21 @@ describe('CLI uninit', () => {
665674
// Tree-sitter Version Pinning
666675
// =============================================================================
667676

668-
describe('Tree-sitter Version Pinning', () => {
669-
it('should have exact versions (no caret) in package.json', () => {
677+
describe('Tree-sitter WASM Setup', () => {
678+
it('should use web-tree-sitter and tree-sitter-wasms in dependencies', () => {
670679
const pkgPath = path.join(__dirname, '..', 'package.json');
671680
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
672681

673-
const treeSitterDeps = Object.entries(pkg.dependencies as Record<string, string>)
674-
.filter(([name]) => name.startsWith('tree-sitter') || name.includes('tree-sitter'));
675-
676-
for (const [name, version] of treeSitterDeps) {
677-
// Skip github: references
678-
if (version.startsWith('github:')) continue;
679-
expect(version, `${name} should not use caret range`).not.toMatch(/^\^/);
680-
}
682+
expect(pkg.dependencies['web-tree-sitter']).toBeDefined();
683+
expect(pkg.dependencies['tree-sitter-wasms']).toBeDefined();
681684
});
682685

683-
it('should have tree-sitter override pinned', () => {
686+
it('should not have native tree-sitter in dependencies', () => {
684687
const pkgPath = path.join(__dirname, '..', 'package.json');
685688
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
686689

687-
expect(pkg.overrides).toBeDefined();
688-
expect(pkg.overrides['tree-sitter']).toBeDefined();
689-
expect(pkg.overrides['tree-sitter']).not.toMatch(/^\^/);
690+
expect(pkg.dependencies['tree-sitter']).toBeUndefined();
691+
expect(pkg.overrides).toBeUndefined();
690692
});
691693
});
692694

0 commit comments

Comments
 (0)