fix(mcp): do not enable production mode at module import time#602
Merged
Conversation
The top-level enableProductionMode() call added in tobi#537 fixed a real issue (MCP server resolving the wrong database path at startup) but introduced test-isolation breakage as a side effect: merely importing src/mcp/server.ts flipped the global _productionMode flag, which then broke unrelated tests that depend on the default (development) database path resolution. This shows up concretely as "Store Creation > createStore throws without explicit path in test mode" failing on Bun (ubuntu-latest) in CI, because test/mcp.test.ts imports startMcpHttpServer from this module. Move the enableProductionMode() call from module scope into the two server entry points (startMcpServer and startMcpHttpServer). The fix originally intended by tobi#537 — ensuring production mode is active before getDefaultDbPath runs — is preserved because both call sites still flip the flag before createStore / getDefaultDbPath. Importing the module for its exports no longer mutates global state. Verified locally against current main: CI failure on Bun (ubuntu-latest) reproduces on unmodified upstream main (14+ consecutive failed runs since tobi#537 merged on 2026-04-09) and is resolved by this change. Refs: tobi#537
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 was referenced Apr 23, 2026
lucndm
pushed a commit
to lucndm/qmd
that referenced
this pull request
Jun 7, 2026
…-leak fix(mcp): do not enable production mode at module import time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Upstream CI on
mainhas been failing onBun (ubuntu-latest)since 2026-04-09 — 14+ consecutive runs — with a single test failure:Root Cause
#537 fixed a real bug — the MCP server was resolving the wrong database path at startup — by adding
enableProductionMode()at module scope insrc/mcp/server.ts:This flips the global
_productionModeflag as a side effect of importing the module.test/mcp.test.tsimportsstartMcpHttpServerfrom this module, and while the HTTP Transportdescribeblock isskipIf(!!process.env.CI), the import happens regardless of skip status. So_productionModeflips totruein CI, which later makesstore.test.tsfail:In production mode,
getDefaultDbPath()resolves a cache path instead of throwing — so the assertion fails.Fix
Move
enableProductionMode()from module scope into the two server entry points —startMcpServer()(stdio) andstartMcpHttpServer()(HTTP). Both entry points still flip the flag beforegetDefaultDbPath()runs, so #537's original fix is preserved. Merely importing the module for its exports no longer mutates global state.Testing
Verified against current
main:main(as expected — every main run since fix(mcp): call enableProductionMode before getDefaultDbPath #537 has failed).qmd mcpandqmd mcp --httpstill enter production mode before path resolution — behaviour identical to before for end users.Notes
This is a module-scope vs function-scope question rather than a conceptual disagreement with #537. If you'd prefer a different approach (e.g. explicit
getDefaultDbPath({ production: true })parameter, or moving the whole path-resolution logic), happy to adjust.Relationship to #411 and #537
qmd mcpcrashed on startup becauseenableProductionMode()was never called beforegetDefaultDbPath().src/mcp/server.ts.startMcpServer(stdio) andstartMcpHttpServer(HTTP) still enable production mode before resolving the database path — but moves the call out of module scope so importing the module for its exports (e.g. fromtest/mcp.test.ts) does not leak the flag into unrelated tests.Net effect on #411's original symptom: unchanged (still fixed). Net effect on CI:
Bun (ubuntu-latest)goes green again.