feat: accept 3-digit semver + package.json version sources (#2501)

Two version-source shapes failed CLOSED in a way that silently disabled
/ship's queue-collision check:

1. A --version-path / .gstack/version-path target that is a package.json
   was read as raw text: the whitespace strip turned the JSON into
   '{"name":"frontend",... which parseVersion rejected, so every read —
   local, `git show`, and rival PRs' claims through the GitHub/GitLab
   Contents APIs — fell back to 0.0.0.0 and competing claims were dropped
   as "malformed".
2. parseVersion required exactly four components, so gstack-next-version
   exited 2 on EVERY invocation in a 3-digit repo. That CLI IS the
   queue-collision check; /ship then took its documented offline path of
   naive local arithmetic, two branches cut from the same base picked the
   same version, and git merged the duplicate without a conflict.

New lib/version-source.ts holds the shared semantics so both CLIs agree
by construction: parseVersion accepts 3- or 4-digit (3 pads the micro
slot for uniform comparison), versionWidth/fmtVersion keep a 3-digit repo
3-digit through bumping and formatting, micro coerces to patch on 3-digit
repos (with a warning in the output), and extractVersion reads a .json
version-path as JSON (.version) from any byte source. gstack-version-bump
treats a package.json version-path as that repo's single source of truth
(written in place, DRIFT_* states can't arise — no second file to drift
from). Detection is by shape, not new configuration.

Scope per the wave plan's version-tooling end-state spec (decision 11,
ENG-OV1): this is the READING capability + 3-digit acceptance ONLY.
gstack's own VERSION file stays the 4-digit source of truth; nothing here
flips authority to package.json. The PR's bundled fix for the
.gstack/version-path pin being ignored by classify's base read lands
separately (#2462) — these tests drive the JSON version-path through the
explicit --version-path flag.

Re-derived from PR #2501 by @YiftahR (73 tests pass across
test/gstack-version-bump.test.ts, test/gstack-next-version.test.ts,
test/ship-version-sync.test.ts).

Fixes #2501

Co-authored-by: YR <work.yiftah.rottem@gmail.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Garry Tan
2026-08-16 10:02:31 -07:00
co-authored by YR Claude Fable 5
parent c7faef885b
commit d7ab20ac06
5 changed files with 359 additions and 59 deletions
+58 -1
View File
@@ -13,6 +13,8 @@ import {
fmtVersion,
bumpVersion,
cmpVersion,
versionWidth,
extractVersion,
pickNextSlot,
markActiveSiblings,
resolveVersionPath,
@@ -29,8 +31,20 @@ describe("parseVersion", () => {
expect(parseVersion(" 1.2.3.4 \n")).toEqual([1, 2, 3, 4]);
});
test("accepts 3-digit semver, padding the micro slot (#2501)", () => {
// 3-digit repos (a package.json holding plain semver) used to fail parsing
// outright, which exited this CLI 2 on EVERY run — and since this CLI is
// the queue-collision check, /ship then fell back to naive local
// arithmetic and duplicate version slots shipped silently. The pad keeps
// comparison uniform; versionWidth narrows output back.
expect(parseVersion("0.99.2")).toEqual([0, 99, 2, 0]);
expect(parseVersion("1.2.3")).toEqual([1, 2, 3, 0]);
expect(versionWidth("0.99.2")).toBe(3);
expect(versionWidth("1.6.3.0")).toBe(4);
});
test("rejects malformed", () => {
expect(parseVersion("1.2.3")).toBeNull();
expect(parseVersion("1.2")).toBeNull();
expect(parseVersion("1.2.3.4.5")).toBeNull();
expect(parseVersion("v1.2.3.4")).toBeNull();
expect(parseVersion("")).toBeNull();
@@ -39,6 +53,49 @@ describe("parseVersion", () => {
});
});
describe("3-digit repos keep their width (#2501)", () => {
test("formatting narrows to the repo's own width", () => {
expect(fmtVersion([0, 99, 3, 0], 3)).toBe("0.99.3");
expect(fmtVersion([0, 99, 3, 0], 4)).toBe("0.99.3.0");
expect(fmtVersion([0, 99, 3, 0])).toBe("0.99.3.0"); // default stays 4-digit
});
test("micro is carried out as patch when there is no micro component", () => {
// /ship auto-picks MICRO by default. Erroring would make it unusable in
// every 3-digit repo; a no-op would be worse — it would write back the
// version it started with and claim a slot already taken.
expect(bumpVersion([0, 99, 2, 0], "micro", 3)).toEqual([0, 99, 3, 0]);
expect(bumpVersion([0, 99, 2, 0], "patch", 3)).toEqual([0, 99, 3, 0]);
expect(bumpVersion([0, 99, 2, 3], "micro", 4)).toEqual([0, 99, 2, 4]); // 4-digit unchanged
});
test("slot picking stays inside the repo's width", () => {
const { version } = pickNextSlot([0, 99, 2, 0], [[0, 99, 5, 0]], "patch", 3);
expect(fmtVersion(version, 3)).toBe("0.99.6");
});
});
describe("extractVersion (#2501)", () => {
test("reads .version when the version-path is a package.json", () => {
const pkg = JSON.stringify({ name: "frontend", version: "0.99.2", private: true });
expect(extractVersion(pkg, "frontend/package.json")).toBe("0.99.2");
expect(extractVersion(pkg, "deep/nested/package.json")).toBe("0.99.2");
});
test("reads raw text for a plain VERSION file", () => {
expect(extractVersion("1.6.3.0\n", "VERSION")).toBe("1.6.3.0");
expect(extractVersion(" 1.6.3.0 ", "version/CURRENT")).toBe("1.6.3.0");
});
test("a JSON path that isn't valid JSON yields empty, not garbage", () => {
// The old readers ran a package.json through a whitespace strip and handed
// the caller '{"name":"frontend",...' as if it were a version. Empty lets
// callers fall back loudly.
expect(extractVersion("{ not json", "package.json")).toBe("");
expect(extractVersion(JSON.stringify({ name: "x" }), "package.json")).toBe("");
});
});
describe("bumpVersion", () => {
test("major zeros everything right", () => {
expect(bumpVersion([1, 6, 3, 0], "major")).toEqual([2, 0, 0, 0]);