Skip to content

Commit 81e5320

Browse files
committed
fix(scripts): bypass gh wrapper shims
1 parent e9f9a68 commit 81e5320

3 files changed

Lines changed: 44 additions & 5 deletions

File tree

scripts/lib/plain-gh.mjs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import fs from "node:fs";
33
import path from "node:path";
44

55
export const PLAIN_GH_MAX_BUFFER_BYTES = 32 * 1024 * 1024;
6+
export const PLAIN_GH_SYSTEM_CANDIDATES = [
7+
// Prefer package-manager opt paths: bin/gh may intentionally be an Octopool shim.
8+
"/opt/homebrew/opt/gh/bin/gh",
9+
"/usr/local/opt/gh/bin/gh",
10+
"/home/linuxbrew/.linuxbrew/opt/gh/bin/gh",
11+
"/opt/homebrew/bin/gh",
12+
"/usr/local/bin/gh",
13+
];
614

715
function isExecutable(filePath) {
816
try {
@@ -32,15 +40,18 @@ export function plainGhEnv(env = process.env) {
3240
return next;
3341
}
3442

35-
export function resolvePlainGhBin(env = process.env) {
43+
export function resolvePlainGhBin(
44+
env = process.env,
45+
systemCandidates = PLAIN_GH_SYSTEM_CANDIDATES,
46+
) {
3647
if (env.OPENCLAW_GH_BIN) {
3748
if (isExecutable(env.OPENCLAW_GH_BIN)) {
3849
return env.OPENCLAW_GH_BIN;
3950
}
4051
throw new Error(`OPENCLAW_GH_BIN is not executable: ${env.OPENCLAW_GH_BIN}`);
4152
}
4253

43-
for (const candidate of ["/opt/homebrew/bin/gh", "/usr/local/bin/gh"]) {
54+
for (const candidate of systemCandidates) {
4455
if (isExecutable(candidate)) {
4556
return candidate;
4657
}

scripts/lib/plain-gh.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ resolve_plain_gh_bin() {
2424
fi
2525

2626
local candidate
27-
for candidate in /opt/homebrew/bin/gh /usr/local/bin/gh; do
27+
while IFS= read -r candidate; do
2828
if [ -x "$candidate" ]; then
2929
printf '%s\n' "$candidate"
3030
return 0
3131
fi
32-
done
32+
done < <(plain_gh_system_candidates)
3333

3434
if candidate=$(PATH="$(plain_gh_search_path)" type -P gh 2>/dev/null); then
3535
printf '%s\n' "$candidate"
@@ -39,6 +39,16 @@ resolve_plain_gh_bin() {
3939
type -P gh 2>/dev/null
4040
}
4141

42+
plain_gh_system_candidates() {
43+
# bin/gh may intentionally be an Octopool shim; prefer package-manager opt paths.
44+
printf '%s\n' \
45+
/opt/homebrew/opt/gh/bin/gh \
46+
/usr/local/opt/gh/bin/gh \
47+
/home/linuxbrew/.linuxbrew/opt/gh/bin/gh \
48+
/opt/homebrew/bin/gh \
49+
/usr/local/bin/gh
50+
}
51+
4252
plain_gh_search_path() {
4353
local path_value="${PATH:-}"
4454
local home_bin="${HOME:-}/bin"

test/scripts/plain-gh.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync
44
import { tmpdir } from "node:os";
55
import path from "node:path";
66
import { afterEach, describe, expect, it } from "vitest";
7-
import { execPlainGh, plainGhEnv, resolvePlainGhBin } from "../../scripts/lib/plain-gh.mjs";
7+
import {
8+
execPlainGh,
9+
plainGhEnv,
10+
PLAIN_GH_SYSTEM_CANDIDATES,
11+
resolvePlainGhBin,
12+
} from "../../scripts/lib/plain-gh.mjs";
813

914
const tempDirs: string[] = [];
1015

@@ -64,6 +69,16 @@ describe("plain gh helpers", () => {
6469
).toBe(ghPath);
6570
});
6671

72+
it("prefers package-manager gh paths over bin shims", () => {
73+
const realGh = makeFakeGh();
74+
const shimGh = makeFakeGh();
75+
76+
expect(resolvePlainGhBin({ PATH: shimGh }, [realGh, shimGh])).toBe(realGh);
77+
expect(PLAIN_GH_SYSTEM_CANDIDATES.indexOf("/opt/homebrew/opt/gh/bin/gh")).toBeLessThan(
78+
PLAIN_GH_SYSTEM_CANDIDATES.indexOf("/opt/homebrew/bin/gh"),
79+
);
80+
});
81+
6782
it("normalizes color environment for JSON-safe gh output", () => {
6883
expect(
6984
plainGhEnv({
@@ -135,5 +150,8 @@ describe("plain gh helpers", () => {
135150

136151
expect(helper).toContain("type -P gh");
137152
expect(helper).not.toContain("command -v gh");
153+
expect(helper.indexOf("/opt/homebrew/opt/gh/bin/gh")).toBeLessThan(
154+
helper.indexOf("/opt/homebrew/bin/gh"),
155+
);
138156
});
139157
});

0 commit comments

Comments
 (0)