From 0539188858b4302428d3231e85a79d9b9b0981e2 Mon Sep 17 00:00:00 2001 From: Harlan Zhou Date: Sun, 26 Apr 2026 02:37:55 +0000 Subject: [PATCH] fix: use os.homedir() instead of process.env.HOME for HF cache dir On Windows, HOME env is often unset, causing cache to be written to './undefined/'. Using os.homedir() ensures cross-platform compatibility while preserving HF_HOME priority. Fixes #1068 --- gitnexus/src/core/embeddings/embedder.ts | 3 ++- gitnexus/src/mcp/core/embedder.ts | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/gitnexus/src/core/embeddings/embedder.ts b/gitnexus/src/core/embeddings/embedder.ts index d27718ce54..66be646c1e 100644 --- a/gitnexus/src/core/embeddings/embedder.ts +++ b/gitnexus/src/core/embeddings/embedder.ts @@ -15,6 +15,7 @@ if (!process.env.ORT_LOG_LEVEL) { } import { pipeline, env, type FeatureExtractionPipeline } from '@huggingface/transformers'; +import os from 'os'; import { existsSync } from 'fs'; import { execFileSync } from 'child_process'; import { join, dirname } from 'path'; @@ -161,7 +162,7 @@ export const initEmbedder = async ( // ./node_modules/.cache inside its own install dir, which is unwritable // when gitnexus is installed globally (e.g. /usr/lib/node_modules/). // Respect HF_HOME if set, otherwise fall back to ~/.cache/huggingface. - env.cacheDir = process.env.HF_HOME ?? `${process.env.HOME}/.cache/huggingface`; + env.cacheDir = process.env.HF_HOME ?? join(os.homedir(), '.cache', 'huggingface'); const isDev = process.env.NODE_ENV === 'development'; if (isDev) { diff --git a/gitnexus/src/mcp/core/embedder.ts b/gitnexus/src/mcp/core/embedder.ts index 592c2bba91..9523b13058 100644 --- a/gitnexus/src/mcp/core/embedder.ts +++ b/gitnexus/src/mcp/core/embedder.ts @@ -6,6 +6,8 @@ */ import { pipeline, env, type FeatureExtractionPipeline } from '@huggingface/transformers'; +import os from 'os'; +import { join } from 'path'; import { isHttpMode, getHttpDimensions, @@ -46,7 +48,7 @@ export const initEmbedder = async (): Promise => { // ./node_modules/.cache inside its own install dir, which is unwritable // when gitnexus is installed globally (e.g. /usr/lib/node_modules/). // Respect HF_HOME if set, otherwise fall back to ~/.cache/huggingface. - env.cacheDir = process.env.HF_HOME ?? `${process.env.HOME}/.cache/huggingface`; + env.cacheDir = process.env.HF_HOME ?? join(os.homedir(), '.cache', 'huggingface'); console.error('GitNexus: Loading embedding model (first search may take a moment)...');