Files
cindy/scripts/desktop-dev-runner.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

103 lines
3.5 KiB
JavaScript

#!/usr/bin/env node
/**
* Internal TTY-preserving runner for restart-desktop-remote.mjs.
* Electron writes the ready status; this runner records an early pnpm/Forge exit.
*/
import { spawn } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { resolvePnpmInvocation, usablePnpmExecPath } from './shared/pnpm-invocation.mjs';
const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const mode = process.argv[2];
if (mode !== 'remote' && mode !== 'local') {
console.error(`desktop-dev-runner: mode must be remote or local, received ${mode ?? '(empty)'}`);
process.exit(2);
}
const devScript = mode === 'local' ? 'dev:desktop' : 'dev:desktop:remote';
// The restart pipeline opens a fresh Windows cmd.exe. That environment does
// not always carry npm_execpath, and may carry a stale one, so the path is
// validated before use; resolvePnpmInvocation then decides how to execute it
// (JS entry via node, native binary directly, command wrapper through PATH),
// and falls back to PATH/PATHEXT resolution when nothing usable is left.
const invocation = resolvePnpmInvocation([devScript], {
npmExecPath: usablePnpmExecPath(process.env.npm_execpath, fs.existsSync),
});
const { command, args } = invocation;
const child = spawn(command, args, {
cwd: rootDir,
env: { ...process.env, COREPACK_ENABLE_AUTO_PIN: '0', ...(invocation.env ?? {}) },
stdio: 'inherit',
windowsHide: false,
shell: invocation.shell,
windowsVerbatimArguments: invocation.windowsVerbatimArguments,
});
child.once('error', (error) => {
writeFailedStatus({ exitCode: null, error: error.message });
console.error(`desktop-dev-runner: ${error.message}`);
process.exit(1);
});
child.once('exit', (code, signal) => {
const statusPath = process.env.XDT_DESKTOP_DEV_STARTUP_STATUS_FILE;
const existing = statusPath ? readStatus(statusPath) : null;
if (statusPath) {
if (existing?.state === 'ready' || existing?.state === 'abandoned' || !existing) {
fs.rmSync(statusPath, { force: true });
} else if (existing.state !== 'failed') {
writeFailedStatus({ exitCode: code, signal });
}
}
if (signal && process.platform !== 'win32') {
process.kill(process.pid, signal);
return;
}
process.exit(code ?? 1);
});
function readStatus(statusPath) {
try {
return JSON.parse(fs.readFileSync(statusPath, 'utf8'));
} catch {
return null;
}
}
function writeFailedStatus(detail) {
const statusPath = process.env.XDT_DESKTOP_DEV_STARTUP_STATUS_FILE;
const state = statusPath ? readStatus(statusPath)?.state : null;
if (!statusPath || state === 'ready' || state === 'abandoned') return;
const errorMessage = detail.error ? ` Desktop dev command failed: ${detail.error}` : '';
writeStatus(statusPath, {
state: 'failed',
code: 'DEV_PROCESS_EXITED',
message: `The desktop dev process exited before the main window became ready.${errorMessage}`,
detail: {
rootDir,
command,
devScript,
...detail,
},
...detail,
pid: process.pid,
at: Date.now(),
});
}
function writeStatus(statusPath, status) {
const tempPath = `${statusPath}.${process.pid}.tmp`;
try {
fs.writeFileSync(tempPath, `${JSON.stringify(status)}\n`, { mode: 0o600 });
fs.renameSync(tempPath, statusPath);
} catch (error) {
fs.rmSync(tempPath, { force: true });
console.error(`desktop-dev-runner: failed to write startup status: ${error.message}`);
}
}