fix(cli): do not enable production mode at module import time

src/cli/qmd.ts has the same module-scope enableProductionMode() call that
src/mcp/server.ts had — and the same test-isolation leak. test/cli.test.ts
imports buildEditorUri and termLink from this module, which executes the
top-level enableProductionMode() as a side effect of import, flipping the
global _productionMode flag for every later test file in the Bun process.

This is the actual driver of the Store Creation > createStore throws
without explicit path in test mode failure — test/cli.test.ts runs
alphabetically before test/store.test.ts, so the flag is already true by
the time store.test.ts checks it.

Mirror the fix applied to src/mcp/server.ts in the previous commit: move
enableProductionMode() from module scope into the if (isMain) guard so
the flag is only flipped when qmd is actually invoked as the CLI
entrypoint, not when the module is imported for its exports.
This commit is contained in:
Pi 2026-04-23 17:41:10 +00:00
parent 54262f566c
commit a0c460333b

View File

@ -101,9 +101,12 @@ import {
} from "../collections.js";
import { getEmbeddedQmdSkillContent, getEmbeddedQmdSkillFiles } from "../embedded-skills.js";
// Enable production mode - allows using default database path
// Tests must set INDEX_PATH or use createStore() with explicit path
enableProductionMode();
// NOTE: enableProductionMode() is intentionally NOT called at module scope here.
// Importing this module for its exports (e.g. buildEditorUri, termLink from
// test/cli.test.ts) must not flip the global production flag, as that leaks
// into unrelated tests that rely on the default (development) database path
// resolution. The flag is flipped inside the CLI's main-module guard below so
// it only fires when qmd is actually invoked as a script.
// =============================================================================
// Store/DB lifecycle (no legacy singletons in store.ts)
@ -2821,6 +2824,11 @@ const isMain = argv1 === __filename
|| argv1?.endsWith("/qmd.js")
|| (argv1 != null && realpathSync(argv1) === __filename);
if (isMain) {
// Flip to production mode only when this module is executed as the CLI
// entrypoint, not when imported for its exports. Tests must set INDEX_PATH
// or use createStore() with an explicit path.
enableProductionMode();
const cli = parseCLI();
if (cli.values.version) {