Skip to content

Commit 131336f

Browse files
fix(skills): bootstrap npm safely on Windows (#3705)
* fix(skills): bootstrap npm safely on Windows * test(skills): verify npm bootstrap on Windows Node 24
1 parent 63182f6 commit 131336f

6 files changed

Lines changed: 217 additions & 34 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,23 @@ jobs:
364364
printf ' * %s\n' "${SKILLS_TESTS[@]}"
365365
node --test "${SKILLS_TESTS[@]}"
366366
367+
test-skill-bootstrap-windows:
368+
name: "Test: skill bootstrap (Windows, Node 24)"
369+
needs: changes
370+
if: needs.changes.outputs.skills == 'true'
371+
runs-on: windows-latest
372+
timeout-minutes: 5
373+
steps:
374+
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
375+
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
376+
with:
377+
node-version: 24
378+
- name: Exercise npm-cli bootstrap command
379+
run: >-
380+
node --test
381+
skills/hyperframes-animation/scripts/package-loader.test.mjs
382+
skills/hyperframes-creative/scripts/package-loader.test.mjs
383+
367384
# Guards that skills-manifest.json (the published freshness fingerprint read
368385
# by `hyperframes skills check`) was regenerated when a skill changed. Runs
369386
# `gen:skills-manifest --check`, which compares per-skill content hashes; the

‎skills-manifest.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"files": 17
2323
},
2424
"hyperframes-animation": {
25-
"hash": "a5be218c0b0fe377",
25+
"hash": "4252b08a4905b5ba",
2626
"files": 121
2727
},
2828
"hyperframes-audio": {
@@ -38,7 +38,7 @@
3838
"files": 20
3939
},
4040
"hyperframes-creative": {
41-
"hash": "68d6b293b011b5e8",
41+
"hash": "f3b201ca29eae92a",
4242
"files": 78
4343
},
4444
"hyperframes-keyframes": {

‎skills/hyperframes-animation/scripts/package-loader.mjs‎

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { spawnSync } from "node:child_process";
1212
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
1313
import { createRequire } from "node:module";
1414
import { tmpdir } from "node:os";
15-
import { basename, delimiter, dirname, join, parse, resolve } from "node:path";
15+
import { basename, delimiter, dirname, join, parse, resolve, win32 as win32Path } from "node:path";
1616
import { createInterface } from "node:readline/promises";
1717
import { fileURLToPath, pathToFileURL } from "node:url";
1818

@@ -339,23 +339,54 @@ function ancestors(start) {
339339
return dirs;
340340
}
341341

342+
export function resolveNpmSpawnCommand(
343+
args,
344+
platform = process.platform,
345+
env = process.env,
346+
nodeExecPath = process.execPath,
347+
pathExists = existsSync,
348+
) {
349+
if (platform !== "win32") {
350+
return { cmd: "npm", args, opts: { stdio: "inherit" } };
351+
}
352+
353+
const bundledNpmCli = win32Path.join(
354+
win32Path.dirname(nodeExecPath),
355+
"node_modules",
356+
"npm",
357+
"bin",
358+
"npm-cli.js",
359+
);
360+
const npmCli = [env.npm_execpath, bundledNpmCli].find(
361+
(candidate) => candidate && pathExists(candidate),
362+
);
363+
if (!npmCli) return null;
364+
return {
365+
cmd: env.npm_node_execpath || nodeExecPath,
366+
args: [npmCli, ...args],
367+
opts: { stdio: "inherit", windowsHide: true },
368+
};
369+
}
370+
342371
function bootstrapWithNpmInstall(packageNames) {
343372
const installRoot = mkdtempSync(join(tmpdir(), "hyperframes-skill-deps-"));
344-
const installResult = spawnSync(
345-
process.platform === "win32" ? "npm.cmd" : "npm",
346-
[
347-
"install",
348-
"--silent",
349-
"--no-audit",
350-
"--no-fund",
351-
"--ignore-scripts",
352-
"--no-save",
353-
"--prefix",
354-
installRoot,
355-
...packageNames,
356-
],
357-
{ stdio: "inherit" },
358-
);
373+
const npmArgs = [
374+
"install",
375+
"--silent",
376+
"--no-audit",
377+
"--no-fund",
378+
"--ignore-scripts",
379+
"--no-save",
380+
"--prefix",
381+
installRoot,
382+
...packageNames,
383+
];
384+
const npmCommand = resolveNpmSpawnCommand(npmArgs);
385+
if (!npmCommand) {
386+
rmSync(installRoot, { recursive: true, force: true });
387+
throw new Error("Could not locate npm-cli.js for dependency bootstrap on Windows.");
388+
}
389+
const installResult = spawnSync(npmCommand.cmd, npmCommand.args, npmCommand.opts);
359390

360391
if (installResult.error) throw installResult.error;
361392
if (installResult.status !== 0) {

‎skills/hyperframes-animation/scripts/package-loader.test.mjs‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,62 @@ import { copyFileSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
55
import { dirname, join } from "node:path";
66
import { tmpdir } from "node:os";
77
import { fileURLToPath } from "node:url";
8+
import { resolveNpmSpawnCommand } from "./package-loader.mjs";
89

910
const HERE = dirname(fileURLToPath(import.meta.url));
1011
const ENV = "HYPERFRAMES_SKILL_PKG_VERSION";
1112

13+
test("resolveNpmSpawnCommand routes Windows npm through node and npm-cli.js", () => {
14+
const npmCli = "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js";
15+
const node = "C:\\Program Files\\nodejs\\node.exe";
16+
const resolved = resolveNpmSpawnCommand(
17+
["install", "@hyperframes/[email protected]", "value & calc"],
18+
"win32",
19+
{ npm_execpath: npmCli, npm_node_execpath: node },
20+
node,
21+
(path) => path === npmCli,
22+
);
23+
24+
assert.deepEqual(resolved, {
25+
cmd: node,
26+
args: [npmCli, "install", "@hyperframes/[email protected]", "value & calc"],
27+
opts: { stdio: "inherit", windowsHide: true },
28+
});
29+
assert.equal(resolved.opts.shell, undefined);
30+
});
31+
32+
test("resolveNpmSpawnCommand finds npm-cli.js beside node for direct Windows runs", () => {
33+
const node = "C:\\Program Files\\nodejs\\node.exe";
34+
const npmCli = "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js";
35+
const resolved = resolveNpmSpawnCommand(
36+
["install", "@hyperframes/[email protected]"],
37+
"win32",
38+
{},
39+
node,
40+
(path) => path === npmCli,
41+
);
42+
43+
assert.equal(resolved?.cmd, node);
44+
assert.deepEqual(resolved?.args, [npmCli, "install", "@hyperframes/[email protected]"]);
45+
});
46+
47+
test(
48+
"resolveNpmSpawnCommand launches the installed npm CLI on Windows",
49+
{ skip: process.platform !== "win32" },
50+
() => {
51+
const resolved = resolveNpmSpawnCommand(["--version"]);
52+
assert.ok(resolved);
53+
54+
const result = spawnSync(resolved.cmd, resolved.args, {
55+
encoding: "utf8",
56+
windowsHide: true,
57+
});
58+
59+
assert.equal(result.status, 0, result.stderr);
60+
assert.match(result.stdout.trim(), /^\d+\./);
61+
},
62+
);
63+
1264
// (a) env override wins — no ancestor lookup, exact version echoed back.
1365
test("hyperframesPackageSpec: env override wins", async () => {
1466
const prev = process.env[ENV];

‎skills/hyperframes-creative/scripts/package-loader.mjs‎

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { spawnSync } from "node:child_process";
1212
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
1313
import { createRequire } from "node:module";
1414
import { tmpdir } from "node:os";
15-
import { basename, delimiter, dirname, join, parse, resolve } from "node:path";
15+
import { basename, delimiter, dirname, join, parse, resolve, win32 as win32Path } from "node:path";
1616
import { createInterface } from "node:readline/promises";
1717
import { fileURLToPath, pathToFileURL } from "node:url";
1818

@@ -339,23 +339,54 @@ function ancestors(start) {
339339
return dirs;
340340
}
341341

342+
export function resolveNpmSpawnCommand(
343+
args,
344+
platform = process.platform,
345+
env = process.env,
346+
nodeExecPath = process.execPath,
347+
pathExists = existsSync,
348+
) {
349+
if (platform !== "win32") {
350+
return { cmd: "npm", args, opts: { stdio: "inherit" } };
351+
}
352+
353+
const bundledNpmCli = win32Path.join(
354+
win32Path.dirname(nodeExecPath),
355+
"node_modules",
356+
"npm",
357+
"bin",
358+
"npm-cli.js",
359+
);
360+
const npmCli = [env.npm_execpath, bundledNpmCli].find(
361+
(candidate) => candidate && pathExists(candidate),
362+
);
363+
if (!npmCli) return null;
364+
return {
365+
cmd: env.npm_node_execpath || nodeExecPath,
366+
args: [npmCli, ...args],
367+
opts: { stdio: "inherit", windowsHide: true },
368+
};
369+
}
370+
342371
function bootstrapWithNpmInstall(packageNames) {
343372
const installRoot = mkdtempSync(join(tmpdir(), "hyperframes-skill-deps-"));
344-
const installResult = spawnSync(
345-
process.platform === "win32" ? "npm.cmd" : "npm",
346-
[
347-
"install",
348-
"--silent",
349-
"--no-audit",
350-
"--no-fund",
351-
"--ignore-scripts",
352-
"--no-save",
353-
"--prefix",
354-
installRoot,
355-
...packageNames,
356-
],
357-
{ stdio: "inherit" },
358-
);
373+
const npmArgs = [
374+
"install",
375+
"--silent",
376+
"--no-audit",
377+
"--no-fund",
378+
"--ignore-scripts",
379+
"--no-save",
380+
"--prefix",
381+
installRoot,
382+
...packageNames,
383+
];
384+
const npmCommand = resolveNpmSpawnCommand(npmArgs);
385+
if (!npmCommand) {
386+
rmSync(installRoot, { recursive: true, force: true });
387+
throw new Error("Could not locate npm-cli.js for dependency bootstrap on Windows.");
388+
}
389+
const installResult = spawnSync(npmCommand.cmd, npmCommand.args, npmCommand.opts);
359390

360391
if (installResult.error) throw installResult.error;
361392
if (installResult.status !== 0) {

‎skills/hyperframes-creative/scripts/package-loader.test.mjs‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,62 @@ import { copyFileSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
55
import { dirname, join } from "node:path";
66
import { tmpdir } from "node:os";
77
import { fileURLToPath } from "node:url";
8+
import { resolveNpmSpawnCommand } from "./package-loader.mjs";
89

910
const HERE = dirname(fileURLToPath(import.meta.url));
1011
const ENV = "HYPERFRAMES_SKILL_PKG_VERSION";
1112

13+
test("resolveNpmSpawnCommand routes Windows npm through node and npm-cli.js", () => {
14+
const npmCli = "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js";
15+
const node = "C:\\Program Files\\nodejs\\node.exe";
16+
const resolved = resolveNpmSpawnCommand(
17+
["install", "@hyperframes/[email protected]", "value & calc"],
18+
"win32",
19+
{ npm_execpath: npmCli, npm_node_execpath: node },
20+
node,
21+
(path) => path === npmCli,
22+
);
23+
24+
assert.deepEqual(resolved, {
25+
cmd: node,
26+
args: [npmCli, "install", "@hyperframes/[email protected]", "value & calc"],
27+
opts: { stdio: "inherit", windowsHide: true },
28+
});
29+
assert.equal(resolved.opts.shell, undefined);
30+
});
31+
32+
test("resolveNpmSpawnCommand finds npm-cli.js beside node for direct Windows runs", () => {
33+
const node = "C:\\Program Files\\nodejs\\node.exe";
34+
const npmCli = "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js";
35+
const resolved = resolveNpmSpawnCommand(
36+
["install", "@hyperframes/[email protected]"],
37+
"win32",
38+
{},
39+
node,
40+
(path) => path === npmCli,
41+
);
42+
43+
assert.equal(resolved?.cmd, node);
44+
assert.deepEqual(resolved?.args, [npmCli, "install", "@hyperframes/[email protected]"]);
45+
});
46+
47+
test(
48+
"resolveNpmSpawnCommand launches the installed npm CLI on Windows",
49+
{ skip: process.platform !== "win32" },
50+
() => {
51+
const resolved = resolveNpmSpawnCommand(["--version"]);
52+
assert.ok(resolved);
53+
54+
const result = spawnSync(resolved.cmd, resolved.args, {
55+
encoding: "utf8",
56+
windowsHide: true,
57+
});
58+
59+
assert.equal(result.status, 0, result.stderr);
60+
assert.match(result.stdout.trim(), /^\d+\./);
61+
},
62+
);
63+
1264
// (a) env override wins — no ancestor lookup, exact version echoed back.
1365
test("hyperframesPackageSpec: env override wins", async () => {
1466
const prev = process.env[ENV];

0 commit comments

Comments
 (0)