Files
cindy/scripts/shared/pnpm-invocation.mjs
Kafeifei ba8258beb2 fix(scripts): pnpm 原生二进制发行版下开发脚本不再把 pnpm 交给 node 执行 (#804)
* fix(scripts): pnpm 原生二进制发行版下 test-workspaces 不再把 pnpm 交给 node 执行

resolvePnpmInvocation 原先假定 npm_execpath 一定指向 pnpm 的 JS 入口(pnpm.cjs),
构造 `node ${npm_execpath}` 调用。pnpm 以原生二进制发行版安装时(macOS 上
~/Library/pnpm/.tools/@pnpm+macos-arm64/<ver>/.../pnpm 是 Mach-O 可执行文件),
node 会把二进制当脚本解析并抛 SyntaxError: Invalid or unexpected token,
于是 `pnpm test:unit` 下每个 workspace 都被误判成 TEST_ASSERTION_FAILED 秒挂;
直接 `node scripts/test-workspaces.mjs` 不经 pnpm run 包裹反而正常。

改为按扩展名判定:只有 .js/.cjs/.mjs 才交给 node;否则直接把 npm_execpath
当可执行文件调用。Windows 上仅 .exe/.com 直接调用,.cmd/.bat 退回 PATH 解析
(shell:true 无法安全传递带空格的路径),与无 npm_execpath 时的兜底同路。

Signed-off-by: Kafeifei <kafeifei@gmail.com>

* fix(scripts): pnpm 原生二进制发行版下 Desktop dev 启动与依赖修复不再交给 node 执行

上一个 commit 只修了 test-workspaces;同样的假设还在启动链路上,于是
`pnpm restart:desktop:remote` 一直以 DEV_PROCESS_EXITED 失败——包装脚本新开的
Terminal 里带着 npm_execpath,desktop-dev-runner 便执行 `node <原生 pnpm 二进制>`,
SyntaxError 后 exit 1,主窗口永远等不到 ready。ensure-deps 自动修复依赖那一步
(resolvePnpmInstallInvocation)有同样的缺陷,只在需要重装时才暴露。

把「该怎么调用 pnpm」收敛到 scripts/shared/pnpm-invocation.mjs:
- resolvePnpmInvocation 按扩展名分派,只有 .js/.cjs/.mjs 交给 node,原生二进制
  直接执行,Windows 的 .cmd/.bat 退回 PATH 解析并 shell:true;
- usablePnpmExecPath 保留原有守卫(basename 匹配 pnpm + 文件存在),处理
  Windows restart 管线新开 cmd.exe 时 npm_execpath 缺失或残留的情况。

test-workspaces、desktop-worker-benchmark、desktop-dev-runner、ensure-deps 四处
统一走该模块;ensure-deps 的 spawnSync 补上 invocation.shell(此前 Windows 走
PATH 兜底时缺 shell,spawnSync 解析不到 pnpm)。

macOS 已实测:restart:desktop:remote 报 Desktop dev is ready,desktop:whoami 确认
实例来自本 worktree。Windows 分支按 spawn 语义推导,未实机验证。

Signed-off-by: Kafeifei <kafeifei@gmail.com>

* fix(scripts): respect explicit pnpm execpath fallback

Signed-off-by: Kafeifei <kafeifei@gmail.com>

* fix(scripts): preserve Windows pnpm cmd arguments

Signed-off-by: Kafeifei <kafeifei@gmail.com>

* fix(scripts): disable cmd delayed expansion for pnpm

Signed-off-by: Kafeifei <kafeifei@gmail.com>

* docs(scripts): clarify Windows pnpm wrapper invocation

Signed-off-by: Kafeifei <kafeifei@gmail.com>

* fix(scripts): preserve explicit pnpm cmd wrapper paths

Signed-off-by: Kafeifei <kafeifei@gmail.com>

* fix(scripts): quote Windows pnpm cmd command lines

Signed-off-by: Kafeifei <kafeifei@gmail.com>

* fix(scripts): shield pnpm cmd args from expansion

Signed-off-by: Kafeifei <kafeifei@gmail.com>

---------

Signed-off-by: Kafeifei <kafeifei@gmail.com>
2026-08-01 10:38:34 +08:00

86 lines
3.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import path from "node:path";
/**
* 解析「该怎么调用 pnpm」。
*
* `npm_execpath` 只保证是一个路径,不保证是脚本:corepack/npm 安装的 pnpm 指向 JS 入口
* (`pnpm.cjs`),而原生二进制发行版(`get.pnpm.io` 的 standalone 安装,以及 pnpm 自管
* `packageManager` 版本时下载的 `@pnpm/<platform>` 包)指向可执行文件本身。把后者交给
* `node` 会让它按 JS 解析 Mach-O/PE,抛 `SyntaxError: Invalid or unexpected token`——
* pnpm 根本没启动,调用方看到的却是命令的失败输出,极易误判成业务错误。
*/
const JS_ENTRY_EXTENSIONS = new Set([".js", ".cjs", ".mjs"]);
// Windows 上只有这两类扩展能被 spawn 直接拉起;.cmd/.bat 这类命令包装
// 通过 cmd.exe 解析 PATH/PATHEXT,避免 shell:true 拼接参数数组。
const WINDOWS_DIRECT_EXEC_EXTENSIONS = new Set([".exe", ".com"]);
const WINDOWS_CMD_ARG_ENV_PREFIX = "CINDY_PNPM_CMD_ARG_";
/**
* @param {string[]} args 传给 pnpm 的参数
* @param {{ npmExecPath?: string, npm_execpath?: string, execPath?: string, platform?: NodeJS.Platform | string, comSpec?: string }} options
* 允许注入 npmExecPath/execPath/platform 覆写,便于测试;生产路径默认从 Node runtime 取 platform。
* @returns {{ command: string, args: string[], shell: boolean, windowsVerbatimArguments?: boolean, env?: Record<string, string> }}
*/
export function resolvePnpmInvocation(args, options = {}) {
const hasNpmExecPathOption =
Object.prototype.hasOwnProperty.call(options, "npmExecPath") ||
Object.prototype.hasOwnProperty.call(options, "npm_execpath");
const npmExecPath = hasNpmExecPathOption
? (options.npmExecPath ?? options.npm_execpath)
: process.env.npm_execpath;
const execPath = options.execPath ?? process.execPath;
const platform = options.platform ?? process.platform;
const isWindows = platform === "win32";
if (npmExecPath) {
const extension = path.extname(npmExecPath).toLowerCase();
if (JS_ENTRY_EXTENSIONS.has(extension))
return { command: execPath, args: [npmExecPath, ...args], shell: false };
if (!isWindows || WINDOWS_DIRECT_EXEC_EXTENSIONS.has(extension))
return { command: npmExecPath, args, shell: false };
return resolveWindowsPnpmThroughCmd(npmExecPath, args, options.comSpec);
}
if (isWindows) return resolveWindowsPnpmThroughCmd("pnpm", args, options.comSpec);
return { command: "pnpm", args, shell: isWindows };
}
function resolveWindowsPnpmThroughCmd(pnpmCommand, args, comSpec = process.env.ComSpec) {
const env = {};
const commandLine = [pnpmCommand, ...args]
.map((arg, index) => {
const name = `${WINDOWS_CMD_ARG_ENV_PREFIX}${index}`;
env[name] = escapeWindowsCmdEnvArg(arg);
return quoteWindowsCmdExpansion(name);
})
.join(" ");
return {
command: comSpec || "cmd.exe",
args: ["/d", "/s", "/v:off", "/c", `"${commandLine}"`],
env,
shell: false,
windowsVerbatimArguments: true,
};
}
function quoteWindowsCmdExpansion(name) {
return `"%${name}%"`;
}
function escapeWindowsCmdEnvArg(arg) {
return String(arg).replace(/"/g, '""');
}
/**
* 判断 `npm_execpath` 是否可用:它可能残留自另一个环境,或指向已删除的文件
* (Windows 的 restart 管线新开 cmd.exe 时就见过)。不可用时返回 undefined,
* 交给 {@link resolvePnpmInvocation} 退回 PATH 解析。
*
* @param {string | undefined} npmExecPath
* @param {(target: string) => boolean} exists
* @returns {string | undefined}
*/
export function usablePnpmExecPath(npmExecPath, exists) {
if (!npmExecPath) return undefined;
if (!/pnpm/i.test(path.basename(npmExecPath))) return undefined;
return exists(npmExecPath) ? npmExecPath : undefined;
}