Skip to content

Commit 2d6bed6

Browse files
committed
feat(#539): detect stray $HOME/node_modules/sequant on startup
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
1 parent 17eaf2f commit 2d6bed6

4 files changed

Lines changed: 133 additions & 12 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- **Stray `$HOME/node_modules/sequant` detection** — CLI startup now resolves its own install root and emits a distinct warning when sequant is running from `$HOME/node_modules/sequant` exactly. The warning names the resolved path and lists the cleanup commands (`remove $HOME/node_modules`, `remove $HOME/package.json` and `package-lock.json`) plus the legitimate alternatives (`npm install -g sequant`, Claude Code plugin). Project-local installs continue to receive the existing generic warning unchanged; global and npx-cache installs are not flagged. New exports: `getInstallRoot()`, `isHomeStrayInstall()` in `src/lib/version-check.ts`. (#539)
13+
1014
### Changed
1115

1216
- **/docs skill classifier** routes infra/scaffold paths (`package.json`, `*.config.*`, `.env.example`, layout/page files, middleware) to the `developer-tool` template instead of falling through to `admin`. Default fallback is now `developer-tool`; `admin` template is opt-in via `/admin/` path only (#531)

‎bin/cli.ts‎

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import { fileURLToPath } from "url";
1111
import { dirname, resolve } from "path";
1212
import { readFileSync } from "fs";
1313
import { initCommand } from "../src/commands/init.js";
14-
import { isLocalNodeModulesInstall } from "../src/lib/version-check.js";
14+
import {
15+
getInstallRoot,
16+
isHomeStrayInstall,
17+
isLocalNodeModulesInstall,
18+
} from "../src/lib/version-check.js";
1519
import { configureUI, banner } from "../src/lib/cli-ui.js";
1620
import { isCI, isStdoutTTY } from "../src/lib/tty.js";
1721
import {
@@ -74,17 +78,33 @@ configureUI({
7478
minimal: process.env.SEQUANT_MINIMAL === "1",
7579
});
7680

77-
// Warn if running from local node_modules (not npx cache or global)
78-
// This helps users who accidentally have a stale local install
79-
if (!process.argv.includes("--quiet") && isLocalNodeModulesInstall()) {
80-
const pmCommands = getPackageManagerCommands(detectPackageManagerSync());
81-
console.warn(
82-
chalk.yellow(
83-
"! Running sequant from local node_modules\n" +
84-
" For latest version: npx sequant@latest\n" +
85-
` To remove local: ${pmCommands.removePkg} sequant\n`,
86-
),
87-
);
81+
// Warn if running from a problematic install location.
82+
// The home-stray case ($HOME/node_modules/sequant) gets a distinct warning
83+
// because it pollutes resolution for every subdirectory of $HOME, which the
84+
// generic "local node_modules" message doesn't communicate.
85+
if (!process.argv.includes("--quiet")) {
86+
if (isHomeStrayInstall()) {
87+
const installRoot = getInstallRoot();
88+
console.warn(
89+
chalk.yellow(
90+
`! Sequant is running from ${installRoot} — this pollutes\n` +
91+
` resolution for every subdirectory of your home directory.\n\n` +
92+
` If accidental (usually is — one stray \`npm install sequant\` from ~ does it):\n` +
93+
` remove ${installRoot?.replace(/\/sequant$/, "") ?? "$HOME/node_modules"}\n` +
94+
` remove $HOME/package.json and $HOME/package-lock.json\n\n` +
95+
` If intentional: use \`npm install -g sequant\` or the Claude Code plugin.\n`,
96+
),
97+
);
98+
} else if (isLocalNodeModulesInstall()) {
99+
const pmCommands = getPackageManagerCommands(detectPackageManagerSync());
100+
console.warn(
101+
chalk.yellow(
102+
"! Running sequant from local node_modules\n" +
103+
" For latest version: npx sequant@latest\n" +
104+
` To remove local: ${pmCommands.removePkg} sequant\n`,
105+
),
106+
);
107+
}
88108
}
89109

90110
program

‎src/lib/version-check.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ vi.mock("fs", () => ({
1616
existsSync: mockExistsSync,
1717
}));
1818

19+
// Mock os.homedir() so isHomeStrayInstall can be tested deterministically.
20+
// process.env.HOME is set in beforeEach, but os.homedir() reads the OS-level
21+
// home on macOS regardless, so we mock the module to honor the env value.
22+
vi.mock("os", async () => {
23+
const actual = await vi.importActual<typeof import("os")>("os");
24+
return {
25+
...actual,
26+
default: {
27+
...actual,
28+
homedir: () => process.env.HOME || "/home/user",
29+
},
30+
homedir: () => process.env.HOME || "/home/user",
31+
};
32+
});
33+
1934
// Mock global fetch
2035
const mockFetch = vi.fn();
2136
vi.stubGlobal("fetch", mockFetch);
@@ -31,6 +46,7 @@ import {
3146
isCacheFresh,
3247
getCurrentVersion,
3348
isLocalNodeModulesInstall,
49+
isHomeStrayInstall,
3450
fetchLatestVersion,
3551
checkVersionThorough,
3652
checkVersionCached,
@@ -136,6 +152,37 @@ describe("version-check utilities", () => {
136152
});
137153
});
138154

155+
describe("isHomeStrayInstall", () => {
156+
// process.env.HOME is set to "/home/user" in beforeEach, so
157+
// os.homedir() resolves to "/home/user" inside the function.
158+
159+
it("returns true when install root equals $HOME/node_modules/sequant", () => {
160+
expect(isHomeStrayInstall("/home/user/node_modules/sequant")).toBe(true);
161+
});
162+
163+
it("returns false for project-local installs", () => {
164+
expect(
165+
isHomeStrayInstall("/home/user/projects/foo/node_modules/sequant"),
166+
).toBe(false);
167+
});
168+
169+
it("returns false for global installs", () => {
170+
expect(isHomeStrayInstall("/usr/local/lib/node_modules/sequant")).toBe(
171+
false,
172+
);
173+
});
174+
175+
it("returns false for npx cache installs", () => {
176+
expect(
177+
isHomeStrayInstall("/home/user/.npm/_npx/abc123/node_modules/sequant"),
178+
).toBe(false);
179+
});
180+
181+
it("returns false when install root cannot be resolved", () => {
182+
expect(isHomeStrayInstall(null)).toBe(false);
183+
});
184+
});
185+
139186
describe("getCacheDir", () => {
140187
it("returns ~/.cache/sequant path", () => {
141188
const cacheDir = getCacheDir();

‎src/lib/version-check.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import fs from "fs";
9+
import os from "os";
910
import path from "path";
1011
import { fileURLToPath } from "url";
1112
import {
@@ -99,6 +100,55 @@ export function isLocalNodeModulesInstall(): boolean {
99100
return inNodeModules && !inNpxCache;
100101
}
101102

103+
/**
104+
* Walk up from the given directory to find the directory containing
105+
* sequant's package.json. Returns null if not found.
106+
*
107+
* Mirrors the walk-up in getCurrentVersion() so callers can reason about
108+
* the install root (the directory that actually holds sequant's package.json),
109+
* which is needed to distinguish between e.g. project-local installs and the
110+
* pathological $HOME/node_modules/sequant case.
111+
*/
112+
export function getInstallRoot(startDir: string = __dirname): string | null {
113+
let dir = startDir;
114+
while (dir !== path.dirname(dir)) {
115+
const candidate = path.resolve(dir, "package.json");
116+
try {
117+
const content = fs.readFileSync(candidate, "utf8");
118+
const pkg = JSON.parse(content);
119+
if (pkg.name === "sequant") {
120+
return dir;
121+
}
122+
} catch {
123+
// Not found, continue searching
124+
}
125+
dir = path.dirname(dir);
126+
}
127+
return null;
128+
}
129+
130+
/**
131+
* Check whether sequant is installed in $HOME/node_modules/sequant exactly.
132+
*
133+
* This is a known footgun: a stray `npm install sequant` run from $HOME
134+
* leaves a package.json + node_modules/ at the home root, and Node's module
135+
* resolution then walks up from any home subdirectory to that stale install
136+
* before falling back to the npx cache. The result is that `npx sequant`
137+
* silently runs the home-stray copy instead of the latest version.
138+
*
139+
* Project-local installs (anywhere else under node_modules/), global installs,
140+
* and the npx cache all return false.
141+
*
142+
* `installRoot` is accepted as a parameter for testability.
143+
*/
144+
export function isHomeStrayInstall(
145+
installRoot: string | null = getInstallRoot(),
146+
): boolean {
147+
if (!installRoot) return false;
148+
const expected = path.join(os.homedir(), "node_modules", "sequant");
149+
return path.resolve(installRoot) === path.resolve(expected);
150+
}
151+
102152
/**
103153
* Read the version cache
104154
*/

0 commit comments

Comments
 (0)