From dfccbf110a7d771810be35c82de5ec6623aae77e Mon Sep 17 00:00:00 2001 From: Michael Mainguy Date: Sun, 23 Nov 2025 10:36:25 -0600 Subject: [PATCH] Fix WASM backend configuration for Alpine Linux MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Set ONNXRUNTIME_BACKEND environment variable before module imports to prevent onnxruntime-node from loading. The environment variable must be set before @xenova/transformers is imported. Updated all index scripts (index-docs, index-api, index-source) to configure the backend at the script entry point. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- scripts/index-api.ts | 6 ++++++ scripts/index-docs.ts | 6 ++++++ scripts/index-source.ts | 6 ++++++ src/search/lancedb-indexer.ts | 9 +++------ 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/scripts/index-api.ts b/scripts/index-api.ts index 73e09ae..8e86a03 100644 --- a/scripts/index-api.ts +++ b/scripts/index-api.ts @@ -1,3 +1,9 @@ +// MUST set environment variable before any imports that use @xenova/transformers +// This prevents onnxruntime-node from being loaded on Alpine Linux (musl libc) +if (process.env.TRANSFORMERS_BACKEND === 'wasm' || process.env.TRANSFORMERS_BACKEND === 'onnxruntime-web') { + process.env.ONNXRUNTIME_BACKEND = 'wasm'; +} + import { ApiIndexer } from '../src/search/api-indexer.js'; import path from 'path'; diff --git a/scripts/index-docs.ts b/scripts/index-docs.ts index a6dd2f4..153b76b 100644 --- a/scripts/index-docs.ts +++ b/scripts/index-docs.ts @@ -1,5 +1,11 @@ #!/usr/bin/env tsx +// MUST set environment variable before any imports that use @xenova/transformers +// This prevents onnxruntime-node from being loaded on Alpine Linux (musl libc) +if (process.env.TRANSFORMERS_BACKEND === 'wasm' || process.env.TRANSFORMERS_BACKEND === 'onnxruntime-web') { + process.env.ONNXRUNTIME_BACKEND = 'wasm'; +} + import { LanceDBIndexer, DocumentSource } from '../src/search/lancedb-indexer.js'; import path from 'path'; import { fileURLToPath } from 'url'; diff --git a/scripts/index-source.ts b/scripts/index-source.ts index a74c1f8..4540fae 100644 --- a/scripts/index-source.ts +++ b/scripts/index-source.ts @@ -1,3 +1,9 @@ +// MUST set environment variable before any imports that use @xenova/transformers +// This prevents onnxruntime-node from being loaded on Alpine Linux (musl libc) +if (process.env.TRANSFORMERS_BACKEND === 'wasm' || process.env.TRANSFORMERS_BACKEND === 'onnxruntime-web') { + process.env.ONNXRUNTIME_BACKEND = 'wasm'; +} + import { SourceCodeIndexer } from '../src/search/source-code-indexer.js'; async function main() { diff --git a/src/search/lancedb-indexer.ts b/src/search/lancedb-indexer.ts index 7e72d2b..dd62ccc 100644 --- a/src/search/lancedb-indexer.ts +++ b/src/search/lancedb-indexer.ts @@ -58,13 +58,10 @@ export class LanceDBIndexer { console.log('Initializing LanceDB connection...'); this.db = await connect(this.dbPath); - // Configure ONNX Runtime backend based on environment variable - // TRANSFORMERS_BACKEND=wasm for Alpine Linux (musl libc) - // Default: native ONNX Runtime (faster, requires glibc) - const backend = process.env.TRANSFORMERS_BACKEND; - if (backend === 'wasm' || backend === 'onnxruntime-web') { + // Log which backend is being used + const backend = process.env.ONNXRUNTIME_BACKEND; + if (backend === 'wasm') { console.log('Using WASM backend for Transformers.js (Alpine/musl compatibility mode)'); - process.env.ONNXRUNTIME_BACKEND = 'wasm'; } else { console.log('Using native ONNX Runtime backend (glibc required)'); }