#3529·ruflo

test: init-memory-package-resolver-2545 fails whenever vitest runs through pnpm's bin shim (NODE_PATH exposes the workspace's own @claude-flow/memory)

Author: HF-teamdevCreated Sep 28, 2026Updated Sep 28, 2026

@ruvnet @proffesor-for-testing

Summary

Two of the five tests in init-memory-package-resolver-2545.test.ts fail whenever vitest is started through the node_modules/.bin/vitest shim that pnpm generates. That covers pnpm exec vitest, pnpm test, and npm test inside v3/@claude-flow/cli after a pnpm install in v3/. pnpm's shim exports NODE_PATH=<v3>/node_modules/.pnpm/node_modules, which is exactly where the monorepo's own @claude-flow/memory sits. Both "unresolvable" scenarios then resolve it through NODE_PATH. Nothing in src/ is wrong; the tests just aren't isolated from NODE_PATH. CI stays green because the Test Suite installs with npm ci and never goes through a pnpm shim.

Environment

Upstream main at b636802d8 and v3.47.1 (the test, the resolver and the relevant hook code are the same on both). pnpm 8.15.0, vitest 4.1.8, Node 26.9.0, macOS arm64. OS-independent as far as I can tell: the trigger is pnpm's shim, not the platform.

Steps to reproduce

bash
cd v3 && pnpm install --frozen-lockfile && pnpm build
cd @claude-flow/cli
./node_modules/.bin/vitest run __tests__/init-memory-package-resolver-2545.test.ts        # pnpm's shim: 2 failed | 3 passed
node node_modules/vitest/vitest.mjs run __tests__/init-memory-package-resolver-2545.test.ts # vitest directly: 5 passed

Expected vs actual

Both commands should pass. With the shim:

× returns null for an isolated project with no package and no sidecar
  AssertionError: expected '<v3>/node_modules/.pnpm/@[email protected]_…/node_modules/@claude-flow/memory/dist/index.js' to be null
× deployed hook fails LOUD (not a silent skip) when memory is unresolvable
  AssertionError: expected '[AutoMemory] Importing au…' to contain 'self-learning imports are DISABLED'

Inside the test process, process.env.NODE_PATH is:

<v3>/node_modules/.pnpm/[email protected]_…/node_modules/vitest/node_modules
<v3>/node_modules/.pnpm/[email protected]_…/node_modules
<v3>/node_modules/.pnpm/node_modules

The same paths show up in Module.globalPaths and in createRequire(<isolated>/package.json).resolve.paths('@claude-flow/memory'). The shim sets them (node_modules/.bin/vitest, lines 8–11: export NODE_PATH="…").

Root cause

  • The "isolated project" test (#L98-L102) calls resolveMemoryPackageFromProject(). Its strategy 2 (memory-package-resolver.ts#L70-L73) is createRequire(<target>/package.json).resolve('@claude-flow/memory'). Node's CJS resolution consults NODE_PATH after the node_modules walk-up, so a temp directory with nothing in it still resolves the workspace's copy.
  • The "fails LOUD" test (#L120-L130) runs the hook with execFileSync('node', …) and no env (#L57), so the child inherits NODE_PATH. The hook's strategy 2 (auto-memory-hook.mjs#L195-L201) is the same createRequire call, so it finds the package and imports instead of printing the DISABLED banner.
  • CI doesn't see it: the Test Suite installs with npm ci (ci.yml#L72-L73) and runs node scripts/ci-test-ratchet.mjs (#L115). npm's bin links don't set NODE_PATH.

Impact

  • Contributors running the CLI tests from the v3/ pnpm workspace get two red tests for code that isn't broken.
  • The same leak weakens the neighbouring test "deployed hook ACTIVATES self-learning when init has recorded the sidecar" (#L104). Under pnpm the hook resolves the package through NODE_PATH even with no sidecar (that's what the "fails LOUD" failure above shows), so that test can't tell whether the sidecar path works.
  • Runtime is unaffected. Honouring NODE_PATH in the resolver and the hook is reasonable: some setups, mine included, point NODE_PATH at a shared deps folder for exactly this hook.

Suggested fix (test-only; verified)

Check "unresolvable" without NODE_PATH, and start the hook without it:

diff
@@ imports
 import { fileURLToPath } from 'url';
+import Module from 'module';
@@ helpers
+/**
+ * pnpm's node_modules/.bin shims export NODE_PATH=<workspace>/node_modules/.pnpm/node_modules,
+ * where the monorepo's own @claude-flow/memory lives. createRequire() (the resolver's and the
+ * hook's strategy 2) honours NODE_PATH, so "unresolvable" must be checked without it. Node reads
+ * NODE_PATH into Module.globalPaths once at startup, hence _initPaths() after changing it.
+ */
+function withoutNodePath<T>(fn: () => T): T {
+  const saved = process.env.NODE_PATH;
+  const initPaths = () => (Module as unknown as { _initPaths(): void })._initPaths();
+  delete process.env.NODE_PATH;
+  initPaths();
+  try {
+    return fn();
+  } finally {
+    if (saved !== undefined) process.env.NODE_PATH = saved;
+    initPaths();
+  }
+}
+
+function envWithoutNodePath(): NodeJS.ProcessEnv {
+  const env = { ...process.env };
+  delete env.NODE_PATH;
+  return env;
+}
@@ function runHook
-    const stdout = execFileSync('node', [hookPath, cmd], { cwd, encoding: 'utf-8' });
+    const stdout = execFileSync('node', [hookPath, cmd], { cwd, encoding: 'utf-8', env: envWithoutNodePath() });
@@ it('returns null for an isolated project with no package and no sidecar')
-    expect(resolveMemoryPackageFromProject(nowhere)).toBeNull();
+    expect(withoutNodePath(() => resolveMemoryPackageFromProject(nowhere))).toBeNull();

With this change, run through pnpm's shim (NODE_PATH set): 5 passed (5), with both the v3.47.1 hook and main's hook. Run directly, it stays at 5 / 5. Because runHook no longer passes NODE_PATH, the ACTIVATES test now genuinely exercises the sidecar.

Alternatives considered

  1. Make the resolver and the hook ignore NODE_PATH. Rejected: that changes runtime behaviour setups rely on, and it would diverge from Node's documented resolution.
  2. Run the isolated check in a child process without NODE_PATH. This avoids Module._initPaths() (an internal, though long-stable, Node API), but the child would need the built dist/ or a TS loader. The test imports ../src directly today, and in-process keeps it working without a build.
  3. test.env in the vitest config. On its own this can't fix the in-process case, because Node copies NODE_PATH into Module.globalPaths when the worker starts.

How it was found

My fork's build runs the upstream CLI tests that exercise code its patches touch, from the v3/ pnpm workspace. These two failed on an unmodified v3.47.1 as well, which ruled out the patches. Tracing that led to pnpm's shim.

Happy to open a PR with the patch above if that's useful.