Skip to content

Commit deffca6

Browse files
authored
fix(release): retry plugin dependency install timeouts (#117618)
1 parent 5dce1a8 commit deffca6

3 files changed

Lines changed: 136 additions & 2 deletions

File tree

‎scripts/lib/plugin-npm-package-manifest.d.mts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ export function resolvePluginNpmCommand(
99
shell: boolean;
1010
windowsVerbatimArguments?: boolean;
1111
};
12+
/** Run package-local npm ci with bounded retries for registry timeouts. */
13+
export function runPluginNpmCiWithRetry(
14+
args: unknown,
15+
options: Record<string, unknown>,
16+
params?: Record<string, unknown>,
17+
): unknown;
1218
/** Build the package.json that should be used while packaging a plugin for npm. */
1319
export function resolveAugmentedPluginNpmPackageJson(params: unknown):
1420
| {

‎scripts/lib/plugin-npm-package-manifest.mjs‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,36 @@ function spawnCommandSync(command, args, options) {
229229
return spawnSync(command, args, options);
230230
}
231231

232+
/** @internal Directly tested release-script implementation detail. */
233+
export function runPluginNpmCiWithRetry(args, options, params = {}) {
234+
const attempts = params.attempts ?? 3;
235+
const timeoutMs = params.timeoutMs ?? 180_000;
236+
const spawn = params.spawn ?? spawnNpmSync;
237+
const cleanupAttempt = params.cleanupAttempt ?? (() => {});
238+
const pluginDir = params.pluginDir ?? "plugin";
239+
240+
let result;
241+
for (let attempt = 1; attempt <= attempts; attempt += 1) {
242+
result = spawn(args, { ...options, timeout: timeoutMs });
243+
if (result.error?.code !== "ETIMEDOUT") {
244+
return result;
245+
}
246+
247+
// A timed-out npm process can leave a partial tree that makes the next
248+
// package attempt nondeterministic. Restore the staging invariant even
249+
// when the retry budget is exhausted.
250+
cleanupAttempt();
251+
if (attempt === attempts) {
252+
return result;
253+
}
254+
console.error(
255+
`[plugin-npm-publish] bundled dependency install timed out for ${pluginDir} ` +
256+
`(attempt ${attempt}/${attempts}); retrying`,
257+
);
258+
}
259+
return result;
260+
}
261+
232262
function resolveInstalledPackageDir(packageDir, packageName) {
233263
return path.join(packageDir, "node_modules", ...packageName.split("/"));
234264
}
@@ -433,7 +463,7 @@ function installPackageLocalBundledDependencies(params) {
433463
generateNpmPackageLock(params.packageDir, { installStrategy: "shallow" }),
434464
"utf8",
435465
);
436-
const result = spawnNpmSync(
466+
const result = runPluginNpmCiWithRetry(
437467
[
438468
"ci",
439469
"--install-strategy=shallow",
@@ -451,6 +481,10 @@ function installPackageLocalBundledDependencies(params) {
451481
env: process.env,
452482
stdio: ["ignore", "ignore", "inherit"],
453483
},
484+
{
485+
cleanupAttempt: () => fs.rmSync(nodeModulesPath, { recursive: true, force: true }),
486+
pluginDir: params.pluginDir,
487+
},
454488
);
455489
if (result.error) {
456490
throw result.error;

‎test/plugin-npm-package-manifest.test.ts‎

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
// Plugin npm manifest tests validate generated plugin package manifests.
22
import { spawnSync } from "node:child_process";
3-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
3+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
44
import { dirname, join, win32 } from "node:path";
55
import { afterEach, describe, expect, it } from "vitest";
66
import {
77
resolveAugmentedPluginNpmPackageJson,
88
resolveAugmentedPluginNpmManifest,
99
resolvePluginNpmCommand,
10+
runPluginNpmCiWithRetry,
1011
withAugmentedPluginNpmManifestForPackage,
1112
} from "../scripts/lib/plugin-npm-package-manifest.mjs";
1213
import { cleanupTempDirs, makeTempRepoRoot, writeJsonFile } from "./helpers/temp-repo.js";
@@ -188,6 +189,99 @@ describe("plugin npm package manifest staging", () => {
188189
).toThrow("OpenClaw refuses to shell out to bare npm on Windows");
189190
});
190191

192+
it("retries timed-out bundled dependency installs after cleaning partial output", () => {
193+
const timeoutError = Object.assign(new Error("timed out"), { code: "ETIMEDOUT" });
194+
const spawnResults = [
195+
{ error: timeoutError, status: null },
196+
{ error: undefined, status: 0 },
197+
];
198+
const spawnOptions: Array<Record<string, unknown>> = [];
199+
let cleanupCalls = 0;
200+
201+
const result = runPluginNpmCiWithRetry(
202+
["ci"],
203+
{ cwd: "/tmp/plugin" },
204+
{
205+
cleanupAttempt: () => {
206+
cleanupCalls += 1;
207+
},
208+
pluginDir: "whatsapp",
209+
spawn: (_args: string[], options: Record<string, unknown>) => {
210+
spawnOptions.push(options);
211+
return spawnResults.shift();
212+
},
213+
timeoutMs: 1234,
214+
},
215+
) as { status: number | null };
216+
217+
expect(result.status).toBe(0);
218+
expect(cleanupCalls).toBe(1);
219+
expect(spawnOptions).toEqual([
220+
{ cwd: "/tmp/plugin", timeout: 1234 },
221+
{ cwd: "/tmp/plugin", timeout: 1234 },
222+
]);
223+
});
224+
225+
it("does not retry ordinary bundled dependency install failures", () => {
226+
let spawnCalls = 0;
227+
const result = runPluginNpmCiWithRetry(
228+
["ci"],
229+
{},
230+
{
231+
cleanupAttempt: () => {
232+
throw new Error("cleanup should not run");
233+
},
234+
spawn: () => {
235+
spawnCalls += 1;
236+
return { error: undefined, status: 1 };
237+
},
238+
},
239+
) as { status: number | null };
240+
241+
expect(result.status).toBe(1);
242+
expect(spawnCalls).toBe(1);
243+
});
244+
245+
it("cleans an exhausted timeout before reusing the same package directory", () => {
246+
const repoDir = makeTempRepoRoot(tempDirs, "openclaw-plugin-npm-timeout-");
247+
const packageDir = join(repoDir, "extensions", "whatsapp");
248+
const nodeModulesPath = join(packageDir, "node_modules");
249+
const timeoutError = Object.assign(new Error("timed out"), { code: "ETIMEDOUT" });
250+
mkdirSync(packageDir, { recursive: true });
251+
252+
const firstResult = runPluginNpmCiWithRetry(
253+
["ci"],
254+
{ cwd: packageDir },
255+
{
256+
attempts: 3,
257+
cleanupAttempt: () => rmSync(nodeModulesPath, { recursive: true, force: true }),
258+
pluginDir: "whatsapp",
259+
spawn: () => {
260+
mkdirSync(nodeModulesPath, { recursive: true });
261+
return { error: timeoutError, status: null };
262+
},
263+
},
264+
) as { error?: NodeJS.ErrnoException };
265+
266+
expect(firstResult.error?.code).toBe("ETIMEDOUT");
267+
expect(existsSync(nodeModulesPath)).toBe(false);
268+
269+
const secondResult = runPluginNpmCiWithRetry(
270+
["ci"],
271+
{ cwd: packageDir },
272+
{
273+
cleanupAttempt: () => rmSync(nodeModulesPath, { recursive: true, force: true }),
274+
pluginDir: "whatsapp",
275+
spawn: () => {
276+
expect(existsSync(nodeModulesPath)).toBe(false);
277+
return { error: undefined, status: 0 };
278+
},
279+
},
280+
) as { status: number | null };
281+
282+
expect(secondResult.status).toBe(0);
283+
});
284+
191285
it("overlays generated channel configs while packing and restores source manifest", () => {
192286
const repoDir = makeTempRepoRoot(tempDirs, "openclaw-plugin-npm-package-manifest-");
193287
const packageDir = join(repoDir, "extensions", "twitch");

0 commit comments

Comments
 (0)