Skip to content

feat(#539): detect stray $HOME/node_modules/sequant on startup - #553

Merged
admarble merged 4 commits into
mainfrom
feature/539-cli-detect-stray-home-node-modules
Apr 26, 2026
Merged

admarble merged 4 commits into
mainfrom
feature/539-cli-detect-stray-home-node-modules

Conversation

@admarble

Copy link
Copy Markdown
Contributor

Summary

  • Adds getInstallRoot() and isHomeStrayInstall() to src/lib/version-check.ts. The first walks up from __dirname to the directory holding sequant's package.json (mirroring the existing getCurrentVersion() pattern). The second compares that path to path.join(os.homedir(), "node_modules", "sequant") exactly.
  • Updates bin/cli.ts so the home-stray case emits a distinct warning that names the resolved path and the cleanup commands; project-local installs still get the existing generic warning unchanged. Globals and the npx cache are not flagged with the home-stray warning.
  • Adds 5 unit tests in src/lib/version-check.test.ts covering home-stray, project-local, global, npx-cache, and the null-install-root case. Mocks os.homedir() because macOS reads the OS-level home regardless of process.env.HOME.

Test plan

  • npm run build passes
  • npm run lint passes (--max-warnings 0)
  • npm test — all 2678 tests green; 5 new ones in version-check.test.ts
  • npx tsx bin/cli.ts --help runs cleanly from the worktree (no warning, expected — worktree path is not $HOME/node_modules/sequant)
  • Manual end-to-end (post-merge optional): construct $HOME/node_modules/sequant, run npx sequant doctor, confirm the targeted warning fires with the resolved path and cleanup guidance

AC Coverage

AC Status Evidence
AC-1 CLI resolves its own install path on startup MET src/lib/version-check.ts:111-128
AC-2 Distinct warning when path equals $HOME/node_modules/sequant MET bin/cli.ts:82-93
AC-3 Project-local generic warning unchanged MET bin/cli.ts:94-102 (byte-for-byte preserved)
AC-4 Globals and npx cache not flagged with home-stray warning MET Exact path-equality check; tests verify both cases return false
AC-5 Unit tests cover all four cases MET src/lib/version-check.test.ts:155-185

Closes #539

A `npm install sequant` accidentally run from $HOME pollutes resolution
for every subdirectory of home — Node walks up to that stale install
before falling back to the npx cache, so `npx sequant` silently runs
the home-stray copy. The existing generic "local node_modules" warning
doesn't communicate the home-specific footgun or its cleanup steps.

Add `getInstallRoot()` (walks up from __dirname to the directory that
holds sequant's package.json) and `isHomeStrayInstall()` (compares that
path to $HOME/node_modules/sequant exactly). When detected at startup,
bin/cli.ts emits a distinct warning naming the resolved path and the
two cleanup commands, plus the legitimate alternatives. Project-local
installs keep the existing generic warning; global and npx-cache paths
are not flagged. Unit tests cover all four cases.

Closes #539
QA second-look surfaced three material gaps in the initial implementation:

1. AC-4 literal: globals at e.g. /usr/local/lib/node_modules/sequant still
   triggered the project-local generic warning because isLocalNodeModulesInstall
   only excluded the npx cache. Add isGlobalInstall() (matches the
   <prefix>/lib/node_modules/sequant convention used by npm, Homebrew, and nvm)
   and exclude its hits from isLocalNodeModulesInstall.

2. Parent-path extraction in the home-stray cleanup line used a
   forward-slash-only regex (replace(/\/sequant$/, "")). Replace with
   path.dirname() so Windows backslashes are handled by construction.

3. The warning emission was never asserted by an automated test — only the
   predicate was. Extract buildHomeStrayWarning(installRoot) as a pure render
   function and add 4 unit tests covering the path, parent, lockfile cleanup,
   and alternative-action lines.

Also: bin/cli.ts now resolves the install root once and passes it to both
isHomeStrayInstall and the warning builder, eliminating a redundant FS walk
on the hit path. getInstallRoot is annotated @internal to signal it is not
a stable public API surface.

Test impact: 12 new unit tests (5 isGlobalInstall, 4 isLocalNodeModulesInstall
covering all four path classes, 4 buildHomeStrayWarning); 51 total in
version-check.test.ts; full suite 2690 green.
Second-look pass on the prior fix surfaced one real gap and two minor items:

1. isGlobalInstall only matched the POSIX `<prefix>/lib/node_modules/`
   convention. Windows globals default to `%AppData%\npm\node_modules\<pkg>`
   (sometimes nested under `Roaming\`) with no `lib\` segment, so a Windows
   global would still trigger the project-local "use npm update sequant"
   warning. Extend the predicate to also match
   `/AppData/(Roaming/)?npm/node_modules/sequant` after backslash
   normalization. Two new tests cover both Windows variants.

2. Replaced `installRoot!` non-null assertion in bin/cli.ts with an
   explicit `installRoot && isHomeStrayInstall(installRoot)` guard.
   Functionally identical; less brittle to future predicate refactors.

3. Added @internal JSDoc tag to buildHomeStrayWarning to signal it's
   exported for the bin/cli.ts caller and unit tests, not as a stable
   public API surface (mirrors the @internal tag on getInstallRoot).

CHANGELOG entry updated to reflect Windows global coverage.

Test impact: 2 new tests in isGlobalInstall (Windows AppData variants);
53 total in version-check.test.ts; full suite 2692 green.
Two tests, both addressing gaps surfaced by an adversarial review pass:

1. Smoke test for isLocalNodeModulesInstall() called with no args. The
   original test was dropped when the describe block was restructured for
   explicit-path cases. Restoring it covers the default __dirname codepath
   so a future change to the parameter default won't slip through.

2. Integration test for isLocalNodeModulesInstall with a Windows global
   path (C:\\Users\\...\\AppData\\Roaming\\npm\\node_modules\\sequant).
   The exclusion is implemented by delegating to isGlobalInstall, which
   IS unit-tested for Windows variants — but no test verified the
   integration through isLocalNodeModulesInstall itself. If the regex in
   isGlobalInstall ever drifts from the substring check that wraps it,
   only this integration test would catch the user-visible regression.

55 tests in version-check.test.ts; full suite still green.
@admarble
admarble merged commit fa2cc7d into main Apr 26, 2026
5 checks passed
@admarble
admarble deleted the feature/539-cli-detect-stray-home-node-modules branch April 26, 2026 15:18
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.

feat(cli): detect stray $HOME/node_modules/sequant and emit targeted warning

1 participant