Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(desktop): coerce commit-hash versions to valid semver (MUL-3314) (#4183)
Bing2030dd9e7bf19d86
diff --git a/apps/desktop/scripts/package.mjs b/apps/desktop/scripts/package.mjsindex 886bbfe925..4dbd96a307 100644--- a/apps/desktop/scripts/package.mjs+++ b/apps/desktop/scripts/package.mjs@@ -98,16 +98,29 @@ export function stripLeadingSeparator(argv) {* - "v0.1.36" → "0.1.36"* - "v0.1.35-14-gf1415e96" → "0.1.35-14-gf1415e96" (semver prerelease)* - "v0.1.35-…-dirty" → same, dirty suffix preserved- * - "f1415e96" (no tag) → "0.0.0-f1415e96" (fallback)+ * - "f1415e96" (no tag) → "0.0.0-gf1415e96" (fallback)+ * - "2f24057b" (no tag, hash begins with a digit) → "0.0.0-g2f24057b"+ * - "0123456" (no tag, all-digit hash w/ leading zero) → "0.0.0-g0123456"** Leading `v` is stripped so the result is valid semver for package.json.+ * The fallback matters because a bare commit hash is never valid semver —+ * even one that happens to start with a digit (e.g. "2f24057b") — and+ * electron-updater throws on launch if package.json carries such a version.+ * The hash is prefixed with `g` so the pre-release identifier is always+ * alphanumeric; a bare all-digit hash with a leading zero (e.g. "0123456")+ * would otherwise form `0.0.0-0123456`, which is invalid semver.*/export function normalizeGitVersion(raw) {if (!raw) return null;const stripped = raw.replace(/^v/, "");- if (!/^\d/.test(stripped)) {- // No reachable tag — `git describe` fell back to just the commit hash.- return `0.0.0-${stripped}`;+ // A real version begins with major.minor.patch. The bare commit hash+ // that `git describe --always` falls back to (no reachable tag) does not,+ // so coerce it to a 0.0.0 prerelease rather than passing it through.+ // Prefix the hash with `g` (mirroring `git describe`'s own `g<hash>`+ // shorthand) so a hash like "0123456" yields "0.0.0-g0123456" — a single+ // alphanumeric identifier — instead of the invalid "0.0.0-0123456".+ if (!/^\d+\.\d+\.\d+/.test(stripped)) {+ return `0.0.0-g${stripped}`;}return stripped;}diff --git a/apps/desktop/scripts/package.test.mjs b/apps/desktop/scripts/package.test.mjsindex 274939e79f..d5e5c98f36 100644--- a/apps/desktop/scripts/package.test.mjs+++ b/apps/desktop/scripts/package.test.mjs@@ -38,11 +38,27 @@ describe("normalizeGitVersion", () => {expect(normalizeGitVersion("v1.0.0-rc.2")).toBe("1.0.0-rc.2");});- it("falls back to 0.0.0-<hash> when no tags are reachable", () => {+ it("falls back to 0.0.0-g<hash> when no tags are reachable", () => {// `git describe --tags --always` returns just the short commit hash- // when there are no tags in the history at all.- expect(normalizeGitVersion("f1415e96")).toBe("0.0.0-f1415e96");- expect(normalizeGitVersion("abc1234")).toBe("0.0.0-abc1234");+ // when there are no tags in the history at all. A hash that begins with+ // a digit (e.g. "2f24057b") is still not valid semver and must fall+ // through — otherwise electron-updater rejects it on launch. The `g`+ // prefix mirrors git describe's own `g<hash>` shorthand and keeps the+ // pre-release identifier a single alphanumeric token.+ expect(normalizeGitVersion("f1415e96")).toBe("0.0.0-gf1415e96");+ expect(normalizeGitVersion("abc1234")).toBe("0.0.0-gabc1234");+ expect(normalizeGitVersion("2f24057b")).toBe("0.0.0-g2f24057b");+ });++ it("prefixes an all-digit hash so the pre-release is valid semver", () => {+ // A short hash that is all decimal digits with a leading zero would+ // produce `0.0.0-0123456` — a numeric pre-release identifier must not+ // have a leading zero, so that value is invalid semver and+ // electron-updater would throw on the no-tag builds this fallback+ // exists to protect. The `g` prefix makes it a single alphanumeric+ // identifier, which is always valid.+ expect(normalizeGitVersion("0123456")).toBe("0.0.0-g0123456");+ expect(normalizeGitVersion("04567")).toBe("0.0.0-g04567");});});