Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): avoid startup crash resolving device id (minimal) (#654)
rsl-tap9d30261b6ab6
diff --git a/apps/desktop/src/main/__tests__/deviceId.test.ts b/apps/desktop/src/main/__tests__/deviceId.test.tsnew file mode 100644index 0000000000..10035320e5--- /dev/null+++ b/apps/desktop/src/main/__tests__/deviceId.test.ts@@ -0,0 +1,76 @@+/**+ * ensureSystemBinPathForMachineId 单测 —— 启动崩溃的根因修复。+ *+ * machineIdSync 在 macOS 跑 bare `ioreg`;Finder 启动的 GUI 进程 PATH 极简(无 /usr/sbin)+ * 会让它抛、进而在模块顶层崩溃。这里保证取指纹前 PATH 补齐了系统 bin 目录。+ *+ * 每个用例在所有平台都断言:darwin/linux 断言补齐,其它平台(Windows)断言 no-op,+ * 避免非目标平台上整套用例空跑通过。+ */+import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import path from 'node:path';++import { ensureSystemBinPathForMachineId } from '../deviceId.js';++const PATCHES_PATH = process.platform === 'darwin' || process.platform === 'linux';++describe('ensureSystemBinPathForMachineId', () => {+ const originalPath = process.env.PATH;++ beforeEach(() => {+ process.env.PATH = '/usr/bin:/bin';+ });++ afterEach(() => {+ if (originalPath === undefined) delete process.env.PATH;+ else process.env.PATH = originalPath;+ });++ it('darwin/linux 补齐 /usr/sbin:/sbin;其它平台为 no-op', () => {+ ensureSystemBinPathForMachineId();+ const dirs = (process.env.PATH ?? '').split(path.delimiter);+ if (PATCHES_PATH) {+ expect(dirs).toContain('/usr/sbin');+ expect(dirs).toContain('/sbin');+ } else {+ expect(process.env.PATH).toBe('/usr/bin:/bin'); // Windows: 不动 PATH+ }+ });++ it('幂等:两次调用结果一致', () => {+ ensureSystemBinPathForMachineId();+ const afterFirst = process.env.PATH;+ ensureSystemBinPathForMachineId();+ expect(process.env.PATH).toBe(afterFirst);+ });++ it('追加而非前置:不覆盖已有的用户 PATH 项', () => {+ process.env.PATH = '/opt/homebrew/bin:/usr/bin:/bin';+ ensureSystemBinPathForMachineId();+ const dirs = (process.env.PATH ?? '').split(path.delimiter);+ expect(dirs[0]).toBe('/opt/homebrew/bin'); // 原有优先项仍在最前+ if (PATCHES_PATH) expect(dirs).toContain('/usr/sbin');+ });++ it('保留原 PATH 的空段(Unix 下代表 CWD),不改语义', () => {+ // 中间的空段(::)在 Unix 下表示当前目录;补齐时不能把它过滤掉。+ process.env.PATH = '/usr/bin::/bin';+ ensureSystemBinPathForMachineId();+ if (PATCHES_PATH) {+ expect(process.env.PATH).toBe('/usr/bin::/bin:/usr/sbin:/sbin');+ } else {+ expect(process.env.PATH).toBe('/usr/bin::/bin');+ }+ });++ it('PATH 未设置(undefined)时:darwin/linux 只置系统目录,其它平台维持 undefined', () => {+ delete process.env.PATH;+ ensureSystemBinPathForMachineId();+ if (PATCHES_PATH) {+ // 空/未设 PATH 只置系统目录,不刻意保留空段 / CWD(见 deviceId.ts)。+ expect(process.env.PATH).toBe('/usr/sbin:/sbin');+ } else {+ expect(process.env.PATH).toBeUndefined();+ }+ });+});diff --git a/apps/desktop/src/main/deviceId.ts b/apps/desktop/src/main/deviceId.tsnew file mode 100644index 0000000000..b7d370d4c9--- /dev/null+++ b/apps/desktop/src/main/deviceId.ts@@ -0,0 +1,38 @@+/**+ * deviceId.ts+ * ---------------------------------------------------------------------------+ * `node-machine-id`'s `machineIdSync()` runs bare `ioreg` on macOS (and relies on+ * `/usr/sbin:/sbin` on Linux). An app launched from Finder/Dock inherits a minimal+ * PATH — often just `/usr/bin:/bin` — that omits those dirs, so the command isn't+ * found and `machineIdSync()` throws `ioreg: command not found`. Because the device+ * id is read at module top level, that throw took the whole main process down on+ * launch — the "打开即闪退" (launch → instant crash) reported on packaged 0.1.14.+ *+ * The fix is just to guarantee the system bin dirs are on PATH before any device-id+ * probe runs. Call `ensureSystemBinPathForMachineId()` once, early in startup,+ * before the module that reads the device id loads.+ */++import path from 'node:path';++/**+ * Ensure the system bin directories are on PATH so `node-machine-id`'s bare+ * `ioreg` / `hostnamectl` invocations resolve regardless of how the app was+ * launched. Idempotent; safe to call before `app` is ready. No-op on Windows+ * (registry-based, no PATH dependency).+ */+export function ensureSystemBinPathForMachineId(): void {+ if (process.platform !== 'darwin' && process.platform !== 'linux') return;+ const required = ['/usr/sbin', '/sbin'];+ const raw = process.env.PATH ?? '';+ // Membership check only — the Set may include empty segments; that's fine, we+ // only look up the required dirs.+ const present = new Set(raw.split(path.delimiter));+ const missing = required.filter((dir) => !present.has(dir));+ if (missing.length === 0) return;+ // Non-empty PATH: keep it verbatim (including any empty segments, which mean+ // CWD on Unix) and append the missing dirs at the end — no reordering, no+ // dropped segments. Empty/unset PATH: set just the system dirs; we intentionally+ // do not materialise an implicit "" / CWD entry into PATH.+ process.env.PATH = raw === '' ? missing.join(path.delimiter) : [raw, ...missing].join(path.delimiter);+}diff --git a/apps/desktop/src/main/index.ts b/apps/desktop/src/main/index.tsindex 1e144b5a49..aebaf33bc1 100644--- a/apps/desktop/src/main/index.ts+++ b/apps/desktop/src/main/index.ts@@ -9,6 +9,7 @@ import { CURRENT_CINDY_REGION } from '../shared/brandRegion.js';import { resolveRegionUserDataDirName } from './regionUserData.js';import { createLogger, initLogger } from './logger.js';import { beginDesktopDevInstance, type DesktopDevMode } from './devStartupStatus.js';+import { ensureSystemBinPathForMachineId } from './deviceId.js';// 同机双装(cn/global):global 构建把 userData 切到区域目录(CindyGlobal),// 与 cn 版(productName 默认 'Cindy')彻底分库;数据库 / 登录态 / 单实例锁 /@@ -37,6 +38,13 @@ log.debug(`[fix-path] before PATH=${process.env.PATH ?? ''}`);fixPath();log.debug(`[fix-path] after PATH=${process.env.PATH ?? ''}`);+// Guarantee /usr/sbin:/sbin are on PATH before anything resolves the device id.+// A Finder/Dock-launched GUI process inherits a minimal PATH without them, so+// node-machine-id's bare `ioreg` isn't found and machineIdSync() throws — which,+// read at authManager's module top, crashed the whole app on launch (0.1.14).+// Must run before bootstrap-electron (and thus authManager) is imported below.+ensureSystemBinPathForMachineId();+// !! 必须在 dispatch() 之前同步执行 !!// 把用户系统(HKCU / shell rc)注入到本进程 process.env 上的 Anthropic / Claude Code// 体系 env 全部清掉,避免泄漏到我们 spawn 的 CC CLI 子进程。