Semantic Versioning & Precedence
A version number is a promise. Semantic Versioning 2.0.0 (SemVer) defines what MAJOR.MINOR.PATCH promises and, just as important for tools, exactly how two versions compare. Package managers, release tools and "is there an update?" checks all depend on that comparison. Version ranges (^1.2.0, ~1.2, >=1.0 <2) are covered in Versions & Semver Ranges; this lesson is about the versions themselves and the release engineer's side: which number to pick and how they order.
The promise
Given a version MAJOR.MINOR.PATCH of something with a public API:
| You changed… | Bump | Example |
|---|---|---|
| something incompatible (removed a function, changed a default, renamed a flag) | MAJOR, reset minor and patch | 2.7.3 → 3.0.0 |
| added functionality in a backwards-compatible way (new function, new optional flag), or deprecated something | MINOR, reset patch | 2.7.3 → 2.8.0 |
| fixed a bug without changing the API | PATCH | 2.7.3 → 2.7.4 |
Rules from the specification that people forget:
- Declare your public API. SemVer is meaningless without one: a CLI's flags and output format, a library's exported functions, a service's HTTP endpoints, a file format.
- A released version never changes. If 2.7.4 is broken you release 2.7.5; you never re-publish different bytes as 2.7.4.
0.y.zis for initial development: anything may change at any time. Most tools still treat0.MINORas the "major" position (^0.3.1won't accept 0.4.0), but the spec itself promises nothing.- 1.0.0 defines the public API. If people depend on you in production, you should already be at 1.0.0.
- Numbers are non-negative integers without leading zeros:
1.02.0is not a valid version.
Precedence: how versions compare
Precedence is compared field by field, numerically, left to right: 1.10.0 is greater than 1.9.0 (string comparison would say the opposite, a classic bug in hand-written scripts). A pre-release (1.0.0-rc.1) is lower than the same version without one, and build metadata (+build.5) is ignored for precedence. The next lesson covers those two in detail.
This comparator implements the rules from section 11 of the spec, including the official regular expression from semver.org:
// The regular expression suggested by semver.org (named groups version).
const SEMVER = /^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<pre>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?<build>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
function parse(v) {
const m = SEMVER.exec(v);
if (!m) throw new Error(`invalid version: ${v}`);
const g = m.groups;
return { major: BigInt(g.major), minor: BigInt(g.minor), patch: BigInt(g.patch),
pre: g.pre ? g.pre.split('.') : [], build: g.build ?? '' };
}
const isNum = (id) => /^\d+$/.test(id);
function compareIds(a, b) {
if (isNum(a) && isNum(b)) return Math.sign(Number(BigInt(a) - BigInt(b)));
if (isNum(a)) return -1; // numeric identifiers sort before alphanumeric ones
if (isNum(b)) return 1;
return a < b ? -1 : a > b ? 1 : 0; // ASCII order
}
function compare(x, y) {
const a = parse(x), b = parse(y);
for (const k of ['major', 'minor', 'patch']) {
if (a[k] !== b[k]) return a[k] < b[k] ? -1 : 1;
}
if (!a.pre.length && !b.pre.length) return 0; // build metadata is ignored
if (!a.pre.length) return 1; // a release beats its pre-releases
if (!b.pre.length) return -1;
for (let i = 0; i < Math.max(a.pre.length, b.pre.length); i++) {
if (i >= a.pre.length) return -1; // fewer identifiers = lower
if (i >= b.pre.length) return 1;
const c = compareIds(a.pre[i], b.pre[i]);
if (c) return c;
}
return 0;
}
const pairs = [['1.9.0', '1.10.0'], ['2.0.0', '1.99.99'], ['1.0.0-rc.1', '1.0.0'],
['1.0.0+build.1', '1.0.0+build.2'], ['1.0.0-beta.11', '1.0.0-beta.2']];
for (const [a, b] of pairs) {
const c = compare(a, b);
console.log(`${a.padEnd(14)} ${c < 0 ? '<' : c > 0 ? '>' : '='} ${b}`);
}
const shuffled = ['1.0.0', '1.0.0-beta.11', '1.0.0-alpha.beta', '0.9.12', '1.0.0-rc.1',
'1.0.0-alpha', '1.0.0-beta.2', '1.0.0-alpha.1', '1.0.0-beta', '1.1.0'];
console.log(shuffled.sort(compare).join(' < '));
for (const bad of ['1.02.0', 'v1.2.3', '1.2', '1.2.3-', '1.2.3-01']) {
try { parse(bad); } catch (e) { console.log(e.message); }
}1.9.0 < 1.10.0
2.0.0 > 1.99.99
1.0.0-rc.1 < 1.0.0
1.0.0+build.1 = 1.0.0+build.2
1.0.0-beta.11 > 1.0.0-beta.2
0.9.12 < 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0 < 1.1.0
invalid version: 1.02.0
invalid version: v1.2.3
invalid version: 1.2
invalid version: 1.2.3-
invalid version: 1.2.3-01
Points worth noticing in the output:
1.0.0+build.1 = 1.0.0+build.2: two builds of the same version have equal precedence. Build metadata identifies a build; it never makes one "newer".beta.11 > beta.2because numeric identifiers compare as numbers. That's why pre-release counters should be separate dot-separated numbers (beta.2), not glued to the label (beta2would compare as text, andbeta10 < beta2).v1.2.3is not a SemVer version. Thevis a tag-naming convention (git tag v1.2.3); tools strip it before parsing.1.2.3-01is invalid: numeric pre-release identifiers can't have leading zeros either.
Choosing the bump in practice
The hard part of SemVer is not the arithmetic; it's deciding what counts as "incompatible". Practical rules:
| Change | Usually |
|---|---|
| a bug fix that changes behaviour someone relied on | still PATCH, but call it out in the notes ("Hyrum's law": every observable behaviour has a user) |
| raising the minimum runtime/compiler/OS version | MAJOR for libraries; many apps treat it as MINOR and say so |
| deprecating a function (still works, warns) | MINOR |
| removing a deprecated function | MAJOR |
| a new required config field | MAJOR (old configs break); a new optional field with a default is MINOR |
| a security fix that must break something | MAJOR, released together with a patched older line if you can (Backport Flows) |
| dependency updates | the bump your users see: a patch-level dependency update is usually PATCH; exposing a dependency's new major in your API is MAJOR |
The tools later in the course make this decision from commit messages: fix: → patch, feat: → minor, ! or BREAKING CHANGE: → major (Conventional Commits). They only automate the arithmetic; a human still decides which type each commit is.
Test yourself
Which is the correct order?
- 1.10.0 < 1.9.0 < 2.0.0
- 1.9.0 < 1.10.0 < 2.0.0
- 2.0.0 < 1.10.0 < 1.9.0
- They can't be compared
What is the precedence of 1.4.0+linux.x64 compared with 1.4.0+windows.x64?
- linux is lower (alphabetical)
- windows is lower
- They are equal: build metadata is ignored
- Comparing them is an error
Version 3.2.1 of a library was published with a bug. What do you do?
- Fix it and re-publish 3.2.1
- Publish 3.2.2 with the fix (and deprecate/yank 3.2.1 if it is harmful)
- Delete the 3.2.1 tag and re-tag
- Publish 3.2.1-fixed