fix: respect env vars and settings.json for DATA_DIR resolution (#1344)

SettingsDefaultsManager.get() returned only the hardcoded default,
ignoring both environment variables and settings.json overrides. This
meant CLAUDE_MEM_DATA_DIR set via env or settings file had no effect
on paths.ts (and other callers using get()).

Two changes:
- get() now checks process.env before falling back to the default
- paths.ts resolves DATA_DIR with full priority: env var > settings.json
  at the default location > hardcoded default

The settings file read uses a synchronous bootstrap pattern to avoid
circular dependencies (DATA_DIR is needed to locate the settings file,
so we check the default path only).

Fixes #1303

Signed-off-by: umut-polat <52835619+umut-polat@users.noreply.github.com>
This commit is contained in:
Umut Polat
2026-03-13 05:57:55 +03:00
committed by GitHub
parent 9dbf63f5d4
commit 88be01910b
2 changed files with 38 additions and 3 deletions
+31 -1
View File
@@ -24,7 +24,37 @@ const _dirname = getDirname();
*/
// Base directories
export const DATA_DIR = SettingsDefaultsManager.get('CLAUDE_MEM_DATA_DIR');
// Resolve DATA_DIR with full priority: env var > settings.json > default.
// SettingsDefaultsManager.get() handles env > default. For settings file
// support, we do a one-time synchronous read of the default settings path
// to check if the user configured a custom DATA_DIR there.
function resolveDataDir(): string {
// 1. Environment variable (highest priority) — already handled by get()
if (process.env.CLAUDE_MEM_DATA_DIR) {
return process.env.CLAUDE_MEM_DATA_DIR;
}
// 2. Settings file at the default location
const defaultDataDir = join(homedir(), '.claude-mem');
const settingsPath = join(defaultDataDir, 'settings.json');
try {
if (existsSync(settingsPath)) {
const { readFileSync } = require('fs');
const raw = JSON.parse(readFileSync(settingsPath, 'utf-8'));
const settings = raw.env ?? raw; // handle legacy nested schema
if (settings.CLAUDE_MEM_DATA_DIR) {
return settings.CLAUDE_MEM_DATA_DIR;
}
}
} catch {
// settings file missing or corrupt — fall through to default
}
// 3. Hardcoded default
return defaultDataDir;
}
export const DATA_DIR = resolveDataDir();
// Note: CLAUDE_CONFIG_DIR is a Claude Code setting, not claude-mem, so leave as env var
export const CLAUDE_CONFIG_DIR = process.env.CLAUDE_CONFIG_DIR || join(homedir(), '.claude');