diff --git a/scripts/ensure-native-deps.cjs b/scripts/ensure-native-deps.cjs index bbb9f2a..ca05b39 100644 --- a/scripts/ensure-native-deps.cjs +++ b/scripts/ensure-native-deps.cjs @@ -41,6 +41,22 @@ function printFailures(failures) { } } +function tryRebuild() { + const { execSync } = require("node:child_process"); + const packages = nativePackages.join(" "); + try { + log(`native binaries incompatible, rebuilding from source: npm rebuild ${packages} --build-from-source`); + execSync(`npm rebuild ${packages} --build-from-source`, { + cwd: rootDir, + stdio: "inherit", + }); + return true; + } catch (err) { + warn(`rebuild failed: ${err && err.message ? err.message : String(err)}`); + return false; + } +} + function main() { const initialFailures = verifyNativePackages(); if (initialFailures.length === 0) { @@ -49,9 +65,22 @@ function main() { } printFailures(initialFailures); - warn("native dependency verification failed"); - warn("install build tools first, then run: npm rebuild better-sqlite3 sqlite-vec --build-from-source"); - warn("Debian/Ubuntu example: apt-get update && apt-get install -y python3 make gcc g++"); + + if (!tryRebuild()) { + warn("install build tools first, then reinstall the plugin"); + warn("Debian/Ubuntu: apt-get update && apt-get install -y python3 make gcc g++"); + process.exit(1); + } + + const afterFailures = verifyNativePackages(); + if (afterFailures.length === 0) { + log("native dependencies rebuilt and verified"); + return; + } + + printFailures(afterFailures); + warn("rebuild succeeded but native packages still fail to load"); + warn("Debian/Ubuntu: apt-get update && apt-get install -y python3 make gcc g++"); process.exit(1); }