Skip to content

Commit eb8c22e

Browse files
authored
fix(release): retry plugin lock generation (#117668)
1 parent e49c833 commit eb8c22e

5 files changed

Lines changed: 98 additions & 8 deletions

‎scripts/generate-npm-package-lock.d.mts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export function createNpmPackageLockInstallStrategyArgs(options?: {
5252
export function generateNpmPackageLock(
5353
packageDir: string,
5454
options?: {
55+
env?: NodeJS.ProcessEnv;
5556
installStrategy?: "hoisted" | "nested" | "shallow" | "linked" | "" | null;
5657
},
5758
): string;

‎scripts/generate-npm-package-lock.mjs‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,9 @@ export function createNpmLockExecOptions(invocation, cwd, env = process.env) {
559559
};
560560
}
561561

562-
function runNpm(args, cwd) {
563-
const npm = createNpmLockCommand(args);
564-
execFileSync(npm.command, npm.args, createNpmLockExecOptions(npm, cwd));
562+
function runNpm(args, cwd, env = process.env) {
563+
const npm = createNpmLockCommand(args, { env });
564+
execFileSync(npm.command, npm.args, createNpmLockExecOptions(npm, cwd, env));
565565
}
566566

567567
function packageExtensionAppliesToDependency(selector, dependencyName) {
@@ -764,7 +764,7 @@ function describeOverrideViolations(violations) {
764764
.join("; ");
765765
}
766766

767-
function normalizeNpmLockOverrides(tempDir, npmLockOverrides, npmInstallArgs) {
767+
function normalizeNpmLockOverrides(tempDir, npmLockOverrides, npmInstallArgs, env) {
768768
const npmLockPath = path.join(tempDir, "package-lock.json");
769769
const overrideRules = exactOverrideRulesFromOverrides(npmLockOverrides);
770770
if (Object.keys(overrideRules).length === 0) {
@@ -787,7 +787,7 @@ function normalizeNpmLockOverrides(tempDir, npmLockOverrides, npmInstallArgs) {
787787
// shrinkwraps as inactive, drop their cached subtree, then ask npm to recalculate this
788788
// package's authoritative lock with registry integrity hashes.
789789
writeFileSync(npmLockPath, `${JSON.stringify(npmLock, null, 2)}\n`);
790-
runNpm(npmInstallArgs, tempDir);
790+
runNpm(npmInstallArgs, tempDir, env);
791791

792792
const normalized = JSON.parse(readFileSync(npmLockPath, "utf8"));
793793
const remaining = collectOverrideViolations(normalized, overrideRules);
@@ -832,6 +832,7 @@ export function createNpmPackageLockInstallStrategyArgs(options = {}) {
832832
export function generateNpmPackageLock(packageDir, options = {}) {
833833
const tempDir = mkdtempSync(path.join(tmpdir(), "openclaw-npm-lock-"));
834834
try {
835+
const env = options.env ?? process.env;
835836
const packageJson = JSON.parse(readFileSync(path.join(packageDir, "package.json"), "utf8"));
836837
const npmLockOverrides = readNpmLockOverrides();
837838
const peerResolutionArgs = shouldUseLegacyPeerDepsForNpmLock(packageJson)
@@ -851,8 +852,8 @@ export function generateNpmPackageLock(packageDir, options = {}) {
851852
`${JSON.stringify(packageJsonForNpmLock(packageJson, npmLockOverrides), null, 2)}\n`,
852853
);
853854
copyLocalFileDependencies(packageJson, packageDir, tempDir);
854-
runNpm(npmInstallArgs, tempDir);
855-
normalizeNpmLockOverrides(tempDir, npmLockOverrides, npmInstallArgs);
855+
runNpm(npmInstallArgs, tempDir, env);
856+
normalizeNpmLockOverrides(tempDir, npmLockOverrides, npmInstallArgs, env);
856857
const generated = normalizeNpmVersionDrift(
857858
applyPackageExtensionPeerMetadata(
858859
JSON.parse(readFileSync(path.join(tempDir, "package-lock.json"), "utf8")),

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ export function runPluginNpmCiWithRetry(
1515
options: Record<string, unknown>,
1616
params?: Record<string, unknown>,
1717
): unknown;
18+
/** Generate a plugin package lock with bounded retries for registry timeouts. */
19+
export function generatePluginNpmPackageLockWithRetry(
20+
packageDir: string,
21+
options?: {
22+
env?: NodeJS.ProcessEnv;
23+
installStrategy?: "hoisted" | "nested" | "shallow" | "linked" | "" | null;
24+
},
25+
params?: Record<string, unknown>,
26+
): string;
1827
/** Build the package.json that should be used while packaging a plugin for npm. */
1928
export function resolveAugmentedPluginNpmPackageJson(params: unknown):
2029
| {

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,33 @@ export function runPluginNpmCiWithRetry(args, options, params = {}) {
259259
return result;
260260
}
261261

262+
/** @internal Directly tested release-script implementation detail. */
263+
export function generatePluginNpmPackageLockWithRetry(packageDir, options = {}, params = {}) {
264+
const attempts = params.attempts ?? 3;
265+
const timeoutMs = params.timeoutMs ?? 180_000;
266+
const generate = params.generate ?? generateNpmPackageLock;
267+
const pluginDir = params.pluginDir ?? "plugin";
268+
const env = {
269+
...(options.env ?? process.env),
270+
OPENCLAW_NPM_LOCK_COMMAND_TIMEOUT_MS: String(timeoutMs),
271+
};
272+
273+
for (let attempt = 1; attempt <= attempts; attempt += 1) {
274+
try {
275+
return generate(packageDir, { ...options, env });
276+
} catch (error) {
277+
if (error?.code !== "ETIMEDOUT" || attempt === attempts) {
278+
throw error;
279+
}
280+
console.error(
281+
`[plugin-npm-publish] package-lock generation timed out for ${pluginDir} ` +
282+
`(attempt ${attempt}/${attempts}); retrying`,
283+
);
284+
}
285+
}
286+
throw new Error(`package-lock generation retry loop exhausted for ${pluginDir}`);
287+
}
288+
262289
function resolveInstalledPackageDir(packageDir, packageName) {
263290
return path.join(packageDir, "node_modules", ...packageName.split("/"));
264291
}
@@ -460,7 +487,11 @@ function installPackageLocalBundledDependencies(params) {
460487
try {
461488
fs.writeFileSync(
462489
packageLockPath,
463-
generateNpmPackageLock(params.packageDir, { installStrategy: "shallow" }),
490+
generatePluginNpmPackageLockWithRetry(
491+
params.packageDir,
492+
{ installStrategy: "shallow" },
493+
{ pluginDir: params.pluginDir },
494+
),
464495
"utf8",
465496
);
466497
const result = runPluginNpmCiWithRetry(

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node
44
import { dirname, join, win32 } from "node:path";
55
import { afterEach, describe, expect, it } from "vitest";
66
import {
7+
generatePluginNpmPackageLockWithRetry,
78
resolveAugmentedPluginNpmPackageJson,
89
resolveAugmentedPluginNpmManifest,
910
resolvePluginNpmCommand,
@@ -282,6 +283,53 @@ describe("plugin npm package manifest staging", () => {
282283
expect(secondResult.status).toBe(0);
283284
});
284285

286+
it("retries timed-out package-lock generation with a bounded command timeout", () => {
287+
const timeoutError = Object.assign(new Error("timed out"), { code: "ETIMEDOUT" });
288+
const generateOptions: Array<Record<string, unknown>> = [];
289+
let generateCalls = 0;
290+
291+
const lock = generatePluginNpmPackageLockWithRetry(
292+
"/tmp/plugin",
293+
{ installStrategy: "shallow" },
294+
{
295+
generate: (_packageDir: string, options: Record<string, unknown>) => {
296+
generateCalls += 1;
297+
generateOptions.push(options);
298+
if (generateCalls === 1) {
299+
throw timeoutError;
300+
}
301+
return '{"lockfileVersion":3}\n';
302+
},
303+
pluginDir: "whatsapp",
304+
},
305+
);
306+
307+
expect(lock).toBe('{"lockfileVersion":3}\n');
308+
expect(generateOptions).toHaveLength(2);
309+
expect(generateOptions[0]).toMatchObject({
310+
env: { OPENCLAW_NPM_LOCK_COMMAND_TIMEOUT_MS: "180000" },
311+
installStrategy: "shallow",
312+
});
313+
expect(generateOptions[1]).toEqual(generateOptions[0]);
314+
});
315+
316+
it("does not retry ordinary package-lock generation failures", () => {
317+
let generateCalls = 0;
318+
expect(() =>
319+
generatePluginNpmPackageLockWithRetry(
320+
"/tmp/plugin",
321+
{ installStrategy: "shallow" },
322+
{
323+
generate: () => {
324+
generateCalls += 1;
325+
throw new Error("invalid dependency");
326+
},
327+
},
328+
),
329+
).toThrow("invalid dependency");
330+
expect(generateCalls).toBe(1);
331+
});
332+
285333
it("overlays generated channel configs while packing and restores source manifest", () => {
286334
const repoDir = makeTempRepoRoot(tempDirs, "openclaw-plugin-npm-package-manifest-");
287335
const packageDir = join(repoDir, "extensions", "twitch");

0 commit comments

Comments
 (0)