Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
MUL-8802 fix(desktop): strip the dev launcher's pnpm/turbo/electron-vite env from the daemon (#10160)
Jiayuan Zhangb586dd96cc27
diff --git a/apps/desktop/src/main/daemon-spawn-env.test.ts b/apps/desktop/src/main/daemon-spawn-env.test.tsindex 1216529d06..ae1794db35 100644--- a/apps/desktop/src/main/daemon-spawn-env.test.ts+++ b/apps/desktop/src/main/daemon-spawn-env.test.ts@@ -1,40 +1,82 @@// @vitest-environment node+import { delimiter, join, resolve } from "path";import { describe, expect, it } from "vitest";import { daemonSpawnEnv } from "./daemon-spawn-env";-describe("daemonSpawnEnv", () => {- it("drops electron-vite's NODE_ENV and its marker under a dev Desktop", () => {- const env = daemonSpawnEnv({- PATH: "/usr/bin",- NODE_ENV: "development",- NODE_ENV_ELECTRON_VITE: "development",- });+const repo = resolve("/repo");+const desktop = join(repo, "apps", "desktop");+const userPath = [resolve("/opt/homebrew/bin"), resolve("/usr/bin")];+const scriptBins = [+ join(desktop, "node_modules", ".bin"),+ join(repo, "node_modules", ".bin"),+ resolve("/pnpm/dist/node-gyp-bin"),+];- expect(env).toEqual({ PATH: "/usr/bin", MULTICA_LAUNCHED_BY: "desktop" });- });+// What `pnpm dev:desktop` → turbo → `electron-vite dev` adds, as observed in a+// dev Desktop's daemon.+const LAUNCHER_ENV = {+ NODE: "/opt/homebrew/bin/node",+ NODE_PATH: "/repo/node_modules/.pnpm/[email protected]/node_modules",+ INIT_CWD: desktop,+ PNPM_SCRIPT_SRC_DIR: desktop,+ npm_command: "run-script",+ npm_config_catalog: '{"react":"19.2.0"}',+ npm_config_frozen_lockfile: "",+ npm_config_registry: "https://registry.npmjs.org/",+ npm_config_shamefully_hoist: "true",+ npm_config_verify_deps_before_run: "false",+ npm_execpath: "/pnpm/bin/pnpm.cjs",+ npm_lifecycle_event: "dev",+ npm_package_name: "@multica/desktop",+ pnpm_config_verify_deps_before_run: "false",+ TURBO_HASH: "1cb7045295f9eb44",+ TURBO_INVOCATION_DIR: repo,+ NODE_ENV: "development",+ NODE_ENV_ELECTRON_VITE: "development",+ ELECTRON_CLI_ARGS: "[]",+ ELECTRON_EXEC_PATH: "/repo/node_modules/electron/dist/Electron",+ ELECTRON_MAJOR_VER: "44",+ ELECTRON_RENDERER_URL: "http://localhost:5173",+};- it("keeps a NODE_ENV that electron-vite did not set", () => {- const env = daemonSpawnEnv({ PATH: "/usr/bin", NODE_ENV: "test" });+// Same prefixes as the launcher env, but set by the user.+const USER_ENV = {+ HOME: "/Users/me",+ PNPM_HOME: "/Users/me/Library/pnpm",+ TURBO_TOKEN: "secret",+ ELECTRON_MIRROR: "https://mirror.example/electron/",+ NODE_OPTIONS: "--max-old-space-size=8192",+};++// fix-path's login-shell PATH: the user's entries, then what pnpm added.+const DEV_PATH = [...userPath, ...scriptBins, resolve("/usr/local/bin")].join(delimiter);++describe("daemonSpawnEnv", () => {+ it("drops the launcher env under a dev Desktop and keeps the user's", () => {+ const env = daemonSpawnEnv({ ...USER_ENV, ...LAUNCHER_ENV, PATH: DEV_PATH });expect(env).toEqual({- PATH: "/usr/bin",- NODE_ENV: "test",+ ...USER_ENV,+ PATH: [...userPath, resolve("/usr/local/bin")].join(delimiter),MULTICA_LAUNCHED_BY: "desktop",});});+ it("keeps the same variables when electron-vite did not launch the Desktop", () => {+ const { NODE_ENV_ELECTRON_VITE: _marker, ...rest } = LAUNCHER_ENV;+ const source = { ...USER_ENV, ...rest, NODE_ENV: "test", PATH: DEV_PATH };++ const env = daemonSpawnEnv(source);++ expect(env).toEqual({ ...source, MULTICA_LAUNCHED_BY: "desktop" });+ });+it("does not mutate the source env", () => {- const source = {- NODE_ENV: "development",- NODE_ENV_ELECTRON_VITE: "development",- };+ const source = { ...LAUNCHER_ENV, PATH: DEV_PATH };daemonSpawnEnv(source);- expect(source).toEqual({- NODE_ENV: "development",- NODE_ENV_ELECTRON_VITE: "development",- });+ expect(source).toEqual({ ...LAUNCHER_ENV, PATH: DEV_PATH });});});diff --git a/apps/desktop/src/main/daemon-spawn-env.ts b/apps/desktop/src/main/daemon-spawn-env.tsindex 9a995776d9..4340f45f33 100644--- a/apps/desktop/src/main/daemon-spawn-env.ts+++ b/apps/desktop/src/main/daemon-spawn-env.ts@@ -1,19 +1,78 @@+import { basename, delimiter, dirname, join } from "path";++// Lowercase `npm_*` / `pnpm_*` is what pnpm writes for a script run: its whole+// merged config (`npm_config_shamefully_hoist`, `npm_config_catalog`, ...) plus+// the running script's lifecycle state. Matched case-sensitively so uppercase+// user settings such as PNPM_HOME are kept.+const LAUNCHER_ENV_PREFIXES = ["npm_", "pnpm_"];++// The rest of the launcher env, named exactly because their prefixes also cover+// variables users set themselves (TURBO_TOKEN, ELECTRON_MIRROR, NODE_OPTIONS).+const LAUNCHER_ENV_NAMES = new Set([+ // pnpm's lifecycle and the bin shim that runs electron-vite.+ "NODE",+ "NODE_PATH",+ "INIT_CWD",+ "PNPM_SCRIPT_SRC_DIR",+ // turbo, per task.+ "TURBO_HASH",+ "TURBO_INVOCATION_DIR",+ "TURBO_IS_TUI",+ // electron-vite dev.+ "NODE_ENV",+ "NODE_ENV_ELECTRON_VITE",+ "ELECTRON_CLI_ARGS",+ "ELECTRON_ENTRY",+ "ELECTRON_EXEC_PATH",+ "ELECTRON_MAJOR_VER",+ "ELECTRON_RENDERER_URL",+]);+/*** Env for every CLI child the Desktop app spawns. The daemon passes its own env* on to each agent task, so whatever lands here is what agents see.*- * `electron-vite dev` overwrites NODE_ENV with "development" and sets- * NODE_ENV_ELECTRON_VITE before launching Electron. Agents building a Next.js- * or Vite app under that NODE_ENV fail at prerender, so both are dropped when- * NODE_ENV_ELECTRON_VITE marks the value as electron-vite's. A packaged app is- * not launched by electron-vite, so a NODE_ENV the user exported reaches the- * daemon untouched.+ * A dev Desktop is launched by `pnpm dev:desktop` → turbo → `electron-vite+ * dev`, and each layer adds env describing that launch. Left in, pnpm in any+ * repo an agent works on reads the Multica workspace's config (hoisted+ * node_modules, its catalog), NODE_PATH and PATH resolve modules and bins from+ * the dev checkout, and NODE_ENV=development breaks Next.js and Vite+ * production builds. NODE_ENV_ELECTRON_VITE marks that launch, so the launcher+ * env is dropped only when it is present. A packaged app is not launched this+ * way, so its env reaches the daemon untouched.*/export function daemonSpawnEnv(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {const spawnEnv: NodeJS.ProcessEnv = { ...env, MULTICA_LAUNCHED_BY: "desktop" };- if (spawnEnv.NODE_ENV_ELECTRON_VITE !== undefined) {- delete spawnEnv.NODE_ENV;- delete spawnEnv.NODE_ENV_ELECTRON_VITE;+ if (spawnEnv.NODE_ENV_ELECTRON_VITE === undefined) return spawnEnv;++ const scriptDir = spawnEnv.PNPM_SCRIPT_SRC_DIR;+ for (const name of Object.keys(spawnEnv)) {+ if (+ LAUNCHER_ENV_NAMES.has(name) ||+ LAUNCHER_ENV_PREFIXES.some((prefix) => name.startsWith(prefix))+ ) {+ delete spawnEnv[name];+ }+ }++ const pathKey = Object.keys(spawnEnv).find((k) => k.toUpperCase() === "PATH");+ if (pathKey && scriptDir) {+ spawnEnv[pathKey] = withoutScriptBins(spawnEnv[pathKey] ?? "", scriptDir);}return spawnEnv;}++// pnpm puts `node_modules/.bin` of the script's package and each ancestor, and+// its own node-gyp shim, on PATH for the script. The login-shell PATH that+// fix-path recovers keeps them, after the user's own entries.+function withoutScriptBins(path: string, scriptDir: string): string {+ const scriptBins = new Set<string>();+ for (let dir = scriptDir; ; dir = dirname(dir)) {+ scriptBins.add(join(dir, "node_modules", ".bin"));+ if (dirname(dir) === dir) break;+ }+ return path+ .split(delimiter)+ .filter((entry) => !scriptBins.has(entry) && basename(entry) !== "node-gyp-bin")+ .join(delimiter);+}diff --git a/apps/desktop/src/main/runtime-config-loader.test.ts b/apps/desktop/src/main/runtime-config-loader.test.tsindex 2bd6840cc5..c391358dfb 100644--- a/apps/desktop/src/main/runtime-config-loader.test.ts+++ b/apps/desktop/src/main/runtime-config-loader.test.ts@@ -2,7 +2,12 @@import { mkdtemp, writeFile } from "fs/promises";import { join } from "path";import { tmpdir } from "os";-import { describe, expect, it } from "vitest";+import { describe, expect, it, vi } from "vitest";++vi.mock("electron", () => ({+ app: { getPath: () => tmpdir() },+}));+import { loadRuntimeConfig } from "./runtime-config-loader";describe("loadRuntimeConfig", () => {