Skip to content

Commit 7866eca

Browse files
authored
fix(release): keep npm 12 pack checks working (#118209)
* fix(release): accept npm 12 pack json * fix(release): share npm pack normalizer in plugin workflow
1 parent ccec549 commit 7866eca

20 files changed

Lines changed: 205 additions & 54 deletions

‎.github/workflows/openclaw-cross-os-release-checks-reusable.yml‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -641,10 +641,11 @@ jobs:
641641
env:
642642
BASELINE_PACK_JSON: ${{ runner.temp }}/openclaw-cross-os-release-checks/prepare/baseline/pack.json
643643
run: |
644-
node <<'NODE' >>"$GITHUB_OUTPUT"
645-
const crypto = require("node:crypto");
646-
const fs = require("node:fs");
647-
const path = require("node:path");
644+
node --input-type=module <<'NODE' >>"$GITHUB_OUTPUT"
645+
import crypto from "node:crypto";
646+
import fs from "node:fs";
647+
import path from "node:path";
648+
import { resolveNpmJsonEntries } from "./scripts/lib/npm-json-output.mjs";
648649
function resolveTarballFileName(value, label) {
649650
const fileName = typeof value === "string" ? value.trim() : "";
650651
if (
@@ -658,7 +659,7 @@ jobs:
658659
return fileName;
659660
}
660661
const payload = JSON.parse(fs.readFileSync(process.env.BASELINE_PACK_JSON, "utf8"));
661-
const entry = Array.isArray(payload) ? payload.at(-1) : null;
662+
const entry = resolveNpmJsonEntries(payload).at(-1);
662663
const fileName = resolveTarballFileName(entry?.filename, "Baseline npm pack filename");
663664
const sha256 = crypto
664665
.createHash("sha256")

‎.github/workflows/plugin-npm-release.yml‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ on:
1111
- "package.json"
1212
- "scripts/generate-npm-package-lock.mjs"
1313
- "scripts/lib/npm-publish-plan.mjs"
14+
- "scripts/lib/npm-json-output.mjs"
1415
- "scripts/lib/release-version.mjs"
1516
- "scripts/lib/plugin-npm-package-manifest.mjs"
1617
- "scripts/lib/plugin-npm-release.ts"
@@ -338,6 +339,7 @@ jobs:
338339
fetch-depth: 1
339340
sparse-checkout: |
340341
scripts/generate-npm-package-lock.mjs
342+
scripts/lib/npm-json-output.mjs
341343
scripts/lib/plugin-npm-package-manifest.mjs
342344
sparse-checkout-cone-mode: false
343345

@@ -347,6 +349,9 @@ jobs:
347349
cp \
348350
.release-tooling/scripts/generate-npm-package-lock.mjs \
349351
scripts/generate-npm-package-lock.mjs
352+
cp \
353+
.release-tooling/scripts/lib/npm-json-output.mjs \
354+
scripts/lib/npm-json-output.mjs
350355
cp \
351356
.release-tooling/scripts/lib/plugin-npm-package-manifest.mjs \
352357
scripts/lib/plugin-npm-package-manifest.mjs
@@ -398,26 +403,27 @@ jobs:
398403
OPENCLAW_PLUGIN_NPM_PACK_OUTPUT_DIR="${artifact_dir}" \
399404
bash scripts/plugin-npm-publish.sh --pack "${PACKAGE_DIR}" > "${pack_output}"
400405
401-
node - "${pack_output}" "${pack_json}" <<'NODE'
402-
const fs = require("node:fs");
406+
node --input-type=module - "${pack_output}" "${pack_json}" <<'NODE'
407+
import fs from "node:fs";
408+
import { resolveNpmJsonEntries } from "./scripts/lib/npm-json-output.mjs";
409+
403410
const raw = fs.readFileSync(process.argv[2], "utf8").trim();
404411
let pack;
405-
for (let index = raw.lastIndexOf("["); index >= 0; index = raw.lastIndexOf("[", index - 1)) {
412+
for (let index = raw.length - 1; index >= 0; index -= 1) {
413+
if (raw[index] !== "[" && raw[index] !== "{") continue;
406414
try {
407415
const candidate = JSON.parse(raw.slice(index));
408-
if (Array.isArray(candidate)) {
409-
pack = candidate;
416+
const entries = resolveNpmJsonEntries(candidate);
417+
if (entries.length > 0) {
418+
pack = entries;
410419
break;
411420
}
412421
} catch {
413422
// npm can print bundled-dependency summaries before its JSON result.
414423
}
415-
if (index === 0) {
416-
break;
417-
}
418424
}
419425
if (!pack) {
420-
throw new Error("npm pack did not emit a JSON array.");
426+
throw new Error("npm pack did not emit package JSON.");
421427
}
422428
fs.writeFileSync(process.argv[3], `${JSON.stringify(pack, null, 2)}\n`);
423429
NODE
@@ -1238,6 +1244,9 @@ jobs:
12381244
cp \
12391245
scripts/generate-npm-package-lock.mjs \
12401246
.publication-target/scripts/generate-npm-package-lock.mjs
1247+
cp \
1248+
scripts/lib/npm-json-output.mjs \
1249+
.publication-target/scripts/lib/npm-json-output.mjs
12411250
cp \
12421251
scripts/lib/plugin-npm-package-manifest.mjs \
12431252
.publication-target/scripts/lib/plugin-npm-package-manifest.mjs

‎scripts/e2e/parallels/package-artifact.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { randomUUID } from "node:crypto";
33
import { mkdir, readFile, rm, stat, writeFile } from "node:fs/promises";
44
import { tmpdir } from "node:os";
55
import path from "node:path";
6+
import { resolveNpmJsonEntries } from "../../lib/npm-json-output.mjs";
67
import { sleep as delay } from "../../lib/sleep.mjs";
78
import { readPositiveIntEnv } from "./env-limits.ts";
89
import { exists, readJson } from "./filesystem.ts";
@@ -28,12 +29,7 @@ export async function packageBuildCommitFromTgz(tgzPath: string): Promise<string
2829
}
2930

3031
function resolveNpmPackTarballFilename(value: unknown): string {
31-
// npm 10/11 return arrays; npm 12 keys local-workspace results by package name.
32-
const result = Array.isArray(value)
33-
? value.at(-1)
34-
: value && typeof value === "object" && "openclaw" in value
35-
? value.openclaw
36-
: value;
32+
const result = resolveNpmJsonEntries(value).at(-1);
3733
const filename =
3834
result &&
3935
typeof result === "object" &&

‎scripts/lib/npm-json-output.d.mts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export declare function resolveNpmJsonEntries(value: unknown): unknown[];

‎scripts/lib/npm-json-output.mjs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Normalize npm <=11 entry/array JSON and npm 12 name-keyed JSON.
3+
* Keep pack consumers on one dependency-contract boundary so a package-manager
4+
* upgrade cannot silently bypass release, installer, or security checks.
5+
*/
6+
export function resolveNpmJsonEntries(value) {
7+
if (Array.isArray(value)) {
8+
return value;
9+
}
10+
if (value && typeof value === "object") {
11+
const looksLikeEntry =
12+
typeof value.id === "string" ||
13+
typeof value.name === "string" ||
14+
typeof value.version === "string" ||
15+
typeof value.filename === "string";
16+
if (!looksLikeEntry) {
17+
const entries = Object.values(value).filter(
18+
(entry) => Boolean(entry) && typeof entry === "object" && !Array.isArray(entry),
19+
);
20+
if (entries.length > 0) {
21+
return entries;
22+
}
23+
}
24+
}
25+
return [value];
26+
}

‎scripts/lib/npm-pack-budget.d.mts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ export type NpmPackBudgetResult = {
33
unpackedSize?: number;
44
};
55

6+
export type NpmPackBudgetResults =
7+
| Iterable<NpmPackBudgetResult>
8+
| NpmPackBudgetResult
9+
| Record<string, NpmPackBudgetResult>;
10+
611
export declare function collectPackUnpackedSizeErrors(
7-
results: Iterable<NpmPackBudgetResult>,
12+
results: NpmPackBudgetResults,
813
options?: {
914
budgetBytes?: number;
1015
missingDataMessage?: string;

‎scripts/lib/npm-pack-budget.mjs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { resolveNpmJsonEntries } from "./npm-json-output.mjs";
2+
13
// 2026.3.12 ballooned to ~213.6 MiB unpacked and correlated with low-memory
24
// startup/doctor OOM reports. 2026.4.12 intentionally stages Matrix runtime
35
// dependencies, including crypto wasm, so packaged installs do not miss Docker
@@ -22,12 +24,15 @@ function formatPackUnpackedSizeBudgetError(params) {
2224
}
2325

2426
export function collectPackUnpackedSizeErrors(results, options = {}) {
25-
const entries = Array.from(results);
27+
const entries = resolveNpmJsonEntries(results);
2628
const errors = [];
2729
const budgetBytes = options.budgetBytes ?? NPM_PACK_UNPACKED_SIZE_BUDGET_BYTES;
2830
let checkedCount = 0;
2931

3032
for (const [index, entry] of entries.entries()) {
33+
if (!entry || typeof entry !== "object" || Array.isArray(entry)) {
34+
continue;
35+
}
3136
if (typeof entry.unpackedSize !== "number" || !Number.isFinite(entry.unpackedSize)) {
3237
continue;
3338
}

‎scripts/openclaw-npm-release-check.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { execFileSync } from "node:child_process";
55
import { readFileSync } from "node:fs";
66
import { join } from "node:path";
77
import { pathToFileURL } from "node:url";
8+
import { resolveNpmJsonEntries } from "./lib/npm-json-output.mjs";
89
import { resolveNpmDistTagMirrorAuth as resolveNpmDistTagMirrorAuthBase } from "./lib/npm-publish-plan.mjs";
910
import { readPositiveEnvInt } from "./lib/numeric-options.mjs";
1011
import {
@@ -575,16 +576,20 @@ export function parseNpmPackJsonOutput(stdout: string): NpmPackResult[] | null {
575576
}
576577

577578
const candidates = [trimmed];
578-
const trailingArrayStart = trimmed.lastIndexOf("\n[");
579-
if (trailingArrayStart !== -1) {
580-
candidates.push(trimmed.slice(trailingArrayStart + 1).trim());
579+
const trailingJsonStart = Math.max(trimmed.lastIndexOf("\n["), trimmed.lastIndexOf("\n{"));
580+
if (trailingJsonStart !== -1) {
581+
candidates.push(trimmed.slice(trailingJsonStart + 1).trim());
581582
}
582583

583584
for (const candidate of candidates) {
584585
try {
585586
const parsed = JSON.parse(candidate) as unknown;
586-
if (Array.isArray(parsed)) {
587-
return parsed as NpmPackResult[];
587+
const entries = resolveNpmJsonEntries(parsed).filter(
588+
(entry): entry is NpmPackResult =>
589+
Boolean(entry) && typeof entry === "object" && !Array.isArray(entry),
590+
);
591+
if (entries.length > 0) {
592+
return entries;
588593
}
589594
} catch {
590595
// Try the next candidate. npm lifecycle output can prepend non-JSON logs.

‎scripts/package-openclaw-for-docker.mjs‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import path from "node:path";
99
import { fileURLToPath, pathToFileURL } from "node:url";
1010
import { DOCKER_SELECTED_PLUGIN_BUILD_IDS_ENV } from "./lib/bundled-plugin-build-entries.mjs";
1111
import { terminateManagedChild } from "./lib/managed-child-process.mjs";
12+
import { resolveNpmJsonEntries } from "./lib/npm-json-output.mjs";
1213
import { resolveNpmRunner } from "./npm-runner.mjs";
1314
import { preparePackageChangelog, restorePackageChangelog } from "./package-changelog.mjs";
1415
import { resolvePnpmRunner } from "./pnpm-runner.mjs";
@@ -416,15 +417,13 @@ async function newestOpenClawTarball(outputDir, packOutput) {
416417
let fromOutput = "";
417418
try {
418419
const parsed = JSON.parse(packOutput);
419-
if (Array.isArray(parsed)) {
420-
for (const entry of parsed) {
421-
if (typeof entry?.filename !== "string") {
422-
continue;
423-
}
424-
const filename = resolvePackedOpenClawFileName(entry.filename);
425-
if (filename) {
426-
fromOutput = filename;
427-
}
420+
for (const entry of resolveNpmJsonEntries(parsed)) {
421+
if (typeof entry?.filename !== "string") {
422+
continue;
423+
}
424+
const filename = resolvePackedOpenClawFileName(entry.filename);
425+
if (filename) {
426+
fromOutput = filename;
428427
}
429428
}
430429
} catch {}
@@ -465,18 +464,22 @@ async function writePackJson(packOutput, tarball, packJsonPath, sourceDir) {
465464
} catch (error) {
466465
throw new Error("npm pack --json output was not valid JSON", { cause: error });
467466
}
468-
if (!Array.isArray(parsed)) {
469-
throw new Error("npm pack --json output must be an array");
467+
const entries = resolveNpmJsonEntries(parsed);
468+
if (
469+
entries.length === 0 ||
470+
entries.some((entry) => !entry || typeof entry !== "object" || Array.isArray(entry))
471+
) {
472+
throw new Error("npm pack --json output did not contain package results");
470473
}
471474
const filename = path.basename(tarball);
472-
for (const entry of parsed) {
475+
for (const entry of entries) {
473476
if (entry && typeof entry === "object" && typeof entry.filename === "string") {
474477
entry.filename = filename;
475478
}
476479
}
477480
const target = path.resolve(sourceDir, packJsonPath);
478481
await fs.mkdir(path.dirname(target), { recursive: true });
479-
await fs.writeFile(target, `${JSON.stringify(parsed, null, 2)}\n`);
482+
await fs.writeFile(target, `${JSON.stringify(entries, null, 2)}\n`);
480483
}
481484

482485
async function cleanPackedOpenClawTarballs(outputDir) {

‎scripts/release-check.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
collectRootPackageExcludedExtensionDirs,
3232
listBundledPluginPackArtifacts,
3333
} from "./lib/bundled-plugin-build-entries.mjs";
34+
import { resolveNpmJsonEntries } from "./lib/npm-json-output.mjs";
3435
import { collectPackUnpackedSizeErrors as collectNpmPackUnpackedSizeErrors } from "./lib/npm-pack-budget.mjs";
3536
import { readPositiveEnvInt } from "./lib/numeric-options.mjs";
3637
import {
@@ -397,7 +398,7 @@ function runPackDry(): PackResult[] {
397398
stdio: ["ignore", "pipe", "pipe"],
398399
maxBuffer: 1024 * 1024 * 100,
399400
});
400-
return JSON.parse(raw) as PackResult[];
401+
return resolveNpmJsonEntries(JSON.parse(raw)) as PackResult[];
401402
}
402403

403404
function runPack(packDestination: string, cwd?: string): PackResult[] {
@@ -410,8 +411,7 @@ function runPack(packDestination: string, cwd?: string): PackResult[] {
410411
maxBuffer: 1024 * 1024 * 100,
411412
},
412413
);
413-
const parsed = JSON.parse(raw) as PackResult | PackResult[];
414-
return Array.isArray(parsed) ? parsed : [parsed];
414+
return resolveNpmJsonEntries(JSON.parse(raw)) as PackResult[];
415415
}
416416

417417
export function resolvePackedTarballPath(packDestination: string, results: PackResult[]): string {

0 commit comments

Comments
 (0)