fix: allowBuilds 只写裸包名,清除所有 dsh-purge@* 键,避免 pnpm 11 崩溃(#185)

- pnpm 的 allowBuilds 按包名匹配,裸包名 dsh-purge 对 tarball / git /
  registry 三种安装形态都放行;而 dsh-purge@https://... 这类键在 pnpm 11
  (DSH 内置 11.2.2)会被 semver 解析器当成版本联合,直接报
  [ERR_PNPM_INVALID_VERSION_UNION] Invalid versions union 让 install 崩掉
- purgePnpmBuildSpecs() 只返回 ["dsh-purge"],不再拼 tarball URL 键
- 自愈时所有 dsh-purge@* 历史键(URL 键、旧截断键 dsh-purge@https、
  @1.1.81 之类版本键)一律丢弃,只保留裸包名;非 purge 键原样保留
- 删除随之失效的 readPurgeTarballFromLock() / readPkgTarball()
- 新增用例:输入只含 URL 键时重写后只剩裸包名;健康文件重写输出等于输入,
  零写入

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
叫我小杨同学andClaude committed 2026-10-30 12:25:32 +08:00
1 parent 6345f22cd5
commit f41ecc238c
2 files changed
+46 -96

No files matched your search

+31 -28
View File
@@ -86,7 +86,7 @@ test("污染的 allowBuilds 重写后只剩期望键", () => {
].join("\n");
const next = rewritePnpmAllowBuilds(polluted);
const keys = next.split("\n").filter((line) => parseAllowBuildLine(line)).map((line) => parseAllowBuildLine(line).key);
// 期望键:裸包名 + package.json 的 tarball(本仓库装的是 master tarball,无 profileDir 时)
// 期望键:只有裸包名(#185,不再写 URL 键)
const wanted = ["cpu-features", "ssh2", ...purgePnpmBuildSpecs()];
assert.deepEqual([...new Set(keys)], wanted);
// 畸形历史键一个都不留
@@ -122,7 +122,10 @@ test("写前校验:重复顶层键 / 重复 allowBuilds 键 / 解析不了的
assert.equal(validateAllowBuildRewrite("allowBuilds:\n# 注释行放行\n ssh2: true\n"), true);
});
test("pnpm-lock.yaml 里的 commit URL 优先于 master", () => {
test("期望键只有裸包名,不含任何 URL 键(#185)", () => {
// pnpm 11 遇到 `dsh-purge@https://...` 会报 ERR_PNPM_INVALID_VERSION_UNION,
// allowBuilds 按包名匹配即可,tarball / git / registry 安装都放行
assert.deepEqual(purgePnpmBuildSpecs(), ["dsh-purge"]);
const dir = withTempProfile([
"lockfileVersion: '9.0'",
"",
@@ -135,29 +138,25 @@ test("pnpm-lock.yaml 里的 commit URL 优先于 master", () => {
` version: dsh-purge@${COMMIT_URL}`,
"",
].join("\n"));
const specs = purgePnpmBuildSpecs(dir);
assert.deepEqual(specs, ["dsh-purge", `dsh-purge@${COMMIT_URL}`]);
assert.deepEqual(purgePnpmBuildSpecs(dir), ["dsh-purge"]);
});
test("lock 读不到(旧 master 记录 / 无文件)退回 package.json tarball", () => {
// 无 pnpm-lock.yaml 的临时目录 → 只用裸包名兜底逻辑应落到 package.json 的 master URL
const dir = withTempProfile();
assert.deepEqual(purgePnpmBuildSpecs(dir), ["dsh-purge", `dsh-purge@${MASTER_URL}`]);
// lock 里 specifier 写死 master,也如实保留
const dir2 = withTempProfile([
"importers:",
test("输入只含 URL 键时重写后只剩裸包名", () => {
const onlyUrl = [
"allowBuilds:",
` "dsh-purge@${MASTER_URL}": true`,
` dsh-purge@${MASTER_URL}: true`,
"",
" .:",
" dependencies:",
" dsh-purge:",
" specifier: https://codeload.github.com/YuJunZhiXue/dsh-purge/tar.gz/refs/heads/master",
" version: '1.1.81'",
"",
].join("\n"));
assert.deepEqual(purgePnpmBuildSpecs(dir2), ["dsh-purge", `dsh-purge@${MASTER_URL}`]);
].join("\n");
const next = rewritePnpmAllowBuilds(onlyUrl);
const keys = next.split("\n").filter((line) => parseAllowBuildLine(line)).map((line) => parseAllowBuildLine(line).key);
assert.deepEqual(keys, ["dsh-purge"]);
assert.equal(next.includes("dsh-purge@"), false);
assert.equal(next.split("\n").some((line) => line === " dsh-purge: true"), true);
assert.equal(validateAllowBuildRewrite(next), true);
});
test("固定 commit 安装:重写后过期 master 键被换成 commit 键", () => {
test("固定 commit 安装:重写后只剩裸包名,历史 URL / 畸形键全清", () => {
const dir = withTempProfile([
"lockfileVersion: '9.0'",
"",
@@ -177,17 +176,19 @@ test("固定 commit 安装:重写后过期 master 键被换成 commit 键", ()
"allowBuilds:",
" ssh2: true",
` dsh-purge@${MASTER_URL}: true`,
` "dsh-purge@${COMMIT_URL}": true`,
" dsh-purge@https: true",
"",
].join("\n");
const next = rewritePnpmAllowBuilds(polluted, dir);
const keys = next.split("\n").filter((line) => parseAllowBuildLine(line)).map((line) => parseAllowBuildLine(line).key);
assert.equal(keys.includes(`dsh-purge@${COMMIT_URL}`), true);
assert.equal(keys.includes("dsh-purge"), true);
assert.equal(keys.includes(`dsh-purge@${COMMIT_URL}`), false);
assert.equal(keys.includes(`dsh-purge@${MASTER_URL}`), false);
assert.equal(keys.includes("dsh-purge@https"), false);
assert.equal(keys.includes("ssh2"), true);
// URL 键写回带引号,可被 yaml 解析方安全读取
assert.equal(next.split("\n").some((line) => line === ` "dsh-purge@${COMMIT_URL}": true`), true);
// 只写裸包名,不带引号
assert.equal(next.split("\n").some((line) => line === " dsh-purge: true"), true);
});
test("幂等:健康文件重写输出等于输入,零写入", () => {
@@ -200,8 +201,7 @@ test("幂等:健康文件重写输出等于输入,零写入", () => {
"allowBuilds:",
" cpu-features: true",
" ssh2: true",
' dsh-purge: true',
` "dsh-purge@${MASTER_URL}": true`,
" dsh-purge: true",
"",
].join("\n");
fs.writeFileSync(fp, healthy, "utf8");
@@ -223,10 +223,13 @@ test("幂等:第二次 ensure 不再写盘(自愈后收敛)", () => {
const first = ensurePnpmAllowBuild(dir);
assert.equal(first.wrote, true);
const healed = fs.readFileSync(fp, "utf8");
// 畸形历史键被清掉(URL 键带引号重写,不会留下这些裸键)
assert.equal(healed.includes("dsh-purge@https: true"), false);
// 畸形历史键被清掉
assert.equal(healed.includes("dsh-purge@https"), false);
assert.equal(healed.includes("dsh-purge@1.1.81"), false);
// 只剩裸包名
assert.equal(healed.includes(" dsh-purge: true"), true);
const second = ensurePnpmAllowBuild(dir);
assert.equal(second.wrote, false);
assert.equal(fs.readFileSync(fp, "utf8"), healed);
});
});