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 0c933dc7d3
commit 97e4e6094d
2 files changed
+46 -96

No files matched your search

+15 -68
View File
@@ -105,73 +105,18 @@ export function ensureProfileBundles(profileDir) {
return { ok: true, wrote: true, path: fp };
}
function readPkgTarball() {
try {
const pkg = JSON.parse(fs.readFileSync(new URL("../package.json", import.meta.url), "utf8"));
return String(pkg?.dsh?.install?.tarball || "").trim();
} catch {
return "https://codeload.github.com/YuJunZhiXue/dsh-purge/tar.gz/refs/heads/master";
}
}
/**
* 从 profile 的 pnpm-lock.yaml 读 dsh-purge 实际解析到的 tarball URL(#180)。
* 用户用 `github:user/repo#<commit>` 固定 commit 安装时,lock 里记录的是
* `.../tar.gz/<commit>`,写死 master URL 会和实际键并存成两个 URL 键。
* 简单文本/正则解析即可,不引 yaml 依赖;读不到返回空串。
* pnpm allowBuilds 期望键(#185)。
*
* 只写裸包名 `dsh-purge`:pnpm 的 allowBuilds 按包名匹配,tarball / git / registry
* 三种安装形态都放行。而 `dsh-purge@https://...` 这类键在 pnpm 11(DSH 内置
* 11.2.2)会被 semver 解析器当成版本联合,直接报
* `[ERR_PNPM_INVALID_VERSION_UNION] Invalid versions union` 让 install 崩掉。
*
* profileDir 参数只为兼容旧调用保留,实际不再需要(#185)。
*/
function readPurgeTarballFromLock(profileDir) {
if (!profileDir) return "";
let text = "";
try {
text = fs.readFileSync(path.join(profileDir, "pnpm-lock.yaml"), "utf8");
} catch {
return "";
}
const lines = String(text || "").replace(/\r\n/g, "\n").split("\n");
let inImporters = false;
for (let i = 0; i < lines.length; i += 1) {
const line = lines[i];
if (/^\S/.test(line)) {
if (inImporters) break; // 已扫过 importers,遇到下一个顶层键就结束
inImporters = /^importers[ \t]*:/.test(line);
continue;
}
if (!inImporters) continue;
const entry = line.match(/^([ \t]+)dsh-purge[ \t]*:[ \t]*$/);
if (!entry) continue;
const indent = entry[1].length;
// 收集该条目下的 specifier / version 行,直到缩进回到条目本身
let specifier = "";
let version = "";
for (let j = i + 1; j < lines.length; j += 1) {
const sub = lines[j];
if (sub.trim() && (sub.match(/^[ \t]*/) || [""])[0].length <= indent) break;
const sm = sub.match(/^[ \t]+(specifier|version)[ \t]*:[ \t]*(.*)$/);
if (!sm) continue;
let value = String(sm[2] || "").trim();
// 掉引号:pnpm-lock 里带引号与否取决于内容
if (/^(["']).*\1[ \t]*$/.test(value)) value = value.slice(1, -1).trim();
if (sm[1] === "specifier") specifier = value;
else version = value;
}
// version 是实际解析结果,优先;specifier 只是没有 URL 化版本时兜底
for (const candidate of [version, specifier]) {
const url = String(candidate || "").match(/https?:\/\/[^\s'"]+/);
if (url) return url[0];
}
return "";
}
return "";
}
/** pnpm 12 会给 tarball 单独写一行 `dsh-purge@https://...: set this to true or false`。只写包名不够。 */
export function purgePnpmBuildSpecs(profileDir) {
const specs = ["dsh-purge"];
// 期望键和实际安装形态一致:lock 里解析到的 URL > package.json 的 tarball > 只用裸包名
const tarball = readPurgeTarballFromLock(profileDir) || readPkgTarball();
if (tarball) specs.push(`${PURGE_NAME}@${tarball}`);
return specs;
return [PURGE_NAME];
}
function isPurgeAllowBuildKey(key) {
@@ -251,10 +196,12 @@ export function validateAllowBuildRewrite(text) {
}
/**
* 把 dsh-purge 以及 `dsh-purge@tarball` 都写成 true,避免装别的插件还报 ERR_PNPM_IGNORED_BUILDS(#168)。
* profileDir 可选:给了就从该 profile 的 pnpm-lock.yaml 读实际 tarball URL(#180)。
* 期望键精确命中的才保留;其它一切 `dsh-purge@*` 历史键(畸形 `dsh-purge@https`、过期版本/commit URL)一律丢弃,
* 让旧的污染文件在下一次 ensure 时自动清干净。
* 把 dsh-purge 写成 true,避免装别的插件还报 ERR_PNPM_IGNORED_BUILDS(#168)。
*
* #185:只写裸包名。`dsh-purge@https://...` 这类键在 pnpm 11 会被当成版本联合,
* 直接触发 ERR_PNPM_INVALID_VERSION_UNION 让 install 崩掉。
* 自愈时所有 `dsh-purge@*` 历史键(URL 键、旧截断键 `dsh-purge@https`、
* `@1.1.81` 之类版本键)一律丢弃,只保留裸包名;非 purge 键原样保留。
*/
export function rewritePnpmAllowBuilds(raw, profileDir) {
const wanted = purgePnpmBuildSpecs(profileDir);
+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);
});
});