fix: use os.homedir() instead of process.env.HOME for HF cache dir (#1078)

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
This commit is contained in:
Harlan Zhou 2026-04-26 13:42:41 +08:00 committed by GitHub
parent 441745c124
commit 13abf192e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 5 additions and 2 deletions

View file

@ -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) {

View file

@ -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<FeatureExtractionPipeline> => {
// ./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)...');