Repository navigation
feat(#539): detect stray $HOME/node_modules/sequant on startup - #553
Merged
Merged
Conversation
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
5 tasks
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.
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.
Summary
getInstallRoot()andisHomeStrayInstall()tosrc/lib/version-check.ts. The first walks up from__dirnameto the directory holding sequant'spackage.json(mirroring the existinggetCurrentVersion()pattern). The second compares that path topath.join(os.homedir(), "node_modules", "sequant")exactly.bin/cli.tsso 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.src/lib/version-check.test.tscovering home-stray, project-local, global, npx-cache, and the null-install-root case. Mocksos.homedir()because macOS reads the OS-level home regardless ofprocess.env.HOME.Test plan
npm run buildpassesnpm run lintpasses (--max-warnings 0)npm test— all 2678 tests green; 5 new ones inversion-check.test.tsnpx tsx bin/cli.ts --helpruns cleanly from the worktree (no warning, expected — worktree path is not$HOME/node_modules/sequant)$HOME/node_modules/sequant, runnpx sequant doctor, confirm the targeted warning fires with the resolved path and cleanup guidanceAC Coverage
src/lib/version-check.ts:111-128$HOME/node_modules/sequantbin/cli.ts:82-93bin/cli.ts:94-102(byte-for-byte preserved)falsesrc/lib/version-check.test.ts:155-185Closes #539