ci: a package's shipped files don't change under a version that is out (#1131)

A version is packed once (dist keeps its first tarball, npm won't take it
twice, and magpie updates by version), so a change shipped without a bump
reaches no one. #1095 did that to astudio 0.2.2.

scripts/bumped.mjs <base> diffs packages/ against base and names each
package whose packed files (npm pack --dry-run) changed with its version
the same. README.md is left out: magpie reads it from dist's index.json,
rewritten every run. bumped.yml runs it on each pull request (base sha)
and each push to main (event.before).

Run: bun test scripts/bumped.test.mjs, 3 pass (mica). Broken by hand:
with astudio set back to 0.2.2, `bun scripts/bumped.mjs a2af9d2d~1`
fails naming packages/astudio@0.2.2: index.mjs; at 0.2.3 it passes.
Not tried: the workflow itself, until this push runs it.
This commit is contained in:
yetone
2026-11-14 15:15:29 +08:00
parent 288310b6ac
commit 2e4403d1fb
3 changed files with 112 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
# A package whose shipped files change keeps nothing of the change unless
# its version changes too (scripts/bumped.mjs, plugins#1131). Runs on each
# pull request and on each push to main, against what it is based on.
name: bumped
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
jobs:
bumped:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: oven-sh/setup-bun@v2
- uses: actions/setup-node@v4
with:
node-version: 24
- name: versions raised
run: bun scripts/bumped.mjs "${{ github.event.pull_request.base.sha || github.event.before }}"
+68
View File
@@ -0,0 +1,68 @@
// Fails when a package's shipped files changed since <base> while its
// version stayed the same.
// bun scripts/bumped.mjs <base-ref>
// A version is packed once: the dist branch keeps the first tarball made of
// it (scripts/dist.mjs), npm refuses to publish it twice, and magpie's
// update compares versions only. So a change shipped under a version that
// is already out never reaches anyone who has that version, and new
// installs from dist get the old code under the new number (plugins#1131:
// astudio 0.2.2's DeepSeek-V4.1-Flash fill-in went out without a bump).
// README.md is left out: magpie reads it from dist's index.json, which is
// written again on every run. Files npm doesn't pack (tests) don't count.
import { execFileSync } from "node:child_process"
import { existsSync, readFileSync } from "node:fs"
import { join, resolve } from "node:path"
// unbumped lists "<dir>@<version>: files…" for each package whose shipped
// files changed with its version the same. changed: repo paths changed
// since base. versionAt(dir, "base"|"head"): the version, or null when
// the package isn't there. packed(dir): the paths npm packs, relative to
// dir.
export function unbumped(changed, versionAt, packed) {
const byDir = new Map()
for (const p of changed) {
const m = /^(packages\/[^/]+)\/(.+)$/.exec(p)
if (!m) continue
if (!byDir.has(m[1])) byDir.set(m[1], [])
byDir.get(m[1]).push(m[2])
}
const out = []
for (const [dir, files] of byDir) {
const head = versionAt(dir, "head")
const base = versionAt(dir, "base")
if (head === null || base === null || head !== base) continue
const ships = new Set(packed(dir))
const hit = files.filter((f) => f !== "README.md" && ships.has(f)).sort()
if (hit.length) out.push(`${dir}@${head}: ${hit.join(", ")}`)
}
return out.sort()
}
if (import.meta.main) {
const base = process.argv[2]
if (!base || /^0+$/.test(base)) {
console.log("no base to compare with")
process.exit(0)
}
const root = resolve(import.meta.dir, "..")
const git = (...a) => execFileSync("git", a, { cwd: root, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] })
const changed = git("diff", "--name-only", base, "HEAD", "--", "packages").split("\n").filter(Boolean)
const versionAt = (dir, at) => {
try {
const src = at === "head" ? (existsSync(join(root, dir, "package.json")) ? readFileSync(join(root, dir, "package.json"), "utf8") : null) : git("show", `${base}:${dir}/package.json`)
return src === null ? null : JSON.parse(src).version ?? null
} catch {
return null
}
}
const packed = (dir) => {
const out = execFileSync("npm", ["pack", "--dry-run", "--json", "--ignore-scripts"], { cwd: join(root, dir), encoding: "utf8" })
return JSON.parse(out)[0].files.map((f) => f.path)
}
const problems = unbumped(changed, versionAt, packed)
if (problems.length) {
for (const p of problems) console.log(`::error::${p} changed without a version bump; raise the version in its package.json`)
process.exit(1)
}
console.log(`every package that changed since ${base} has a new version`)
}
+18
View File
@@ -0,0 +1,18 @@
import { expect, test } from "bun:test"
import { unbumped } from "./bumped.mjs"
const versions = { "packages/a": ["0.1.0", "0.1.0"], "packages/b": ["0.1.0", "0.1.1"], "packages/new": [null, "0.1.0"], "packages/gone": ["0.1.0", null] }
const versionAt = (dir, at) => (versions[dir] ?? [null, null])[at === "base" ? 0 : 1]
const packed = () => ["index.mjs", "opencode2.mjs", "package.json", "README.md"]
test("a shipped file changed under the same version is named (plugins#1131)", () => {
expect(unbumped(["packages/a/index.mjs", "packages/a/opencode2.mjs"], versionAt, packed)).toEqual(["packages/a@0.1.0: index.mjs, opencode2.mjs"])
})
test("a bumped, new or removed package passes", () => {
expect(unbumped(["packages/b/index.mjs", "packages/new/index.mjs", "packages/gone/index.mjs"], versionAt, packed)).toEqual([])
})
test("README.md, tests and files outside packages need no bump", () => {
expect(unbumped(["packages/a/README.md", "packages/a/index.test.mjs", "scripts/check.mjs", "registry.json"], versionAt, packed)).toEqual([])
})