openclaw/node-runtime-env.mjs
Peter Steinberger 2cf3343410
fix: recover Node from quoted Windows PATH entries (#181678)
On Windows, PATH entries wrapped in double quotes (for example "C:\Program Files\nodejs") were rejected before Node could be probed, so startup/update Node runtime recovery and Node discovery under Bun failed. One shared parser now unwraps entries quoted at both ends (embedded quotes stay literal) for recovery and discovery, while relative-path rejection and POSIX splitting are unchanged. Supersedes #181652, thanks @Links2008.

Co-authored-by: Links <222737184+Links2008@users.noreply.github.com>
2026-10-23 08:32:55 -07:00

27 lines
1 KiB
JavaScript

import path from "node:path";
export function nodeRuntimePathEntries(value) {
const windows = process.platform === "win32";
return (value ?? "")
.split(windows ? ";" : ":")
.map((entry) => (windows ? entry.replace(/^"(.*)"$/, "$1") : entry))
.filter(Boolean);
}
/** Keep lifecycle children on the explicitly selected runtime without changing npm config. */
export function withNodeRuntimePath(env, nodePath, platform = process.platform) {
const paths = platform === "win32" ? path.win32 : path.posix;
const keys = Object.keys(env)
.toSorted()
.filter((key) => (platform === "win32" ? key.toUpperCase() === "PATH" : key === "PATH"));
const key = keys[0] ?? "PATH";
const directory = paths.dirname(nodePath);
const entries = (env[key] ? env[key].split(paths.delimiter) : []).filter(
(entry) => entry !== directory,
);
const result = { ...env };
for (const pathKey of keys.length ? keys : [key]) {
result[pathKey] = [directory, ...entries].join(paths.delimiter);
}
return result;
}