From c061373edeaea5e7cd3b9a0550ad0f35f5fcfc07 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Tue, 23 Jun 2026 10:31:05 +0200 Subject: [PATCH] fix(release): track CommonJS package dist imports --- scripts/lib/package-dist-imports.mjs | 6 ++++++ .../check-openclaw-package-tarball.test.ts | 17 +++++++++++++++++ test/scripts/check-package-dist-imports.test.ts | 15 +++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/scripts/lib/package-dist-imports.mjs b/scripts/lib/package-dist-imports.mjs index 9adb52e5e014..b8caf4d02dac 100644 --- a/scripts/lib/package-dist-imports.mjs +++ b/scripts/lib/package-dist-imports.mjs @@ -50,6 +50,11 @@ function isImportSpecifierContext(source, index) { ); } +function isRequireSpecifierContext(source, index) { + const prefix = source.slice(Math.max(0, index - 32), index); + return /\brequire\s*\(\s*$/u.test(prefix); +} + function isImportMetaUrlContext(source, quoteStart, quoteEnd) { const prefix = source.slice(Math.max(0, quoteStart - 32), quoteStart); if (!/\bnew\s+URL\s*\(\s*$/u.test(prefix)) { @@ -115,6 +120,7 @@ function collectImportSpecifiers(source) { if (value.startsWith(".")) { const isDistDependency = isImportSpecifierContext(source, index) || + isRequireSpecifierContext(source, index) || (isImportMetaUrlContext(source, index, cursor) && hasJavaScriptFileExtension(value)); if (isDistDependency) { specifiers.push(value); diff --git a/test/scripts/check-openclaw-package-tarball.test.ts b/test/scripts/check-openclaw-package-tarball.test.ts index d90a58ff84f7..5b7af19e07cf 100644 --- a/test/scripts/check-openclaw-package-tarball.test.ts +++ b/test/scripts/check-openclaw-package-tarball.test.ts @@ -271,6 +271,23 @@ describe("check-openclaw-package-tarball", () => { ); }); + it("rejects CommonJS require chunks omitted from the postinstall inventory", () => { + withTarball( + ["dist/index.cjs"], + { + "dist/index.cjs": 'module.exports = require("./chunk.cjs");\n', + "dist/chunk.cjs": "module.exports = {};\n", + }, + (tarball) => { + const result = spawnSync("node", [CHECK_SCRIPT, tarball], { encoding: "utf8" }); + + expect(result.status).not.toBe(0); + expect(result.stderr).toContain("inventory omits imported dist file dist/chunk.cjs"); + }, + "2026.4.27", + ); + }); + it("rejects dist files with missing import.meta.url URL dependencies", () => { withTarball( ["dist/index.js"], diff --git a/test/scripts/check-package-dist-imports.test.ts b/test/scripts/check-package-dist-imports.test.ts index 1e27ba4cac01..479c2aedfd36 100644 --- a/test/scripts/check-package-dist-imports.test.ts +++ b/test/scripts/check-package-dist-imports.test.ts @@ -46,4 +46,19 @@ describe("check-package-dist-imports", () => { expect(result.status, result.stderr).toBe(0); expect(result.stdout).toContain("OpenClaw package dist import closure passed."); }); + + it("rejects missing CommonJS require chunks", () => { + const root = makeTempDir(tempDirs, "openclaw-package-dist-imports-"); + mkdirSync(join(root, "dist"), { recursive: true }); + writeFileSync( + join(root, "dist", "index.cjs"), + 'module.exports = require("./chunk.cjs");\n', + "utf8", + ); + + const result = spawnSync("node", [CHECK_SCRIPT, root], { encoding: "utf8" }); + + expect(result.status).not.toBe(0); + expect(result.stderr).toContain("dist/index.cjs imports missing dist/chunk.cjs"); + }); });