mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-24 07:01:49 +00:00
fix: restore bundled runtime dependency provisioning (#58782) (thanks @obviyus)
* fix: restore bundled runtime dependency provisioning * fix: ship npm runner in packed installs * fix: address bundled runtime staging review feedback * fix: include npm runner in docker build contexts * fix: restore bundled runtime dependency provisioning (#58782) (thanks @obviyus) * fix: allow caret specs through windows npm cmd (#58782) (thanks @obviyus)
This commit is contained in:
@@ -19,7 +19,7 @@ COPY ui/package.json ./ui/package.json
|
||||
COPY packages ./packages
|
||||
COPY extensions ./extensions
|
||||
COPY patches ./patches
|
||||
COPY scripts/postinstall-bundled-plugins.mjs ./scripts/postinstall-bundled-plugins.mjs
|
||||
COPY scripts/postinstall-bundled-plugins.mjs scripts/npm-runner.mjs ./scripts/
|
||||
RUN --mount=type=cache,id=openclaw-pnpm-store,target=/root/.local/share/pnpm/store,sharing=locked \
|
||||
corepack enable \
|
||||
&& pnpm install --frozen-lockfile
|
||||
|
||||
@@ -22,7 +22,7 @@ COPY --chown=appuser:appuser package.json pnpm-lock.yaml pnpm-workspace.yaml .np
|
||||
COPY --chown=appuser:appuser ui/package.json ./ui/package.json
|
||||
COPY --chown=appuser:appuser extensions ./extensions
|
||||
COPY --chown=appuser:appuser patches ./patches
|
||||
COPY --chown=appuser:appuser scripts/postinstall-bundled-plugins.mjs ./scripts/postinstall-bundled-plugins.mjs
|
||||
COPY --chown=appuser:appuser scripts/postinstall-bundled-plugins.mjs scripts/npm-runner.mjs ./scripts/
|
||||
|
||||
RUN --mount=type=cache,id=openclaw-pnpm-store,target=/home/appuser/.local/share/pnpm/store,sharing=locked \
|
||||
pnpm install --frozen-lockfile
|
||||
|
||||
111
scripts/npm-runner.mjs
Normal file
111
scripts/npm-runner.mjs
Normal file
@@ -0,0 +1,111 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
const WINDOWS_UNSAFE_CMD_CHARS_RE = /[&|<>%\r\n]/;
|
||||
|
||||
function resolvePathEnvKey(env) {
|
||||
return Object.keys(env).find((key) => key.toLowerCase() === "path") ?? "PATH";
|
||||
}
|
||||
|
||||
function escapeForCmdExe(arg) {
|
||||
if (WINDOWS_UNSAFE_CMD_CHARS_RE.test(arg)) {
|
||||
throw new Error(`unsafe Windows cmd.exe argument detected: ${JSON.stringify(arg)}`);
|
||||
}
|
||||
const escaped = arg.replace(/\^/g, "^^");
|
||||
if (!escaped.includes(" ") && !escaped.includes('"')) {
|
||||
return escaped;
|
||||
}
|
||||
return `"${escaped.replace(/"/g, '""')}"`;
|
||||
}
|
||||
|
||||
function buildCmdExeCommandLine(command, args) {
|
||||
return [escapeForCmdExe(command), ...args.map(escapeForCmdExe)].join(" ");
|
||||
}
|
||||
|
||||
function resolveToolchainNpmRunner(params) {
|
||||
const npmCliCandidates = [
|
||||
params.pathImpl.resolve(params.nodeDir, "../lib/node_modules/npm/bin/npm-cli.js"),
|
||||
params.pathImpl.resolve(params.nodeDir, "node_modules/npm/bin/npm-cli.js"),
|
||||
];
|
||||
const npmCliPath = npmCliCandidates.find((candidate) => params.existsSync(candidate));
|
||||
if (npmCliPath) {
|
||||
return {
|
||||
command:
|
||||
params.platform === "win32"
|
||||
? params.pathImpl.join(params.nodeDir, "node.exe")
|
||||
: params.pathImpl.join(params.nodeDir, "node"),
|
||||
args: [npmCliPath, ...params.npmArgs],
|
||||
shell: false,
|
||||
};
|
||||
}
|
||||
if (params.platform !== "win32") {
|
||||
return null;
|
||||
}
|
||||
const npmExePath = params.pathImpl.resolve(params.nodeDir, "npm.exe");
|
||||
if (params.existsSync(npmExePath)) {
|
||||
return {
|
||||
command: npmExePath,
|
||||
args: params.npmArgs,
|
||||
shell: false,
|
||||
};
|
||||
}
|
||||
const npmCmdPath = params.pathImpl.resolve(params.nodeDir, "npm.cmd");
|
||||
if (params.existsSync(npmCmdPath)) {
|
||||
return {
|
||||
command: params.comSpec,
|
||||
args: ["/d", "/s", "/c", buildCmdExeCommandLine(npmCmdPath, params.npmArgs)],
|
||||
shell: false,
|
||||
windowsVerbatimArguments: true,
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function resolveNpmRunner(params = {}) {
|
||||
const execPath = params.execPath ?? process.execPath;
|
||||
const npmArgs = params.npmArgs ?? [];
|
||||
const existsSync = params.existsSync ?? fs.existsSync;
|
||||
const env = params.env ?? process.env;
|
||||
const platform = params.platform ?? process.platform;
|
||||
const comSpec = params.comSpec ?? env.ComSpec ?? "cmd.exe";
|
||||
const pathImpl = platform === "win32" ? path.win32 : path.posix;
|
||||
const nodeDir = pathImpl.dirname(execPath);
|
||||
const npmToolchain = resolveToolchainNpmRunner({
|
||||
comSpec,
|
||||
existsSync,
|
||||
nodeDir,
|
||||
npmArgs,
|
||||
pathImpl,
|
||||
platform,
|
||||
});
|
||||
if (npmToolchain) {
|
||||
return npmToolchain;
|
||||
}
|
||||
if (platform === "win32") {
|
||||
const expectedPaths = [
|
||||
pathImpl.resolve(nodeDir, "../lib/node_modules/npm/bin/npm-cli.js"),
|
||||
pathImpl.resolve(nodeDir, "node_modules/npm/bin/npm-cli.js"),
|
||||
pathImpl.resolve(nodeDir, "npm.exe"),
|
||||
pathImpl.resolve(nodeDir, "npm.cmd"),
|
||||
];
|
||||
throw new Error(
|
||||
`failed to resolve a toolchain-local npm next to ${execPath}. ` +
|
||||
`Checked: ${expectedPaths.join(", ")}. ` +
|
||||
"OpenClaw refuses to shell out to bare npm on Windows; install a Node.js toolchain that bundles npm or run with a matching Node installation.",
|
||||
);
|
||||
}
|
||||
const pathKey = resolvePathEnvKey(env);
|
||||
const currentPath = env[pathKey];
|
||||
return {
|
||||
command: "npm",
|
||||
args: npmArgs,
|
||||
shell: false,
|
||||
env: {
|
||||
...env,
|
||||
[pathKey]:
|
||||
typeof currentPath === "string" && currentPath.length > 0
|
||||
? `${nodeDir}${path.delimiter}${currentPath}`
|
||||
: nodeDir,
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -4,10 +4,11 @@
|
||||
// so runtime dependencies declared in dist/extensions/*/package.json must also
|
||||
// resolve from the package root node_modules after a global install.
|
||||
// This script is a no-op outside of a global npm install context.
|
||||
import { execSync } from "node:child_process";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { resolveNpmRunner } from "./npm-runner.mjs";
|
||||
|
||||
export const BUNDLED_PLUGIN_INSTALL_TARGETS = [];
|
||||
|
||||
@@ -107,7 +108,7 @@ export function runBundledPluginPostinstall(params = {}) {
|
||||
}
|
||||
const extensionsDir = params.extensionsDir ?? DEFAULT_EXTENSIONS_DIR;
|
||||
const packageRoot = params.packageRoot ?? DEFAULT_PACKAGE_ROOT;
|
||||
const exec = params.execSync ?? execSync;
|
||||
const spawn = params.spawnSync ?? spawnSync;
|
||||
const pathExists = params.existsSync ?? existsSync;
|
||||
const log = params.log ?? console;
|
||||
const runtimeDeps =
|
||||
@@ -122,11 +123,29 @@ export function runBundledPluginPostinstall(params = {}) {
|
||||
}
|
||||
|
||||
try {
|
||||
exec(`npm install --omit=dev --no-save --package-lock=false ${missingSpecs.join(" ")}`, {
|
||||
const nestedEnv = createNestedNpmInstallEnv(env);
|
||||
const npmRunner =
|
||||
params.npmRunner ??
|
||||
resolveNpmRunner({
|
||||
env: nestedEnv,
|
||||
execPath: params.execPath,
|
||||
existsSync: pathExists,
|
||||
platform: params.platform,
|
||||
comSpec: params.comSpec,
|
||||
npmArgs: ["install", "--omit=dev", "--no-save", "--package-lock=false", ...missingSpecs],
|
||||
});
|
||||
const result = spawn(npmRunner.command, npmRunner.args, {
|
||||
cwd: packageRoot,
|
||||
env: createNestedNpmInstallEnv(env),
|
||||
encoding: "utf8",
|
||||
env: npmRunner.env ?? nestedEnv,
|
||||
stdio: "pipe",
|
||||
shell: npmRunner.shell,
|
||||
windowsVerbatimArguments: npmRunner.windowsVerbatimArguments,
|
||||
});
|
||||
if (result.status !== 0) {
|
||||
const output = [result.stderr, result.stdout].filter(Boolean).join("\n").trim();
|
||||
throw new Error(output || "npm install failed");
|
||||
}
|
||||
log.log(`[postinstall] installed bundled plugin deps: ${missingSpecs.join(", ")}`);
|
||||
} catch (e) {
|
||||
// Non-fatal: gateway will surface the missing dep via doctor.
|
||||
|
||||
@@ -23,6 +23,8 @@ const requiredPathGroups = [
|
||||
["dist/entry.js", "dist/entry.mjs"],
|
||||
...listPluginSdkDistArtifacts(),
|
||||
...listBundledPluginPackArtifacts(),
|
||||
"scripts/npm-runner.mjs",
|
||||
"scripts/postinstall-bundled-plugins.mjs",
|
||||
"dist/plugin-sdk/compat.js",
|
||||
"dist/plugin-sdk/root-alias.cjs",
|
||||
"dist/build-info.json",
|
||||
|
||||
@@ -4,8 +4,8 @@ import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
|
||||
const WINDOWS_UNSAFE_CMD_CHARS_RE = /[&|<>^%\r\n]/;
|
||||
import semverSatisfies from "semver/functions/satisfies.js";
|
||||
import { resolveNpmRunner } from "./npm-runner.mjs";
|
||||
|
||||
function readJson(filePath) {
|
||||
return JSON.parse(fs.readFileSync(filePath, "utf8"));
|
||||
@@ -42,6 +42,60 @@ function replaceDir(targetPath, sourcePath) {
|
||||
removePathIfExists(sourcePath);
|
||||
}
|
||||
|
||||
function dependencyNodeModulesPath(nodeModulesDir, depName) {
|
||||
return path.join(nodeModulesDir, ...depName.split("/"));
|
||||
}
|
||||
|
||||
function readInstalledDependencyVersion(nodeModulesDir, depName) {
|
||||
const packageJsonPath = path.join(
|
||||
dependencyNodeModulesPath(nodeModulesDir, depName),
|
||||
"package.json",
|
||||
);
|
||||
if (!fs.existsSync(packageJsonPath)) {
|
||||
return null;
|
||||
}
|
||||
const version = readJson(packageJsonPath).version;
|
||||
return typeof version === "string" ? version : null;
|
||||
}
|
||||
|
||||
function dependencyVersionSatisfied(spec, installedVersion) {
|
||||
return semverSatisfies(installedVersion, spec, { includePrerelease: false });
|
||||
}
|
||||
|
||||
function collectInstalledRuntimeClosure(rootNodeModulesDir, dependencySpecs) {
|
||||
const packageCache = new Map();
|
||||
const closure = new Set();
|
||||
const queue = Object.entries(dependencySpecs);
|
||||
|
||||
while (queue.length > 0) {
|
||||
const [depName, spec] = queue.shift();
|
||||
const installedVersion = readInstalledDependencyVersion(rootNodeModulesDir, depName);
|
||||
if (installedVersion === null || !dependencyVersionSatisfied(spec, installedVersion)) {
|
||||
return null;
|
||||
}
|
||||
if (closure.has(depName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const packageJsonPath = path.join(
|
||||
dependencyNodeModulesPath(rootNodeModulesDir, depName),
|
||||
"package.json",
|
||||
);
|
||||
const packageJson = packageCache.get(depName) ?? readJson(packageJsonPath);
|
||||
packageCache.set(depName, packageJson);
|
||||
closure.add(depName);
|
||||
|
||||
for (const [childName, childSpec] of Object.entries(packageJson.dependencies ?? {})) {
|
||||
queue.push([childName, childSpec]);
|
||||
}
|
||||
for (const [childName, childSpec] of Object.entries(packageJson.optionalDependencies ?? {})) {
|
||||
queue.push([childName, childSpec]);
|
||||
}
|
||||
}
|
||||
|
||||
return [...closure];
|
||||
}
|
||||
|
||||
function listBundledPluginRuntimeDirs(repoRoot) {
|
||||
const extensionsRoot = path.join(repoRoot, "dist", "extensions");
|
||||
if (!fs.existsSync(extensionsRoot)) {
|
||||
@@ -130,114 +184,59 @@ function readRuntimeDepsStamp(stampPath) {
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveNpmRunner(params = {}) {
|
||||
const execPath = params.execPath ?? process.execPath;
|
||||
const npmArgs = params.npmArgs ?? [];
|
||||
const existsSync = params.existsSync ?? fs.existsSync;
|
||||
const env = params.env ?? process.env;
|
||||
const platform = params.platform ?? process.platform;
|
||||
const comSpec = params.comSpec ?? env.ComSpec ?? "cmd.exe";
|
||||
const pathImpl = platform === "win32" ? path.win32 : path.posix;
|
||||
const nodeDir = pathImpl.dirname(execPath);
|
||||
const npmToolchain = resolveToolchainNpmRunner({
|
||||
comSpec,
|
||||
existsSync,
|
||||
nodeDir,
|
||||
npmArgs,
|
||||
pathImpl,
|
||||
platform,
|
||||
});
|
||||
if (npmToolchain) {
|
||||
return npmToolchain;
|
||||
}
|
||||
if (platform === "win32") {
|
||||
const expectedPaths = [
|
||||
pathImpl.resolve(nodeDir, "../lib/node_modules/npm/bin/npm-cli.js"),
|
||||
pathImpl.resolve(nodeDir, "node_modules/npm/bin/npm-cli.js"),
|
||||
pathImpl.resolve(nodeDir, "npm.exe"),
|
||||
pathImpl.resolve(nodeDir, "npm.cmd"),
|
||||
];
|
||||
throw new Error(
|
||||
`failed to resolve a toolchain-local npm next to ${execPath}. ` +
|
||||
`Checked: ${expectedPaths.join(", ")}. ` +
|
||||
"OpenClaw refuses to shell out to bare npm on Windows; install a Node.js toolchain that bundles npm or run with a matching Node installation.",
|
||||
);
|
||||
}
|
||||
const pathKey = resolvePathEnvKey(env);
|
||||
const currentPath = env[pathKey];
|
||||
return {
|
||||
command: "npm",
|
||||
args: npmArgs,
|
||||
shell: false,
|
||||
env: {
|
||||
...env,
|
||||
[pathKey]:
|
||||
typeof currentPath === "string" && currentPath.length > 0
|
||||
? `${nodeDir}${path.delimiter}${currentPath}`
|
||||
: nodeDir,
|
||||
},
|
||||
function stageInstalledRootRuntimeDeps(params) {
|
||||
const { fingerprint, packageJson, pluginDir, repoRoot } = params;
|
||||
const dependencySpecs = {
|
||||
...packageJson.dependencies,
|
||||
...packageJson.optionalDependencies,
|
||||
};
|
||||
}
|
||||
const rootNodeModulesDir = path.join(repoRoot, "node_modules");
|
||||
if (Object.keys(dependencySpecs).length === 0 || !fs.existsSync(rootNodeModulesDir)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
function resolveToolchainNpmRunner(params) {
|
||||
const npmCliCandidates = [
|
||||
params.pathImpl.resolve(params.nodeDir, "../lib/node_modules/npm/bin/npm-cli.js"),
|
||||
params.pathImpl.resolve(params.nodeDir, "node_modules/npm/bin/npm-cli.js"),
|
||||
];
|
||||
const npmCliPath = npmCliCandidates.find((candidate) => params.existsSync(candidate));
|
||||
if (npmCliPath) {
|
||||
return {
|
||||
command:
|
||||
params.platform === "win32"
|
||||
? params.pathImpl.join(params.nodeDir, "node.exe")
|
||||
: params.pathImpl.join(params.nodeDir, "node"),
|
||||
args: [npmCliPath, ...params.npmArgs],
|
||||
shell: false,
|
||||
};
|
||||
const dependencyNames = collectInstalledRuntimeClosure(rootNodeModulesDir, dependencySpecs);
|
||||
if (dependencyNames === null) {
|
||||
return false;
|
||||
}
|
||||
if (params.platform !== "win32") {
|
||||
return null;
|
||||
}
|
||||
const npmExePath = params.pathImpl.resolve(params.nodeDir, "npm.exe");
|
||||
if (params.existsSync(npmExePath)) {
|
||||
return {
|
||||
command: npmExePath,
|
||||
args: params.npmArgs,
|
||||
shell: false,
|
||||
};
|
||||
}
|
||||
const npmCmdPath = params.pathImpl.resolve(params.nodeDir, "npm.cmd");
|
||||
if (params.existsSync(npmCmdPath)) {
|
||||
return {
|
||||
command: params.comSpec,
|
||||
args: ["/d", "/s", "/c", buildCmdExeCommandLine(npmCmdPath, params.npmArgs)],
|
||||
shell: false,
|
||||
windowsVerbatimArguments: true,
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function resolvePathEnvKey(env) {
|
||||
return Object.keys(env).find((key) => key.toLowerCase() === "path") ?? "PATH";
|
||||
}
|
||||
const nodeModulesDir = path.join(pluginDir, "node_modules");
|
||||
const stampPath = resolveRuntimeDepsStampPath(pluginDir);
|
||||
const stagedNodeModulesDir = path.join(
|
||||
makeTempDir(
|
||||
os.tmpdir(),
|
||||
`openclaw-runtime-deps-${sanitizeTempPrefixSegment(path.basename(pluginDir))}-`,
|
||||
),
|
||||
"node_modules",
|
||||
);
|
||||
|
||||
function escapeForCmdExe(arg) {
|
||||
if (WINDOWS_UNSAFE_CMD_CHARS_RE.test(arg)) {
|
||||
throw new Error(`unsafe Windows cmd.exe argument detected: ${JSON.stringify(arg)}`);
|
||||
}
|
||||
if (!arg.includes(" ") && !arg.includes('"')) {
|
||||
return arg;
|
||||
}
|
||||
return `"${arg.replace(/"/g, '""')}"`;
|
||||
}
|
||||
try {
|
||||
for (const depName of dependencyNames) {
|
||||
const sourcePath = dependencyNodeModulesPath(rootNodeModulesDir, depName);
|
||||
const targetPath = dependencyNodeModulesPath(stagedNodeModulesDir, depName);
|
||||
fs.mkdirSync(path.dirname(targetPath), { recursive: true });
|
||||
fs.cpSync(sourcePath, targetPath, { recursive: true, force: true, dereference: true });
|
||||
}
|
||||
|
||||
function buildCmdExeCommandLine(command, args) {
|
||||
return [escapeForCmdExe(command), ...args.map(escapeForCmdExe)].join(" ");
|
||||
replaceDir(nodeModulesDir, stagedNodeModulesDir);
|
||||
writeJson(stampPath, {
|
||||
fingerprint,
|
||||
generatedAt: new Date().toISOString(),
|
||||
});
|
||||
return true;
|
||||
} finally {
|
||||
removePathIfExists(path.dirname(stagedNodeModulesDir));
|
||||
}
|
||||
}
|
||||
|
||||
function installPluginRuntimeDeps(params) {
|
||||
const { fingerprint, packageJson, pluginDir, pluginId } = params;
|
||||
const { fingerprint, packageJson, pluginDir, pluginId, repoRoot } = params;
|
||||
if (
|
||||
repoRoot &&
|
||||
stageInstalledRootRuntimeDeps({ fingerprint, packageJson, pluginDir, repoRoot })
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const nodeModulesDir = path.join(pluginDir, "node_modules");
|
||||
const stampPath = resolveRuntimeDepsStampPath(pluginDir);
|
||||
const tempInstallDir = makeTempDir(
|
||||
@@ -333,6 +332,7 @@ export function stageBundledPluginRuntimeDeps(params = {}) {
|
||||
packageJson,
|
||||
pluginDir,
|
||||
pluginId,
|
||||
repoRoot,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user