tobi/qmd · error · Error

Database path not set. Tests must set INDEX_PATH env var or

Error message

Database path not set. Tests must set INDEX_PATH env var or use createStore() with explicit path. This prevents tests from accidentally writing to the global index.

What it means

getDefaultDbPath() refuses to fall back to the default cache location when not in production mode and INDEX_PATH is unset. This is a safety rail so tests never write to the user's global index.sqlite (~/.cache/qmd/index.sqlite).

Source

Thrown at src/store.ts:644

export function enableProductionMode(): void {
  _productionMode = true;
}

/** Reset production mode flag — only for testing. */
export function _resetProductionModeForTesting(): void {
  _productionMode = false;
}

export function getDefaultDbPath(indexName: string = "index"): string {
  // Always allow override via INDEX_PATH (for testing)
  if (process.env.INDEX_PATH) {
    return process.env.INDEX_PATH;
  }

  // In non-production mode (tests), require explicit path
  if (!_productionMode) {
    throw new Error(
      "Database path not set. Tests must set INDEX_PATH env var or use createStore() with explicit path. " +
      "This prevents tests from accidentally writing to the global index."
    );
  }

  const cacheDir = process.env.XDG_CACHE_HOME || resolve(homedir(), ".cache");
  const qmdCacheDir = resolve(cacheDir, "qmd");
  try { mkdirSync(qmdCacheDir, { recursive: true }); } catch { }
  return resolve(qmdCacheDir, `${indexName}.sqlite`);
}

export function getPwd(): string {
  return process.env.PWD || process.cwd();
}

export function getRealPath(path: string): string {
  try {
    return realpathSync(path);

View on GitHub (pinned to dbfd0b4736)

Solutions

  1. Set process.env.INDEX_PATH to a temp file before any store call in tests
  2. Use createStore() with an explicit path argument
  3. Run with production mode enabled if you genuinely want the global index

Example fix

// before
const db = getDbPath(); // throws in tests
// after
process.env.INDEX_PATH ||= path.join(os.tmpdir(), 'qmd-test.sqlite');
const db = getDbPath();
Defensive patterns

Strategy: validation

Validate before calling

process.env.INDEX_PATH ||= path.join(os.tmpdir(), `qmd-${process.pid}.sqlite`);

Try / catch

try { const db = createStore(); } catch (e) { if (/INDEX_PATH/.test((e as Error).message)) { process.env.INDEX_PATH = tmp(); return createStore(); } throw e; }

Prevention

When it happens

Trigger: Running code in non-production mode (tests, NODE_ENV != production) that constructs a store or calls getDbPath/setIndexName without setting process.env.INDEX_PATH and without passing an explicit path to createStore().

Common situations: A new test file forgot the INDEX_PATH setup hook; a script run with NODE_ENV=test; importing store.ts transitively in a dev tool that instantiates a store lazily.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of tobi/qmd@dbfd0b4736 (2026-08-28). Data as JSON: /api/errors/ffe4ce886a2a3ece. Report an issue: GitHub.