mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
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
This commit is contained in:
@@ -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<boolean> {
|
||||
if (!runsWithoutInstallPrompt("xcrun")) return false;
|
||||
try {
|
||||
await runFile("xcrun", ["--find", "swiftc"], { timeout: 30_000 });
|
||||
return true;
|
||||
|
||||
@@ -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<number> {
|
||||
const { consumeCommandResult } = await import("../utils/commandResult.js");
|
||||
@@ -177,8 +180,13 @@ function skillFlagValues(args: ReadonlyArray<string>): 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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user