Skip to content

Commit bd74a62

Browse files
committed
fix(install): use repo pnpm for git installs
1 parent 9f888d9 commit bd74a62

4 files changed

Lines changed: 140 additions & 0 deletions

File tree

scripts/install-cli.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,15 @@ EOF
580580
}
581581

582582
run_pnpm() {
583+
if [[ ${#PNPM_CMD[@]} -eq 2 && "${PNPM_CMD[1]}" == "pnpm" ]] && [[ "${1:-}" == "-C" && -n "${2:-}" ]]; then
584+
local repo_dir="$2"
585+
shift 2
586+
if ! (cd "$repo_dir" && "${PNPM_CMD[@]}" --version >/dev/null 2>&1); then
587+
ensure_pnpm
588+
fi
589+
(cd "$repo_dir" && "${PNPM_CMD[@]}" "$@")
590+
return
591+
fi
583592
if ! pnpm_cmd_is_ready; then
584593
ensure_pnpm
585594
fi
@@ -739,6 +748,10 @@ activate_repo_pnpm_version() {
739748
if [[ -n "$corepack_cmd" ]]; then
740749
log "Activating repo pnpm ${version}"
741750
"$corepack_cmd" prepare "pnpm@${version}" --activate >/dev/null 2>&1 || true
751+
if [[ "$(cd "$repo_dir" && "$corepack_cmd" pnpm --version 2>/dev/null || true)" == "$version" ]]; then
752+
set_pnpm_cmd "$corepack_cmd" pnpm
753+
return 0
754+
fi
742755
detect_pnpm_cmd || true
743756
fi
744757
}

scripts/install.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,6 +2128,15 @@ EOF
21282128
}
21292129

21302130
run_pnpm() {
2131+
if [[ "${PNPM_CMD[*]}" == "corepack pnpm" && "${1:-}" == "-C" && -n "${2:-}" ]]; then
2132+
local repo_dir="$2"
2133+
shift 2
2134+
if ! (cd "$repo_dir" && "${PNPM_CMD[@]}" --version >/dev/null 2>&1); then
2135+
ensure_pnpm
2136+
fi
2137+
(cd "$repo_dir" && "${PNPM_CMD[@]}" "$@")
2138+
return
2139+
fi
21312140
if ! pnpm_cmd_is_ready; then
21322141
ensure_pnpm
21332142
fi
@@ -2260,6 +2269,10 @@ activate_repo_pnpm_version() {
22602269
ui_info "Activating repo pnpm ${version}"
22612270
corepack prepare "pnpm@${version}" --activate >/dev/null 2>&1 || true
22622271
refresh_shell_command_cache
2272+
if [[ "$(cd "$repo_dir" && corepack pnpm --version 2>/dev/null || true)" == "$version" ]]; then
2273+
set_pnpm_cmd corepack pnpm
2274+
return 0
2275+
fi
22632276
detect_pnpm_cmd || true
22642277
fi
22652278
}

test/scripts/install-cli.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,63 @@ describe("install-cli.sh", () => {
158158
expect(script).toContain('activate_repo_pnpm_version "$repo_dir"');
159159
});
160160

161+
it("uses the repo Corepack pnpm when a global pnpm version is already present", () => {
162+
const tmp = mkdtempSync(join(tmpdir(), "openclaw-install-cli-pnpm-version-"));
163+
const bin = join(tmp, "bin");
164+
const outer = join(tmp, "outer");
165+
const repo = join(tmp, "repo");
166+
mkdirSync(bin, { recursive: true });
167+
mkdirSync(outer, { recursive: true });
168+
mkdirSync(repo, { recursive: true });
169+
writeFileSync(
170+
join(outer, "package.json"),
171+
'{\n "packageManager": "[email protected]"\n}\n',
172+
);
173+
writeFileSync(
174+
join(repo, "package.json"),
175+
'{\n "packageManager": "[email protected]+sha512.test"\n}\n',
176+
);
177+
writeFileSync(
178+
join(bin, "pnpm"),
179+
["#!/bin/bash", '[[ "${1:-}" == "--version" ]] && echo "11.8.0"', ""].join("\n"),
180+
);
181+
writeFileSync(
182+
join(bin, "corepack"),
183+
[
184+
"#!/bin/bash",
185+
'if [[ "${1:-}" == "prepare" ]]; then exit 0; fi',
186+
'if [[ "${1:-}" == "pnpm" && "${2:-}" == "--version" ]]; then',
187+
' if grep -q "[email protected]" package.json 2>/dev/null; then echo "11.2.2"; else exit 1; fi',
188+
" exit 0",
189+
"fi",
190+
"exit 1",
191+
"",
192+
].join("\n"),
193+
);
194+
chmodSync(join(bin, "pnpm"), 0o755);
195+
chmodSync(join(bin, "corepack"), 0o755);
196+
197+
try {
198+
const result = runInstallCliShell(
199+
[
200+
`cd ${JSON.stringify(process.cwd())}`,
201+
`source ${JSON.stringify(SCRIPT_PATH)}`,
202+
`cd ${JSON.stringify(outer)}`,
203+
`activate_repo_pnpm_version ${JSON.stringify(repo)}`,
204+
'printf "cmd=%s\\n" "${PNPM_CMD[*]}"',
205+
`printf "run=%s\\n" "$(run_pnpm -C ${JSON.stringify(repo)} --version)"`,
206+
].join("\n"),
207+
{ PATH: `${bin}:${process.env.PATH ?? ""}` },
208+
);
209+
210+
expect(result.status).toBe(0);
211+
expect(result.stdout).toContain(`cmd=${join(bin, "corepack")} pnpm`);
212+
expect(result.stdout).toContain("run=11.2.2");
213+
} finally {
214+
rmSync(tmp, { force: true, recursive: true });
215+
}
216+
});
217+
161218
it("links an existing usable Alpine/musl Node runtime without sudo", () => {
162219
const tmp = mkdtempSync(join(tmpdir(), "openclaw-install-cli-alpine-"));
163220
const bin = join(tmp, "bin");

test/scripts/install-sh.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,63 @@ describe("install.sh", () => {
13501350
expect(script).toContain('activate_repo_pnpm_version "$repo_dir"');
13511351
});
13521352

1353+
it("uses the repo Corepack pnpm when a global pnpm version is already present", () => {
1354+
const tmp = mkdtempSync(join(tmpdir(), "openclaw-install-pnpm-version-"));
1355+
const bin = join(tmp, "bin");
1356+
const outer = join(tmp, "outer");
1357+
const repo = join(tmp, "repo");
1358+
mkdirSync(bin, { recursive: true });
1359+
mkdirSync(outer, { recursive: true });
1360+
mkdirSync(repo, { recursive: true });
1361+
writeFileSync(
1362+
join(outer, "package.json"),
1363+
'{\n "packageManager": "[email protected]"\n}\n',
1364+
);
1365+
writeFileSync(
1366+
join(repo, "package.json"),
1367+
'{\n "packageManager": "[email protected]+sha512.test"\n}\n',
1368+
);
1369+
writeFileSync(
1370+
join(bin, "pnpm"),
1371+
["#!/bin/bash", '[[ "${1:-}" == "--version" ]] && echo "11.8.0"', ""].join("\n"),
1372+
);
1373+
writeFileSync(
1374+
join(bin, "corepack"),
1375+
[
1376+
"#!/bin/bash",
1377+
'if [[ "${1:-}" == "prepare" ]]; then exit 0; fi',
1378+
'if [[ "${1:-}" == "pnpm" && "${2:-}" == "--version" ]]; then',
1379+
' if grep -q "[email protected]" package.json 2>/dev/null; then echo "11.2.2"; else exit 1; fi',
1380+
" exit 0",
1381+
"fi",
1382+
"exit 1",
1383+
"",
1384+
].join("\n"),
1385+
);
1386+
chmodSync(join(bin, "pnpm"), 0o755);
1387+
chmodSync(join(bin, "corepack"), 0o755);
1388+
1389+
try {
1390+
const result = runInstallShell(
1391+
[
1392+
`cd ${JSON.stringify(process.cwd())}`,
1393+
`source ${JSON.stringify(SCRIPT_PATH)}`,
1394+
`cd ${JSON.stringify(outer)}`,
1395+
`activate_repo_pnpm_version ${JSON.stringify(repo)}`,
1396+
'printf "cmd=%s\\n" "${PNPM_CMD[*]}"',
1397+
`printf "run=%s\\n" "$(run_pnpm -C ${JSON.stringify(repo)} --version)"`,
1398+
].join("\n"),
1399+
{ PATH: `${bin}:${process.env.PATH ?? ""}` },
1400+
);
1401+
1402+
expect(result.status).toBe(0);
1403+
expect(result.stdout).toContain("cmd=corepack pnpm");
1404+
expect(result.stdout).toContain("run=11.2.2");
1405+
} finally {
1406+
rmSync(tmp, { force: true, recursive: true });
1407+
}
1408+
});
1409+
13531410
it("does not treat /dev/tty permissions as a controlling terminal", () => {
13541411
const result = runInstallShell(`
13551412
set -euo pipefail

0 commit comments

Comments
 (0)