Semver check: baseline is the newest older tag that is an ancestor of the commit; first release refused when the changelog lists an earlier version; declaration checked whole

The previous release is chosen by version order among same-prefix tags strictly older than the version, and must be an ancestor of HEAD, so a later backport on another branch never moves a rebuild's predecessor. A release with no previous tag is refused when the changelog already lists an earlier version, so a mistyped prefix cannot make an unmeasured first release. The declaration is validated as a whole: two packages sharing a prefix, or an entry without a string tagPrefix and changelog, refuse.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-03 05:55:12 +02:00
commit 3a25a9d3aa
2 changed files with 167 additions and 11 deletions

View file

@ -168,11 +168,18 @@ export function evaluateRelease({ packageName, changelogText, version, previousV
// changelog, then delegates to the pure functions above. ---
// The release record, derived from `git tag -l` in --cwd. The previous release is the
// newest '<prefix>-vMAJOR.MINOR.PATCH' tag strictly older than the version; the version's own
// tag is ignored, so a rebuild at a tagged version gets its release-time verdict. A same-prefix
// tag that does not parse is never skipped. A first release is the absence of any older tag in
// a checkout that holds other tags, because a checkout with none cannot tell a first release
// from a missing record.
// newest '<prefix>-vMAJOR.MINOR.PATCH' tag strictly older than the version, by version order
// (never git's string order), that is also an ancestor of the commit being checked (HEAD of
// --cwd): a backport tagged later on another branch is not in this commit's history, so it
// never changes a rebuild's predecessor. The version's own tag is ignored, so a rebuild at a
// tagged version gets its release-time verdict. A same-prefix tag that does not parse is never
// skipped. A first release is the absence of any older tag in a checkout that holds other tags,
// because a checkout with none cannot tell a first release from a missing record.
function isAncestorOfHead(tag, cwd) {
const run = spawnSync("git", ["merge-base", "--is-ancestor", `refs/tags/${tag}`, "HEAD"], { cwd, stdio: "ignore" });
return run.status === 0 ? true : run.status === 1 ? false : null;
}
function resolveRelease(tagPrefix, versionText, cwd) {
let tags;
try {
@ -186,7 +193,7 @@ function resolveRelease(tagPrefix, versionText, cwd) {
const prefix = `${tagPrefix}-v`;
const current = parseVersion(versionText);
const own = `${prefix}${formatVersion(current)}`;
let best = null;
const older = [];
let newer = null;
for (const tag of tags) {
if (!tag.startsWith(prefix)) continue;
@ -199,11 +206,19 @@ function resolveRelease(tagPrefix, versionText, cwd) {
if (order === 0) continue; // the tag being released itself
if (order > 0) {
newer ??= tag;
} else if (best === null || compareVersions(candidate, best) > 0) {
best = candidate;
} else {
older.push({ tag, version: candidate });
}
}
if (best !== null) return { previous: formatVersion(best) };
older.sort((a, b) => compareVersions(b.version, a.version));
for (const { tag, version } of older) {
const ancestor = isAncestorOfHead(tag, cwd);
if (ancestor === null) return { failure: `whether ${tag} is an ancestor of the commit being checked could not be read` };
if (ancestor) return { previous: formatVersion(version) };
}
if (older.length > 0) {
return { failure: `${older.length} older ${prefix} tag(s) exist but none is an ancestor of the commit being checked, so the previous release is unknown` };
}
if (newer !== null) {
return { failure: `${newer} exists but no ${prefix} tag is older than ${formatVersion(current)}: the version is older than every release` };
}
@ -213,6 +228,34 @@ function resolveRelease(tagPrefix, versionText, cwd) {
return { previous: null };
}
// The newest `## [x.y.z]` entry of the changelog older than the version, or null. A release with
// no previous tag whose changelog already lists an earlier version is not a first release: the
// declared tag prefix names none of the package's releases.
function earlierChangelogVersion(changelogText, versionText) {
const current = parseVersion(versionText);
let newest = null;
for (const match of changelogText.matchAll(/^##\s*\[?(\d+\.\d+\.\d+)\]?\s*$/gm)) {
const entry = parseVersion(match[1]);
if (compareVersions(entry, current) < 0 && (newest === null || compareVersions(entry, newest) > 0)) newest = entry;
}
return newest === null ? null : formatVersion(newest);
}
// The declaration as a whole: every entry names a string tagPrefix and changelog, and no two
// packages share a prefix (each would be measured against the other's releases). Returns a
// refusal text, or null.
export function checkDeclaration(declared) {
const seen = new Map();
for (const [name, entry] of Object.entries(declared ?? {})) {
if (entry == null || typeof entry.tagPrefix !== "string" || typeof entry.changelog !== "string") {
return `${name} must declare a string tagPrefix and a string changelog`;
}
if (seen.has(entry.tagPrefix)) return `${seen.get(entry.tagPrefix)} and ${name} share one tag prefix`;
seen.set(entry.tagPrefix, name);
}
return null;
}
// --- Measured public API: the built DLLs are compared with the DLLs the
// previous tag tracked, by Microsoft.DotNet.ApiCompat.Tool. The tool, and the
// netstandard reference assemblies it needs to resolve inherited members, are
@ -412,8 +455,13 @@ async function main() {
refuse(`${packageName}: scripts/release-packages.mjs could not be loaded from --cwd`);
return;
}
const malformed = checkDeclaration(declared);
if (malformed) {
refuse(`${packageName}: scripts/release-packages.mjs is malformed: ${malformed}`);
return;
}
const entry = Object.hasOwn(declared ?? {}, packageName) ? declared[packageName] : null;
if (entry == null || typeof entry.tagPrefix !== "string" || typeof entry.changelog !== "string") {
if (entry == null) {
refuse(`${packageName}: not a package declared in scripts/release-packages.mjs`);
return;
}
@ -440,6 +488,11 @@ async function main() {
return;
}
const previousVersion = release.previous;
const earlier = previousVersion == null ? earlierChangelogVersion(changelogText, version) : null;
if (earlier !== null) {
refuse(`${packageName}: ${version} would be a first release, but the changelog already lists ${earlier} and no ${tagPrefix}-v tag is an earlier release: the declared tag prefix is wrong`);
return;
}
let measuredBreaks = [];
if (previousVersion != null && built.length > 0) {
const measurement = measureApiBreaks({

View file

@ -8,6 +8,7 @@ import { fileURLToPath } from "node:url";
import { test } from "node:test";
import {
BREAKING_HEADING,
checkDeclaration,
classifyBump,
evaluateRelease,
hasBreakingSection,
@ -243,7 +244,7 @@ function runCheckerExpectFailure(cwd, args, { status = 1, ...options } = {}) {
const git = (dir, ...args) => execFileSync("git", args, { cwd: dir });
const commitAndTag = (dir, ...tags) => {
git(dir, "commit", "--allow-empty", "-q", "-m", "init");
git(dir, "commit", "--allow-empty", "-q", "-m", `init ${tags.join(" ")}`);
for (const tag of tags) git(dir, "tag", tag);
};
const writeChangelog = (dir, version, options) => writeFileSync(join(dir, "CHANGELOG.md"), changelogFor(version, options));
@ -348,6 +349,108 @@ test("CLI: a version older than every release refuses", () => {
});
});
test("CLI: the previous release is the newest older tag by version order, not git's string order", () => {
withTempGitRepo((dir) => {
// git tag -l lists these as 1.0.0, 1.0.5, 1.10.0, 1.2.0, 1.9.0, 2.0.0: 1.10.0 sorts before 1.2.0
commitAndTag(dir, "widget-v1.0.0", "widget-v1.2.0", "widget-v1.10.0", "widget-v1.9.0", "widget-v2.0.0", "other-v9.9.9");
git(dir, "tag", "-a", "-m", "annotated", "widget-v1.0.5");
const previousOf = (version) => {
writeChangelog(dir, version);
return runChecker(dir, widgetArgs(version));
};
assert.match(previousOf("1.10.1"), /1\.10\.1 is a patch bump over 1\.10\.0/);
assert.match(previousOf("1.11.0"), /1\.11\.0 is a minor bump over 1\.10\.0/);
assert.match(previousOf("1.3.0"), /1\.3\.0 is a minor bump over 1\.2\.0/);
assert.match(previousOf("1.0.6"), /1\.0\.6 is a patch bump over 1\.0\.5/);
assert.match(previousOf("2.0.1"), /2\.0\.1 is a patch bump over 2\.0\.0/);
assert.match(previousOf("3.0.0"), /3\.0\.0 is a major bump over 2\.0\.0/);
});
});
test("CLI: a later backport tag on another branch does not change a rebuild's predecessor", () => {
const head = (dir) => git(dir, "rev-parse", "HEAD").toString().trim();
withTempGitRepo((dir) => {
commitAndTag(dir, "widget-v0.2.0");
const older = head(dir);
commitAndTag(dir, "widget-v0.3.0");
const rebuilt = head(dir);
writeChangelog(dir, "0.3.0");
const atRelease = runChecker(dir, widgetArgs("0.3.0"));
assert.match(atRelease, /0\.3\.0 is a minor bump over 0\.2\.0/);
// the backport is cut from the older release, later, and is never merged
git(dir, "checkout", "-q", older);
commitAndTag(dir, "widget-v0.2.5");
const backport = head(dir);
git(dir, "checkout", "-q", rebuilt);
assert.equal(runChecker(dir, widgetArgs("0.3.0")), atRelease);
// history that does contain the backport still measures against it
git(dir, "checkout", "-q", backport);
writeChangelog(dir, "0.2.6");
assert.match(runChecker(dir, widgetArgs("0.2.6")), /0\.2\.6 is a patch bump over 0\.2\.5/);
});
// older tags that are all outside the history leave the predecessor unknown
withTempGitRepo((dir) => {
commitAndTag(dir, "other-v1.0.0");
const root = head(dir);
git(dir, "checkout", "-q", "--detach");
commitAndTag(dir, "widget-v0.1.0");
git(dir, "checkout", "-q", root);
writeChangelog(dir, "0.2.0");
assert.match(runCheckerExpectFailure(dir, widgetArgs("0.2.0")), /none is an ancestor of the commit being checked/);
});
});
test("CLI: a declared prefix that names no release is not a first release when the changelog lists earlier versions", () => {
// CultMath's shape: 0.3.0 is released and tracks the DLL; 0.3.1 removes a member.
const cultMath = buildAssembly("CultMath", WITH_GONE);
const trimmed = buildAssembly("CultMath", WITHOUT_GONE);
withReleasedBaseline({ baseline: { "CultMath.dll": cultMath }, previous: "0.3.0" }, (dir) => {
const entry = { ...WIDGET, assemblies: PLUGINS };
const args = measuredArgs(dir, "0.3.1", [trimmed]);
writeFileSync(join(dir, "CHANGELOG.md"), `${changelogFor("0.3.1")}\n## [0.3.0]\n\n- the release\n`);
assert.match(runCheckerExpectFailure(dir, args), /Gone/);
// one mistyped character in the declaration
declare(dir, { widget: { ...entry, tagPrefix: "widgt" } });
const typo = runCheckerExpectFailure(dir, args);
assert.match(typo, /0\.3\.1 would be a first release, but the changelog already lists 0\.3\.0/);
assert.doesNotMatch(typo, /first release \(/);
// a release the changelog lists nothing before is still a first release
writeChangelog(dir, "0.3.1");
assert.match(runChecker(dir, args), /first release \(0\.3\.1\)/);
});
});
test("CLI: two packages that share a tag prefix are refused, and so is an entry that is not whole", () => {
withTempGitRepo((dir) => {
commitAndTag(dir, "widget-v1.0.0");
writeChangelog(dir, "1.0.1");
declare(dir, { widget: WIDGET, gadget: { ...WIDGET } });
for (const name of ["widget", "gadget"]) {
const output = runCheckerExpectFailure(dir, ["--package", name, "--version", "1.0.1"]);
assert.match(output, /scripts\/release-packages\.mjs is malformed: widget and gadget share one tag prefix/);
}
// distinct prefixes pass
declare(dir, { widget: WIDGET, gadget: { ...WIDGET, tagPrefix: "gadget" } });
assert.match(runChecker(dir, widgetArgs("1.0.1")), /patch bump over 1\.0\.0/);
for (const broken of [
{ changelog: "CHANGELOG.md" },
{ tagPrefix: 7, changelog: "CHANGELOG.md" },
{ tagPrefix: "widget" },
{ tagPrefix: "widget", changelog: ["CHANGELOG.md"] },
null,
]) {
declare(dir, { widget: WIDGET, gadget: broken });
const output = runCheckerExpectFailure(dir, widgetArgs("1.0.1"));
assert.match(output, /scripts\/release-packages\.mjs is malformed: gadget must declare a string tagPrefix and a string changelog/);
}
});
});
test("the real declaration is whole and shares no tag prefix", () => {
assert.equal(checkDeclaration(releasePackages), null);
assert.equal(new Set(Object.values(releasePackages).map((entry) => entry.tagPrefix)).size, Object.keys(releasePackages).length);
});
test("CLI: the tag prefix is the declaration's, not a spelling the caller or the directory supplies", () => {
withTempGitRepo((dir) => {
declare(dir, { widget: { tagPrefix: "widget-unity", changelog: "widget/CHANGELOG.md" } });