Skip to content

Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.

Detect npx on Windows, and stop printing one path with both separators

anirudhkumar-nanonets9d29b8b419c0
diff --git a/src/cli-meta.ts b/src/cli-meta.ts
index 01767fc9..f9cad44c 100644
--- a/src/cli-meta.ts
+++ b/src/cli-meta.ts
@@ -8,6 +8,7 @@ import { existsSync, readFileSync } from "node:fs";
import { execFileSync, spawnSync } from "node:child_process";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
+import { toPosixPath } from "./util/paths.js";
const PKG_NAME = "@nanonets/graft";
@@ -31,9 +32,14 @@ export function readCurrentVersion(moduleUrl: string): string {
}
/** True when the running module lives under an npx cache dir (e.g.
- * `~/.npm/_npx/<hash>/node_modules/...`) rather than a regular global install. */
+ * `~/.npm/_npx/<hash>/node_modules/...`) rather than a regular global install.
+ *
+ * Normalized first: `fileURLToPath` returns the *platform* separator, so on
+ * Windows the cache path is `…\_npx\…` and a bare `includes("/_npx/")` is always
+ * false — `graft upgrade` would then run `npm install -g` on top of an npx run.
+ * Same hardcoded-`/` mistake as #33; `src/util/paths.ts` exists for exactly this. */
export function isRunningViaNpx(moduleUrl: string): boolean {
- return fileURLToPath(moduleUrl).includes("/_npx/");
+ return toPosixPath(fileURLToPath(moduleUrl)).includes("/_npx/");
}
export interface NpmViewResult {
diff --git a/src/cli-picker.ts b/src/cli-picker.ts
index 696904f7..22d4a037 100644
--- a/src/cli-picker.ts
+++ b/src/cli-picker.ts
@@ -48,7 +48,9 @@ export function describeWrites(
}
if (globals.length > 0) {
const where = tilde(commonDir(globals.map((w) => w.path)), home);
- parts.push(`+ ${globals.length} in ${where}/ (machine-wide)`);
+ // `sep`, not a literal `/`: every other path on this line is native, so a
+ // hardcoded slash produced `~\.codex/` — one path, both separators.
+ parts.push(`+ ${globals.length} in ${where}${sep} (machine-wide)`);
}
if (!writes.some((w) => w.kind === 'mcp')) parts.push('no MCP');
return parts.join(' · ');
diff --git a/test/cli-meta.test.ts b/test/cli-meta.test.ts
index 8b3f0801..47e1a514 100644
--- a/test/cli-meta.test.ts
+++ b/test/cli-meta.test.ts
@@ -2,7 +2,7 @@ import { test } from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { pathToFileURL } from 'node:url';
-import { resolve } from 'node:path';
+import { join, resolve, sep } from 'node:path';
import {
formatVersionReport,
formatUpgradeReport,
@@ -68,13 +68,22 @@ test('readCurrentVersion reads the real package.json version', () => {
});
// --- isRunningViaNpx: pure path heuristic ---
+//
+// Built with `join` rather than a `/`-separated literal so the path carries the
+// platform separator: `fileURLToPath` hands back `…\_npx\…` on Windows, where the
+// original `includes("/_npx/")` was always false and `graft upgrade` would have run
+// `npm install -g` on top of an npx invocation. On posix this is the identity case.
test('isRunningViaNpx detects an npx cache path', () => {
- const npxPath = pathToFileURL('/Users/x/.npm/_npx/abc123/node_modules/@nanonets/graft/dist/cli.js').href;
+ const npxPath = pathToFileURL(
+ join(sep, 'Users', 'x', '.npm', '_npx', 'abc123', 'node_modules', '@nanonets', 'graft', 'dist', 'cli.js'),
+ ).href;
assert.equal(isRunningViaNpx(npxPath), true);
});
test('isRunningViaNpx is false for a regular global install', () => {
- const globalPath = pathToFileURL('/usr/local/lib/node_modules/@nanonets/graft/dist/cli.js').href;
+ const globalPath = pathToFileURL(
+ join(sep, 'usr', 'local', 'lib', 'node_modules', '@nanonets', 'graft', 'dist', 'cli.js'),
+ ).href;
assert.equal(isRunningViaNpx(globalPath), false);
});
diff --git a/test/cli-picker.test.ts b/test/cli-picker.test.ts
index 041dcd9a..2ba40783 100644
--- a/test/cli-picker.test.ts
+++ b/test/cli-picker.test.ts
@@ -2,7 +2,7 @@ import { test } from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, mkdirSync } from 'node:fs';
import { tmpdir } from 'node:os';
-import { join } from 'node:path';
+import { join, sep } from 'node:path';
import { planInit } from '../src/hosts/plan.js';
import {
KEY_CTRL_C, KEY_DOWN, KEY_ESC, KEY_UP,
@@ -128,11 +128,17 @@ test('abort sets aborted and never done', () => {
assert.ok(!state.done);
});
+// These are *display* paths, which deliberately keep the platform separator — so
+// the fixtures are built with `join`/`sep` rather than `/`-separated literals.
+// `tilde` keys on `sep`, so a posix literal simply never matched on Windows.
+
test('tilde shortens home paths and leaves others alone', () => {
- assert.equal(tilde('/Users/me/.codex/config.toml', '/Users/me'), '~/.codex/config.toml');
- assert.equal(tilde('/repo/AGENTS.md', '/Users/me'), '/repo/AGENTS.md');
+ const home = join(sep, 'Users', 'me');
+ assert.equal(tilde(join(home, '.codex', 'config.toml'), home), `~${sep}${join('.codex', 'config.toml')}`);
+ assert.equal(tilde(join(sep, 'repo', 'AGENTS.md'), home), join(sep, 'repo', 'AGENTS.md'));
// A sibling directory that merely shares a prefix is not the home dir.
- assert.equal(tilde('/Users/mediocre/x', '/Users/me'), '/Users/mediocre/x');
+ const sibling = join(sep, 'Users', 'mediocre', 'x');
+ assert.equal(tilde(sibling, home), sibling);
});
test('describeWrites names out-of-repo writes as machine-wide', () => {
@@ -140,7 +146,12 @@ test('describeWrites names out-of-repo writes as machine-wide', () => {
const agents = planInit(repo, { home, ids: ['agents'] })[0];
const summary = describeWrites(agents.writes, repo, home);
assert.match(summary, /AGENTS\.md/);
- assert.match(summary, /\+ 3 in ~\/\.codex\/ \(machine-wide\)/);
+ // `includes` rather than a regex: the expectation contains `sep`, which needs
+ // escaping in a pattern on Windows and reads worse for it.
+ assert.ok(
+ summary.includes(`+ 3 in ~${sep}.codex${sep} (machine-wide)`),
+ `machine-wide summary should name ~${sep}.codex${sep}, got: ${summary}`,
+ );
});
test('describeWrites flags hosts with no MCP target', () => {
@@ -175,7 +186,7 @@ test('formatPlan separates repo writes from machine-wide ones', () => {
const repoAt = text.indexOf('would write — this repo:');
const globalAt = text.indexOf('affects ALL repos:');
assert.ok(repoAt >= 0 && globalAt > repoAt, 'repo section comes first');
- assert.match(text, /~\/\.codex\/hooks\.json/);
+ assert.ok(text.includes(`~${sep}${join('.codex', 'hooks.json')}`), `plan should name the codex hooks file:\n${text}`);
assert.match(text, /PostToolUse: Write\|Edit\|MultiEdit/);
assert.match(text, /nothing was written \(--dry-run\)/);
assert.match(text, /--no-global/);