Skip to content

fix: lazy read NEMAR_CONFIG_DIR in config store (#489) - #490

Merged
neuromechanist merged 2 commits into
devfrom
feature/issue-489-config-lazy-init
May 14, 2026
Merged

neuromechanist merged 2 commits into
devfrom
feature/issue-489-config-lazy-init

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Summary

  • src/lib/config.ts no longer captures NEMAR_CONFIG_DIR at module load. A new getStore() resolves the env var on each call, caches the Conf instance by dir, and rebuilds + reruns structural migrations once when the env flips between calls.
  • migrateConfig() / migrateApiUrl() are now triggered lazily on first config access. Production behavior is unchanged: the first command-handler call (e.g. getConfig()) triggers the migration before returning.
  • test/config-migrate-api-url.test.ts test 1 calls migrateApiUrl() explicitly so it runs against the test's NEMAR_CONFIG_DIR regardless of which test file imported src/lib/config.ts first.

Why

Module-load capture of NEMAR_CONFIG_DIR made bun test results depend on file load order. If any earlier test imported src/lib/config.ts before config-migrate-api-url.test.ts's beforeAll set the env var, the Conf instance was already pinned to the wrong dir and the migration tests asserted against a file the module never read.

Closes #489.

Test plan

  • bun test test/config-migrate-api-url.test.ts (3/3 pass)
  • bun test test/publish-progress.test.ts test/config-migrate-api-url.test.ts (config loaded first, still pass)
  • bun test test/auth-status.test.ts test/config-migrate-api-url.test.ts (same, pass)
  • bun run build — green
  • CLI smoke: bun dist/index.js auth status reads config and lists accounts

Drop the module-load capture of NEMAR_CONFIG_DIR (and the eager Conf
instance) so test ordering no longer decides which config dir wins. The
new getStore() resolves the env var on each call, caches the Conf
instance keyed by dir, and rebuilds it (rerunning structural migrations
once per dir) when the env flips between calls.

Production: first config accessor triggers migration before returning,
preserving the old startup behavior.

Tests: update config-migrate-api-url test 1 to call migrateApiUrl()
explicitly so it runs against the test's NEMAR_CONFIG_DIR regardless of
whether another test file imported src/lib/config.ts first.

Fixes #489
@github-actions

github-actions Bot commented May 14, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR490

Version: 0.8.15-PR490.fbf13c3
Commit: fbf13c3


🤖 Updated on each push.

- update-check.ts: same module-load env capture as the old config.ts;
  swap CONFIG_DIR/CACHE_FILE constants for getConfigDir()/getCacheFile()
  helpers so tests setting NEMAR_CONFIG_DIR after import always win.
- config.ts:
  - Clarify the re-entrancy comment on the migration block. The cache
    short-circuit (cachedStore && cachedStoreDir === dir) is the actual
    guard against recursion; migrationsRunForDirs covers the separate
    dir-flip-and-revisit case.
  - setConfig: read activeAccount/accounts directly off the cached
    Conf instance instead of going back through getActiveAccountName /
    getAccountsMap. Same behavior, fewer cache lookups.
  - Add __resetStoreCacheForTesting() so tests can force a fresh init
    for the same dir (bun --rerun-each, isolated retests).
- test/config-migrate-api-url.test.ts: rewrite the stale test 3
  comment. The 'previous tests already locked NEMAR_CONFIG_DIR' line
  described the old eager-capture behavior and was misleading.

Review findings: #490
@neuromechanist

Copy link
Copy Markdown
Contributor Author

Review triage (from /pr-review-toolkit:review-pr + single-reviewer pass)

Severity Finding Action
Important Stale comment in test 3 (test/config-migrate-api-url.test.ts:122-125) described old locking Fixed in fbf13c3
Important Re-entrancy comment at src/lib/config.ts:201-203 mis-attributes the guard Fixed in fbf13c3
Important src/lib/update-check.ts:23 has the same eager NEMAR_CONFIG_DIR capture anti-pattern Fixed in fbf13c3 (lazy getConfigDir()/getCacheFile())
Suggestion setConfig() calls getStore() 3x redundantly Fixed in fbf13c3 (one cache read, derive locally)
Suggestion No __resetForTesting() hook for --rerun-each Added __resetStoreCacheForTesting() in fbf13c3
Suggestion Test plan only lists paired-file runs, not full suite CI's unit-test-all covers the full suite; no code change needed
Suggestion No dedicated test for migrateConfig() through the lazy path Covered transitively by auth/account tests; skipping as a separate test would be redundant
Pre-existing new Conf() error path not handled Out of scope for this PR (same in old code); file separately if it becomes a real failure mode

@neuromechanist
neuromechanist merged commit f90be01 into dev May 14, 2026
9 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-489-config-lazy-init branch May 18, 2026 11:11

This branch was previously deployed

1 inactive deployment
npm — fbf13c3d Deployed May 14, 2026 by neuromechanist via publish #882
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant