diff --git a/packages/cli/src/audio/analysis/providers/apple.ts b/packages/cli/src/audio/analysis/providers/apple.ts index ce3147147b..be0ee26fbe 100644 --- a/packages/cli/src/audio/analysis/providers/apple.ts +++ b/packages/cli/src/audio/analysis/providers/apple.ts @@ -1,3 +1,4 @@ +import { runsWithoutInstallPrompt } from "../../../utils/appleStubs.js"; import { createHash } from "node:crypto"; import { existsSync } from "node:fs"; import { mkdir, rename, writeFile } from "node:fs/promises"; @@ -70,6 +71,7 @@ function helperPath(name: string, source: string): string { } async function hasSwiftCompiler(): Promise { + if (!runsWithoutInstallPrompt("xcrun")) return false; try { await runFile("xcrun", ["--find", "swiftc"], { timeout: 30_000 }); return true; diff --git a/packages/cli/src/commands/skills.test.ts b/packages/cli/src/commands/skills.test.ts index cd612f0627..721f6a0b44 100644 --- a/packages/cli/src/commands/skills.test.ts +++ b/packages/cli/src/commands/skills.test.ts @@ -2,8 +2,11 @@ // `node:child_process` at the loader level and inspect the spawned // child's env. -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { EventEmitter } from "node:events"; +import { mkdirSync, mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; async function commandExitCode(): Promise { const { consumeCommandResult } = await import("../utils/commandResult.js"); @@ -177,8 +180,13 @@ function skillFlagValues(args: ReadonlyArray): string[] { return values; } +// Any test that plays a Mac gets developer tools, unless it says otherwise, wherever it runs. +const developerDir = mkdtempSync(join(tmpdir(), "hf-developer-")); +mkdirSync(join(developerDir, "usr", "bin"), { recursive: true }); + describe("hyperframes skills", () => { beforeEach(async () => { + vi.stubEnv("DEVELOPER_DIR", developerDir); state.execCalls = []; state.spawnCalls = []; state.spawnExitCode = 0; @@ -197,6 +205,7 @@ describe("hyperframes skills", () => { afterEach(async () => { setPlatform(originalPlatform); + vi.unstubAllEnvs(); vi.restoreAllMocks(); await resetCommandResult(); }); @@ -445,6 +454,18 @@ describe("hyperframes skills", () => { expect(trackSkillsInstallSkipped).toHaveBeenCalledWith({ reason: "git_missing" }); }); + it("bare `skills` on a Mac without the developer tools skips without running macOS's git stub", async () => { + setPlatform("darwin"); + vi.stubEnv("PATH", "/usr/bin"); + vi.stubEnv("DEVELOPER_DIR", "/nonexistent-developer-dir"); + + const { default: skillsCmd } = await import("./skills.js"); + await skillsCmd.run?.({ args: {}, rawArgs: [], cmd: skillsCmd } as never); + + expect(state.execCalls.map((call) => call.command)).not.toContain("git"); + expect(trackSkillsInstallSkipped).toHaveBeenCalledWith({ reason: "git_missing" }); + }); + // The happy path must never emit the prerequisite-skip event — it's a // skip-only diagnostic, not a per-install signal. it("bare `skills` does not emit the skip event when prerequisites are present", async () => { @@ -609,6 +630,8 @@ describe("hyperframes skills", () => { expect(invalidateSkillsCache).toHaveBeenCalled(); }); }); + + afterAll(() => rmSync(developerDir, { recursive: true, force: true })); }); // The router contract: `/hyperframes` picks a workflow, then runs diff --git a/packages/cli/src/commands/skills.ts b/packages/cli/src/commands/skills.ts index 9dc0e64f73..ba55f766b2 100644 --- a/packages/cli/src/commands/skills.ts +++ b/packages/cli/src/commands/skills.ts @@ -1,3 +1,4 @@ +import { runsWithoutInstallPrompt } from "../utils/appleStubs.js"; import { setCommandExitCode, CliResultSignal } from "../utils/commandResult.js"; import { defineCommand } from "../utils/defineCommand.js"; import { execFileSync, spawn } from "node:child_process"; @@ -50,6 +51,7 @@ function hasNpx(): boolean { // up front and skip cleanly instead of letting that surface. `git` resolves as // a real executable on every platform, so no cmd.exe wrapping is needed. function hasGit(): boolean { + if (!runsWithoutInstallPrompt("git")) return false; try { execFileSync("git", ["--version"], { stdio: "ignore", timeout: 5000 }); return true; diff --git a/packages/cli/src/utils/appleStubs.test.ts b/packages/cli/src/utils/appleStubs.test.ts new file mode 100644 index 0000000000..9644df3fc1 --- /dev/null +++ b/packages/cli/src/utils/appleStubs.test.ts @@ -0,0 +1,59 @@ +import { describe, expect, it } from "vitest"; +import { runsWithoutInstallPrompt } from "./appleStubs.js"; + +/** A Mac whose filesystem holds exactly `files`; `link` is where xcode-select's choice points, if it made one. */ +function mac(files: string[], { env = {}, link }: { env?: NodeJS.ProcessEnv; link?: string } = {}) { + return { + platform: "darwin" as const, + env, + exists: (path: string) => files.includes(path), + executable: (path: string) => files.includes(path), + realpath: (path: string) => (path === "/var/db/xcode_select_link" ? (link ?? null) : null), + }; +} +const TOOLS = "/Library/Developer/CommandLineTools/usr/bin"; + +describe("runsWithoutInstallPrompt", () => { + it("refuses macOS's git, xcrun and cc stubs when no developer tools are installed", () => { + for (const command of ["/usr/bin/git", "xcrun", "/usr/bin/cc"]) + expect(runsWithoutInstallPrompt(command, mac([])), command).toBe(false); + }); + + it("runs them once the Command Line Tools are installed", () => { + expect(runsWithoutInstallPrompt("/usr/bin/git", mac([TOOLS]))).toBe(true); + }); + + it("runs them on a Mac with only Xcode.app, where xcode-select never chose a folder", () => { + const xcode = "/Applications/Xcode.app/Contents/Developer/usr/bin"; + for (const command of ["/usr/bin/git", "xcrun", "/usr/bin/cc"]) + expect(runsWithoutInstallPrompt(command, mac([xcode])), command).toBe(true); + }); + + it("follows the developer folder xcode-select chose, or DEVELOPER_DIR", () => { + const xcode = "/Applications/Xcode-beta.app/Contents/Developer"; + expect(runsWithoutInstallPrompt("xcrun", mac([`${xcode}/usr/bin`], { link: xcode }))).toBe( + true, + ); + expect( + runsWithoutInstallPrompt( + "xcrun", + mac([`${xcode}/usr/bin`], { env: { DEVELOPER_DIR: xcode } }), + ), + ).toBe(true); + }); + + it("runs a git outside /usr/bin, a /usr/bin tool that is no stub, and anything off macOS", () => { + expect(runsWithoutInstallPrompt("/opt/homebrew/bin/git", mac([]))).toBe(true); + expect(runsWithoutInstallPrompt("/usr/bin/which", mac([]))).toBe(true); + expect(runsWithoutInstallPrompt("/usr/bin/git", { ...mac([]), platform: "linux" })).toBe(true); + }); +}); + +describe("runsWithoutInstallPrompt with a bare name", () => { + it("runs the first executable on PATH, found without running anything", () => { + const env = { PATH: "/opt/homebrew/bin:/usr/bin" }; + const brew = mac(["/opt/homebrew/bin/git", "/usr/bin/git"], { env }); + expect(runsWithoutInstallPrompt("git", brew)).toBe(true); + expect(runsWithoutInstallPrompt("git", mac(["/usr/bin/git"], { env }))).toBe(false); + }); +}); diff --git a/packages/cli/src/utils/appleStubs.ts b/packages/cli/src/utils/appleStubs.ts new file mode 100644 index 0000000000..b1f0ac04ea --- /dev/null +++ b/packages/cli/src/utils/appleStubs.ts @@ -0,0 +1,63 @@ +import { accessSync, constants, existsSync, realpathSync } from "node:fs"; +import { basename, dirname, join } from "node:path/posix"; + +// Without the developer tools these /usr/bin commands are stubs that open macOS's install dialog and fail. +const STUBS = new Set(["git", "xcrun", "make", "cc", "clang", "gcc", "python3", "swift", "swiftc"]); +const XCODE = "/Applications/Xcode.app/Contents/Developer"; +const COMMAND_LINE_TOOLS = "/Library/Developer/CommandLineTools"; + +type Host = { + platform: NodeJS.Platform; + env: NodeJS.ProcessEnv; + exists: (path: string) => boolean; + executable: (path: string) => boolean; + realpath: (path: string) => string | null; +}; + +const host: Host = { + get platform() { + return process.platform; + }, + get env() { + return process.env; + }, + exists: existsSync, + executable: (path) => { + try { + accessSync(path, constants.X_OK); + return true; + } catch { + return false; + } + }, + realpath: (path) => { + try { + return realpathSync(path); + } catch { + return null; + } + }, +}; + +/** Where xcode-select looks: DEVELOPER_DIR, the folder chosen with `xcode-select -s`, else Xcode.app or the tools. */ +function developerDirs(h: Host): string[] { + const chosen = h.env.DEVELOPER_DIR || h.realpath("/var/db/xcode_select_link"); + return chosen ? [chosen] : [XCODE, COMMAND_LINE_TOOLS]; +} + +/** Whether running `command` (a path, or a name looked up on PATH) does the work rather than open that dialog. */ +export function runsWithoutInstallPrompt(command: string, h: Host = host): boolean { + if (h.platform !== "darwin") return true; + const file = command.includes("/") ? command : (onPath(command, h) ?? join("/usr/bin", command)); + if (dirname(file) !== "/usr/bin" || !STUBS.has(basename(file))) return true; + return developerDirs(h).some((dir) => h.exists(join(dir, "usr", "bin"))); +} + +/** The first executable `name` on a Mac's PATH, found without running anything. */ +function onPath(name: string, h: Host): string | undefined { + for (const dir of (h.env.PATH ?? "").split(":")) { + const file = dir && join(dir, name); + if (file && h.executable(file)) return file; + } + return undefined; +} diff --git a/packages/cli/src/whisper/manager.ts b/packages/cli/src/whisper/manager.ts index d9aa5503a8..ab2cb7c94a 100644 --- a/packages/cli/src/whisper/manager.ts +++ b/packages/cli/src/whisper/manager.ts @@ -1,4 +1,5 @@ // fallow-ignore-file code-duplication complexity +import { runsWithoutInstallPrompt } from "../utils/appleStubs.js"; import { execFileSync } from "node:child_process"; import { existsSync, mkdirSync, readdirSync, rmSync, statSync } from "node:fs"; import { platform } from "node:os"; @@ -249,7 +250,8 @@ function hasBrew(): boolean { } function hasGit(): boolean { - return whichBinary("git") !== undefined; + const git = whichBinary("git"); + return git !== undefined && runsWithoutInstallPrompt(git); } function hasCmake(): boolean { @@ -294,7 +296,7 @@ export async function ensureWhisper(options?: { } // 3. Build from source (needs git + cmake + C compiler) - if (hasGit() && hasCmake()) { + if (hasGit() && hasCmake() && runsWithoutInstallPrompt("cc")) { try { return await buildFromSource(options?.onProgress, signal); } catch (err) {