Skip to content

Bun on macOS: sqlite-vec fails because Apple SQLite omits extension loading #238

@mogsl

Description

@mogsl

Problem

When running qmd under Bun on macOS, vector embedding fails at startup with:

sqlite-vec is not available. Vector operations require a SQLite build with extension loading support.
    at ensureVecTableInternal (store.js:608)

Root cause: Bun on macOS uses Apple's system SQLite for performance, but Apple's build sets SQLITE_OMIT_LOAD_EXTENSION, which disables sqlite3_load_extension(). The sqlite-vec npm packages are installed and the native vec0.dylib is present, but db.loadExtension() is rejected by the runtime.

Environment

  • qmd 1.0.7
  • Bun 1.3.9
  • macOS arm64 (Darwin 23.1.0)
  • sqlite-vec 0.1.7 + sqlite-vec-darwin-arm64 (vec0.dylib present)
  • Homebrew SQLite installed at /opt/homebrew/opt/sqlite/lib/libsqlite3.dylib

Fix

Bun exposes Database.setCustomSQLite(path) to swap out Apple's restricted SQLite for a vanilla build that supports extension loading. The Bun branch in db.ts needs to call this before creating any Database instances.

Workaround patch applied to dist/db.js:

if (isBun) {
    const bunSqlite = "bun:" + "sqlite";
    _Database = (await import(bunSqlite)).Database;
    // macOS: Apple's SQLite has SQLITE_OMIT_LOAD_EXTENSION; swap in Homebrew's vanilla build
    if (process.platform === "darwin") {
        const brewPrefix = process.env.BREW_PREFIX || (process.arch === "arm64" ? "/opt/homebrew" : "/usr/local");
        const customPath = `${brewPrefix}/opt/sqlite/lib/libsqlite3.dylib`;
        try {
            _Database.setCustomSQLite(customPath);
        } catch (e) {
            console.warn(`[qmd] failed to load Homebrew SQLite from ${customPath}: ${e.message}`);
        }
    }
    const { getLoadablePath } = await import("sqlite-vec");
    _sqliteVecLoad = (db) => db.loadExtension(getLoadablePath());
}

After applying this patch, qmd embed completes successfully and vector search works.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions