Skip to content

Commit c149730

Browse files
fix(cli): no macOS developer-tools prompt from git, xcrun or cc checks on a Mac without the tools (#7257)
* fix(cli): no macOS developer-tools prompt from git, xcrun or cc checks on a Mac without the tools * fix(cli): treat Xcode.app as installed tools and only take executables from PATH * test(cli): give skills tests a developer folder so darwin cases run on any host * fix(cli): read macOS tool paths the same way on every host
1 parent c9fe041 commit c149730

6 files changed

Lines changed: 154 additions & 3 deletions

File tree

‎packages/cli/src/audio/analysis/providers/apple.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { runsWithoutInstallPrompt } from "../../../utils/appleStubs.js";
12
import { createHash } from "node:crypto";
23
import { existsSync } from "node:fs";
34
import { mkdir, rename, writeFile } from "node:fs/promises";
@@ -70,6 +71,7 @@ function helperPath(name: string, source: string): string {
7071
}
7172

7273
async function hasSwiftCompiler(): Promise<boolean> {
74+
if (!runsWithoutInstallPrompt("xcrun")) return false;
7375
try {
7476
await runFile("xcrun", ["--find", "swiftc"], { timeout: 30_000 });
7577
return true;

‎packages/cli/src/commands/skills.test.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
// `node:child_process` at the loader level and inspect the spawned
33
// child's env.
44

5-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
5+
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest";
66
import { EventEmitter } from "node:events";
7+
import { mkdirSync, mkdtempSync, rmSync } from "node:fs";
8+
import { tmpdir } from "node:os";
9+
import { join } from "node:path";
710

811
async function commandExitCode(): Promise<number> {
912
const { consumeCommandResult } = await import("../utils/commandResult.js");
@@ -177,8 +180,13 @@ function skillFlagValues(args: ReadonlyArray<string>): string[] {
177180
return values;
178181
}
179182

183+
// Any test that plays a Mac gets developer tools, unless it says otherwise, wherever it runs.
184+
const developerDir = mkdtempSync(join(tmpdir(), "hf-developer-"));
185+
mkdirSync(join(developerDir, "usr", "bin"), { recursive: true });
186+
180187
describe("hyperframes skills", () => {
181188
beforeEach(async () => {
189+
vi.stubEnv("DEVELOPER_DIR", developerDir);
182190
state.execCalls = [];
183191
state.spawnCalls = [];
184192
state.spawnExitCode = 0;
@@ -197,6 +205,7 @@ describe("hyperframes skills", () => {
197205

198206
afterEach(async () => {
199207
setPlatform(originalPlatform);
208+
vi.unstubAllEnvs();
200209
vi.restoreAllMocks();
201210
await resetCommandResult();
202211
});
@@ -445,6 +454,18 @@ describe("hyperframes skills", () => {
445454
expect(trackSkillsInstallSkipped).toHaveBeenCalledWith({ reason: "git_missing" });
446455
});
447456

457+
it("bare `skills` on a Mac without the developer tools skips without running macOS's git stub", async () => {
458+
setPlatform("darwin");
459+
vi.stubEnv("PATH", "/usr/bin");
460+
vi.stubEnv("DEVELOPER_DIR", "/nonexistent-developer-dir");
461+
462+
const { default: skillsCmd } = await import("./skills.js");
463+
await skillsCmd.run?.({ args: {}, rawArgs: [], cmd: skillsCmd } as never);
464+
465+
expect(state.execCalls.map((call) => call.command)).not.toContain("git");
466+
expect(trackSkillsInstallSkipped).toHaveBeenCalledWith({ reason: "git_missing" });
467+
});
468+
448469
// The happy path must never emit the prerequisite-skip event — it's a
449470
// skip-only diagnostic, not a per-install signal.
450471
it("bare `skills` does not emit the skip event when prerequisites are present", async () => {
@@ -609,6 +630,8 @@ describe("hyperframes skills", () => {
609630
expect(invalidateSkillsCache).toHaveBeenCalled();
610631
});
611632
});
633+
634+
afterAll(() => rmSync(developerDir, { recursive: true, force: true }));
612635
});
613636

614637
// The router contract: `/hyperframes` picks a workflow, then runs

‎packages/cli/src/commands/skills.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { runsWithoutInstallPrompt } from "../utils/appleStubs.js";
12
import { setCommandExitCode, CliResultSignal } from "../utils/commandResult.js";
23
import { defineCommand } from "../utils/defineCommand.js";
34
import { execFileSync, spawn } from "node:child_process";
@@ -50,6 +51,7 @@ function hasNpx(): boolean {
5051
// up front and skip cleanly instead of letting that surface. `git` resolves as
5152
// a real executable on every platform, so no cmd.exe wrapping is needed.
5253
function hasGit(): boolean {
54+
if (!runsWithoutInstallPrompt("git")) return false;
5355
try {
5456
execFileSync("git", ["--version"], { stdio: "ignore", timeout: 5000 });
5557
return true;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { describe, expect, it } from "vitest";
2+
import { runsWithoutInstallPrompt } from "./appleStubs.js";
3+
4+
/** A Mac whose filesystem holds exactly `files`; `link` is where xcode-select's choice points, if it made one. */
5+
function mac(files: string[], { env = {}, link }: { env?: NodeJS.ProcessEnv; link?: string } = {}) {
6+
return {
7+
platform: "darwin" as const,
8+
env,
9+
exists: (path: string) => files.includes(path),
10+
executable: (path: string) => files.includes(path),
11+
realpath: (path: string) => (path === "/var/db/xcode_select_link" ? (link ?? null) : null),
12+
};
13+
}
14+
const TOOLS = "/Library/Developer/CommandLineTools/usr/bin";
15+
16+
describe("runsWithoutInstallPrompt", () => {
17+
it("refuses macOS's git, xcrun and cc stubs when no developer tools are installed", () => {
18+
for (const command of ["/usr/bin/git", "xcrun", "/usr/bin/cc"])
19+
expect(runsWithoutInstallPrompt(command, mac([])), command).toBe(false);
20+
});
21+
22+
it("runs them once the Command Line Tools are installed", () => {
23+
expect(runsWithoutInstallPrompt("/usr/bin/git", mac([TOOLS]))).toBe(true);
24+
});
25+
26+
it("runs them on a Mac with only Xcode.app, where xcode-select never chose a folder", () => {
27+
const xcode = "/Applications/Xcode.app/Contents/Developer/usr/bin";
28+
for (const command of ["/usr/bin/git", "xcrun", "/usr/bin/cc"])
29+
expect(runsWithoutInstallPrompt(command, mac([xcode])), command).toBe(true);
30+
});
31+
32+
it("follows the developer folder xcode-select chose, or DEVELOPER_DIR", () => {
33+
const xcode = "/Applications/Xcode-beta.app/Contents/Developer";
34+
expect(runsWithoutInstallPrompt("xcrun", mac([`${xcode}/usr/bin`], { link: xcode }))).toBe(
35+
true,
36+
);
37+
expect(
38+
runsWithoutInstallPrompt(
39+
"xcrun",
40+
mac([`${xcode}/usr/bin`], { env: { DEVELOPER_DIR: xcode } }),
41+
),
42+
).toBe(true);
43+
});
44+
45+
it("runs a git outside /usr/bin, a /usr/bin tool that is no stub, and anything off macOS", () => {
46+
expect(runsWithoutInstallPrompt("/opt/homebrew/bin/git", mac([]))).toBe(true);
47+
expect(runsWithoutInstallPrompt("/usr/bin/which", mac([]))).toBe(true);
48+
expect(runsWithoutInstallPrompt("/usr/bin/git", { ...mac([]), platform: "linux" })).toBe(true);
49+
});
50+
});
51+
52+
describe("runsWithoutInstallPrompt with a bare name", () => {
53+
it("runs the first executable on PATH, found without running anything", () => {
54+
const env = { PATH: "/opt/homebrew/bin:/usr/bin" };
55+
const brew = mac(["/opt/homebrew/bin/git", "/usr/bin/git"], { env });
56+
expect(runsWithoutInstallPrompt("git", brew)).toBe(true);
57+
expect(runsWithoutInstallPrompt("git", mac(["/usr/bin/git"], { env }))).toBe(false);
58+
});
59+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { accessSync, constants, existsSync, realpathSync } from "node:fs";
2+
import { basename, dirname, join } from "node:path/posix";
3+
4+
// Without the developer tools these /usr/bin commands are stubs that open macOS's install dialog and fail.
5+
const STUBS = new Set(["git", "xcrun", "make", "cc", "clang", "gcc", "python3", "swift", "swiftc"]);
6+
const XCODE = "/Applications/Xcode.app/Contents/Developer";
7+
const COMMAND_LINE_TOOLS = "/Library/Developer/CommandLineTools";
8+
9+
type Host = {
10+
platform: NodeJS.Platform;
11+
env: NodeJS.ProcessEnv;
12+
exists: (path: string) => boolean;
13+
executable: (path: string) => boolean;
14+
realpath: (path: string) => string | null;
15+
};
16+
17+
const host: Host = {
18+
get platform() {
19+
return process.platform;
20+
},
21+
get env() {
22+
return process.env;
23+
},
24+
exists: existsSync,
25+
executable: (path) => {
26+
try {
27+
accessSync(path, constants.X_OK);
28+
return true;
29+
} catch {
30+
return false;
31+
}
32+
},
33+
realpath: (path) => {
34+
try {
35+
return realpathSync(path);
36+
} catch {
37+
return null;
38+
}
39+
},
40+
};
41+
42+
/** Where xcode-select looks: DEVELOPER_DIR, the folder chosen with `xcode-select -s`, else Xcode.app or the tools. */
43+
function developerDirs(h: Host): string[] {
44+
const chosen = h.env.DEVELOPER_DIR || h.realpath("/var/db/xcode_select_link");
45+
return chosen ? [chosen] : [XCODE, COMMAND_LINE_TOOLS];
46+
}
47+
48+
/** Whether running `command` (a path, or a name looked up on PATH) does the work rather than open that dialog. */
49+
export function runsWithoutInstallPrompt(command: string, h: Host = host): boolean {
50+
if (h.platform !== "darwin") return true;
51+
const file = command.includes("/") ? command : (onPath(command, h) ?? join("/usr/bin", command));
52+
if (dirname(file) !== "/usr/bin" || !STUBS.has(basename(file))) return true;
53+
return developerDirs(h).some((dir) => h.exists(join(dir, "usr", "bin")));
54+
}
55+
56+
/** The first executable `name` on a Mac's PATH, found without running anything. */
57+
function onPath(name: string, h: Host): string | undefined {
58+
for (const dir of (h.env.PATH ?? "").split(":")) {
59+
const file = dir && join(dir, name);
60+
if (file && h.executable(file)) return file;
61+
}
62+
return undefined;
63+
}

‎packages/cli/src/whisper/manager.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// fallow-ignore-file code-duplication complexity
2+
import { runsWithoutInstallPrompt } from "../utils/appleStubs.js";
23
import { execFileSync } from "node:child_process";
34
import { existsSync, mkdirSync, readdirSync, rmSync, statSync } from "node:fs";
45
import { platform } from "node:os";
@@ -249,7 +250,8 @@ function hasBrew(): boolean {
249250
}
250251

251252
function hasGit(): boolean {
252-
return whichBinary("git") !== undefined;
253+
const git = whichBinary("git");
254+
return git !== undefined && runsWithoutInstallPrompt(git);
253255
}
254256

255257
function hasCmake(): boolean {
@@ -294,7 +296,7 @@ export async function ensureWhisper(options?: {
294296
}
295297

296298
// 3. Build from source (needs git + cmake + C compiler)
297-
if (hasGit() && hasCmake()) {
299+
if (hasGit() && hasCmake() && runsWithoutInstallPrompt("cc")) {
298300
try {
299301
return await buildFromSource(options?.onProgress, signal);
300302
} catch (err) {

0 commit comments

Comments
 (0)