mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-26 00:57:05 +00:00
fix(skills): bake-basemap asks npm where global packages live, and says where it looked (#8698)
The global folder was taken from where Node is installed, which is not where npm install --global puts packages with a custom npm prefix, Volta, or a distribution's packaged Node, so someone who followed the message could still be told puppeteer-core is missing. The loader now asks npm root --global, run in the home folder so no project's .npmrc is read, and keeps the old folder when npm cannot answer. The missing-package message lists the folders it searched, and the comment says the home lookup also reads the system folders above it.
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
"files": 4
|
||||
},
|
||||
"hyperframes": {
|
||||
"hash": "65b6e173ececb73f",
|
||||
"hash": "c7268ddd621ebbca",
|
||||
"files": 85
|
||||
},
|
||||
"hyperframes-cli": {
|
||||
|
||||
@@ -26,7 +26,7 @@ import { mkdirSync, writeFileSync, readdirSync, existsSync } from "node:fs";
|
||||
import { homedir } from "node:os";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { assertLiteralMediaPath } from "./lib/literal-media-input.mjs";
|
||||
import { importEntry, trustedPackageEntry } from "./lib/trusted-package.mjs";
|
||||
import { importEntry, searchedFolders, trustedPackageEntry } from "./lib/trusted-package.mjs";
|
||||
|
||||
const NEEDS_PUPPETEER = `It loads puppeteer-core only from your own install, never from the project folder:
|
||||
install it from your home folder (cd ~ && npm install --global puppeteer-core), or set
|
||||
@@ -54,7 +54,10 @@ const puppeteerEntry = trustedPackageEntry("puppeteer-core", {
|
||||
overrideName: "HYPERFRAMES_PUPPETEER_CORE",
|
||||
});
|
||||
if (!puppeteerEntry) {
|
||||
console.error(`bake-basemap needs puppeteer-core. ${NEEDS_PUPPETEER}`);
|
||||
const looked = searchedFolders().join(" and ");
|
||||
console.error(
|
||||
`bake-basemap needs puppeteer-core and found none in ${looked}. ${NEEDS_PUPPETEER}`,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
const puppeteer = await importEntry(puppeteerEntry);
|
||||
|
||||
@@ -6,11 +6,12 @@ import {
|
||||
mkdirSync,
|
||||
mkdtempSync,
|
||||
readFileSync,
|
||||
realpathSync,
|
||||
rmSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { delimiter, dirname, join } from "node:path";
|
||||
import { test } from "node:test";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { npmGlobalFolder } from "./lib/trusted-package.mjs";
|
||||
@@ -66,13 +67,14 @@ function setup(t, skillsDir) {
|
||||
|
||||
for (const skillsDir of [".agents/skills", ".claude/skills", "skills"]) {
|
||||
test(`a skill in <project>/${skillsDir} never loads the project's puppeteer-core`, (t) => {
|
||||
const { run, loaded } = setup(t, skillsDir);
|
||||
const { home, run, loaded } = setup(t, skillsDir);
|
||||
const result = run();
|
||||
assert.deepEqual(loaded(), []);
|
||||
if (globalCopy) return;
|
||||
assert.equal(result.status, 1, result.stderr);
|
||||
assert.match(result.stderr, /never from the project folder/);
|
||||
assert.match(result.stderr, /cd ~ && npm install --global puppeteer-core/);
|
||||
assert.ok(result.stderr.includes(join(home, "node_modules")), result.stderr);
|
||||
assert.doesNotMatch(result.stderr, /ERR_MODULE_NOT_FOUND/);
|
||||
});
|
||||
}
|
||||
@@ -94,3 +96,47 @@ test("HYPERFRAMES_PUPPETEER_CORE loads the folder it names, and must be absolute
|
||||
assert.match(relative.stderr, /must be an absolute path/);
|
||||
assert.deepEqual(loaded(), ["override"]);
|
||||
});
|
||||
|
||||
/** A stand-in npm on PATH that records the folder it runs in and answers `root --global` with `answer`. */
|
||||
function fakeNpm(base, answer, status = 0) {
|
||||
const bin = join(base, "bin");
|
||||
mkdirSync(bin, { recursive: true });
|
||||
const ranIn = join(base, "npm-ran-in.txt");
|
||||
writeFileSync(
|
||||
join(bin, "npm"),
|
||||
`#!/bin/sh\npwd > ${JSON.stringify(ranIn)}\necho ${JSON.stringify(answer)}\nexit ${status}\n`,
|
||||
{ mode: 0o755 },
|
||||
);
|
||||
return { PATH: `${bin}${delimiter}${process.env.PATH}`, ranIn };
|
||||
}
|
||||
|
||||
test(
|
||||
"the folder npm root --global names is read, with npm asked from the home folder",
|
||||
{ skip: process.platform === "win32" && "the stand-in npm is a shell script" },
|
||||
(t) => {
|
||||
const { base, home, marker, run, loaded } = setup(t, ".agents/skills");
|
||||
const globalFolder = join(base, "custom-prefix", "lib", "node_modules");
|
||||
stubPackage(globalFolder, marker, "npm global");
|
||||
const npm = fakeNpm(base, globalFolder);
|
||||
run({ PATH: npm.PATH });
|
||||
assert.deepEqual(loaded(), ["npm global"]);
|
||||
assert.equal(realpathSync(readFileSync(npm.ranIn, "utf8").trim()), realpathSync(home));
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
"when npm cannot answer, npm's default folder for this Node is read instead",
|
||||
{ skip: process.platform === "win32" && "the stand-in npm is a shell script" },
|
||||
(t) => {
|
||||
const base = mkdtempSync(join(tmpdir(), "basemap-npm-"));
|
||||
t.after(() => rmSync(base, { recursive: true, force: true }));
|
||||
const npm = fakeNpm(base, "", 1);
|
||||
const path = process.env.PATH;
|
||||
process.env.PATH = npm.PATH;
|
||||
t.after(() => (process.env.PATH = path));
|
||||
assert.equal(
|
||||
npmGlobalFolder({ home: base }),
|
||||
join(dirname(dirname(process.execPath)), "lib", "node_modules"),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -1,18 +1,49 @@
|
||||
// A skill installed inside a project sits below the project's node_modules, so a bare import from a skill script
|
||||
// would run whatever package the project ships under that name. These load a package only from a folder the person
|
||||
// controls: an absolute path they set, their home folder, or npm's global folder.
|
||||
// would run whatever package the project ships under that name. These load a package only from an absolute path the
|
||||
// person sets, a lookup starting in their home folder (which also reads the system folders above it), or the folder
|
||||
// `npm install --global` uses.
|
||||
import "./windows-exe-search.mjs";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { existsSync } from "node:fs";
|
||||
import { createRequire } from "node:module";
|
||||
import { homedir } from "node:os";
|
||||
import { dirname, isAbsolute, join } from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
|
||||
/** npm's default global package folder for the running Node. */
|
||||
export function npmGlobalFolder(env = process.env, platform = process.platform) {
|
||||
if (platform === "win32")
|
||||
// npm runs in the home folder, so no project's .npmrc can name the folder.
|
||||
function askNpm(home) {
|
||||
const npmCli = join(dirname(process.execPath), "node_modules", "npm", "bin", "npm-cli.js");
|
||||
const windows = process.platform === "win32";
|
||||
if (windows && !existsSync(npmCli)) return null;
|
||||
const [command, args] = windows
|
||||
? [process.execPath, [npmCli, "root", "--global"]]
|
||||
: ["npm", ["root", "--global"]];
|
||||
const run = spawnSync(command, args, {
|
||||
cwd: home,
|
||||
encoding: "utf8",
|
||||
timeout: 5_000,
|
||||
windowsHide: true,
|
||||
stdio: ["ignore", "pipe", "ignore"],
|
||||
});
|
||||
const folder = run.status === 0 ? run.stdout.trim() : "";
|
||||
return isAbsolute(folder) ? folder : null;
|
||||
}
|
||||
|
||||
/** The folder `npm install --global` uses, as npm reports it; npm's default for the running Node when npm cannot say. */
|
||||
export function npmGlobalFolder({ home = homedir(), env = process.env } = {}) {
|
||||
const asked = askNpm(home);
|
||||
if (asked) return asked;
|
||||
if (process.platform === "win32")
|
||||
return join(env.APPDATA || dirname(process.execPath), "npm", "node_modules");
|
||||
return join(dirname(dirname(process.execPath)), "lib", "node_modules");
|
||||
}
|
||||
|
||||
/** The folders a lookup without an override reads, for the message when none holds the package. */
|
||||
export const searchedFolders = ({ home = homedir(), globalFolder } = {}) => [
|
||||
join(home, "node_modules"),
|
||||
globalFolder ?? npmGlobalFolder({ home }),
|
||||
];
|
||||
|
||||
function resolveFrom(folder, name) {
|
||||
try {
|
||||
return createRequire(join(folder, "lookup.cjs")).resolve(name);
|
||||
@@ -27,7 +58,7 @@ function resolveFrom(folder, name) {
|
||||
*/
|
||||
export function trustedPackageEntry(
|
||||
name,
|
||||
{ override, overrideName, home = homedir(), globalFolder = npmGlobalFolder() } = {},
|
||||
{ override, overrideName, home = homedir(), globalFolder } = {},
|
||||
) {
|
||||
if (override) {
|
||||
if (!isAbsolute(override)) {
|
||||
@@ -38,7 +69,7 @@ export function trustedPackageEntry(
|
||||
throw new Error(`${overrideName} names ${override}, which holds no ${name} package`);
|
||||
return entry;
|
||||
}
|
||||
return resolveFrom(home, name) ?? resolveFrom(globalFolder, name);
|
||||
return resolveFrom(home, name) ?? resolveFrom(globalFolder ?? npmGlobalFolder({ home }), name);
|
||||
}
|
||||
|
||||
/** The module at a resolved entry, with its default export unwrapped. */
|
||||
|
||||
Reference in New Issue
Block a user