From a2e7306b2fa1ca55a360d7d22b8dbb1ef2bc1f8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Mon, 16 Nov 2026 10:53:40 +0000 Subject: [PATCH] fix(cli): open starts the desktop app in the home folder, not the folder it runs in (#7937) * fix(cli): open starts the desktop app in the home folder, not the folder it runs in * style(cli): keep launchApp's note on one comment line * fix(cli): open starts the desktop app without the project's .env values --- packages/cli/src/utils/desktopApp.test.ts | 62 +++++++++++++++++++++++ packages/cli/src/utils/desktopApp.ts | 14 ++++- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/utils/desktopApp.test.ts b/packages/cli/src/utils/desktopApp.test.ts index 591af14488..6f8a3f4a40 100644 --- a/packages/cli/src/utils/desktopApp.test.ts +++ b/packages/cli/src/utils/desktopApp.test.ts @@ -1,9 +1,11 @@ import { chmodSync, existsSync, + mkdirSync, mkdtempSync, readdirSync, readFileSync, + realpathSync, rmSync, writeFileSync, } from "node:fs"; @@ -12,6 +14,7 @@ import { join, resolve, win32 } from "node:path"; import { isHyperframesProject } from "@hyperframes/core"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { readSeen } from "./appHistory.js"; +import { loadProjectDotEnv } from "./dotEnv.js"; import { AGENT_HANDOFF_FILE, agentSession, @@ -299,6 +302,65 @@ describe("openInDesktop on Linux", () => { }); }); + it.skipIf(process.platform === "win32")( + "starts the app in the home folder, not the folder the command runs in", + async () => { + const dir = mkdtempSync(join(tmpdir(), "open-from-home-")); + const realHome = join(dir, "home"); + mkdirSync(realHome); + const startedIn = join(dir, "started-in"); + const app = join(dir, "HyperFrames.AppImage"); + writeFileSync(app, `#!/bin/sh\npwd -P > ${JSON.stringify(startedIn)}\n`); + chmodSync(app, 0o755); + vi.stubEnv("HOME", realHome); + const files = { [launcher("HyperFrames")]: JSON.stringify({ appImage: app }) }; + try { + expect(openInDesktop(FILM, { ...LINUX, read: (p) => files[p] ?? null })).toMatchObject({ + opened: true, + }); + await vi.waitFor(() => + expect(readFileSync(startedIn, "utf8").trim()).toBe(realpathSync(realHome)), + ); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }, + ); + + it.skipIf(process.platform === "win32")( + "starts the app without the values the project's .env set, keeping the person's own", + async () => { + const dir = mkdtempSync(join(tmpdir(), "open-env-")); + const seen = join(dir, "seen"); + const app = join(dir, "HyperFrames.AppImage"); + writeFileSync( + app, + `#!/bin/sh\nprintf '%s|%s' "\${HEYGEN_API_KEY-unset}" "\${ELEVENLABS_API_KEY-unset}" > ${JSON.stringify(seen)}\n`, + ); + chmodSync(app, 0o755); + writeFileSync( + join(dir, ".env"), + "HEYGEN_API_KEY=from-project\nELEVENLABS_API_KEY=from-project\n", + ); + vi.stubEnv("ELEVENLABS_API_KEY", "the-person's"); + const before = process.env.HEYGEN_API_KEY; + delete process.env.HEYGEN_API_KEY; + loadProjectDotEnv(dir, process.env, () => {}); + const files = { [launcher("HyperFrames")]: JSON.stringify({ appImage: app }) }; + try { + expect(process.env.HEYGEN_API_KEY).toBe("from-project"); + expect(openInDesktop(FILM, { ...LINUX, read: (p) => files[p] ?? null })).toMatchObject({ + opened: true, + }); + await vi.waitFor(() => expect(readFileSync(seen, "utf8")).toBe("unset|the-person's")); + } finally { + if (before === undefined) delete process.env.HEYGEN_API_KEY; + else process.env.HEYGEN_API_KEY = before; + rmSync(dir, { recursive: true, force: true }); + } + }, + ); + it("ignores a relative, moved, or unreadable AppImage path", () => { const relativeFile = { [launcher("HyperFrames")]: JSON.stringify({ appImage: "HF.AppImage" }) }; const relativePath = { ...LINUX, read: (p: string) => relativeFile[p] ?? null, launch: never }; diff --git a/packages/cli/src/utils/desktopApp.ts b/packages/cli/src/utils/desktopApp.ts index 1fe92f797a..f15c81bda0 100644 --- a/packages/cli/src/utils/desktopApp.ts +++ b/packages/cli/src/utils/desktopApp.ts @@ -9,6 +9,7 @@ import { isCI } from "./attendedTerminal.js"; import { isAgentDriven } from "../telemetry/agent_runtime.js"; import { readRecord, removeRecord, writeRecord } from "./projectRecords.js"; import { shellArg } from "./shellArg.js"; +import { projectEnvDir } from "./dotEnv.js"; // The HyperFrames desktop app, then builds named in HYPERFRAMES_DESKTOP_BUNDLE_IDS / _APP_NAMES (comma-separated). const list = (name: string): string[] => @@ -78,10 +79,19 @@ export type DesktopOpenResult = const openWithBundle = (bundleId: string, dir: string): boolean => spawnSync("open", ["-b", bundleId, dir], { stdio: "ignore" }).status === 0; -/** Single-instance app: a running copy is forwarded the folder. Only a refused spawn counts as failed. */ +const withoutProjectEnv = (): NodeJS.ProcessEnv => + Object.fromEntries(Object.entries(process.env).filter(([name]) => !projectEnvDir(name))); + +/** Starts in home, not the caller's folder; a running copy gets the folder. Only a refused spawn counts as failed. */ function launchApp(executable: string, dir: string): boolean { try { - const child = spawn(executable, [dir], { detached: true, stdio: "ignore", windowsHide: false }); + const child = spawn(executable, [dir], { + cwd: homedir(), + env: withoutProjectEnv(), + detached: true, + stdio: "ignore", + windowsHide: false, + }); child.on("error", () => {}); child.unref(); return true;