Minimal Version Selection
Most package managers run a solver: given version ranges (^1.2, >=2.0,<3), search for the newest combination that satisfies all of them (Build Fundamentals covers that approach). The answer depends on what has been published today, so those tools need a lockfile to make tomorrow's build the same as today's.
Go does something simpler. Every requirement is a single minimum version, and the rule is:
For every module in the requirement graph, use the highest of the versions that anyone requires. Nothing newer.
That's minimal version selection (MVS). It's "minimal" because it never picks a version nobody asked for: publishing sampler v1.4.0 tomorrow changes nothing until some go.mod mentions it. So the build list depends only on the go.mod files, and Go needs no lockfile; go.sum only pins the content of those versions.
A diamond, for real
Two libraries need different versions of rsc.io/sampler: liba asks for v1.3.0, libb for v1.3.1. The app requires both. The two libraries are local directories wired in with replace, which doesn't change the selection: their go.mod requirements count like any published module's.
example.com/app
/ \
liba v1.0.0 libb v1.0.0
| |
sampler >= v1.3.0 sampler >= v1.3.1mkdir -p mvs/liba mvs/libb mvs/app && cd mvs
for lib in liba libb; do
ver=v1.3.0; [ $lib = libb ] && ver=v1.3.1
printf 'module example.com/%s\n\ngo 1.27\n\nrequire rsc.io/sampler %s\n' $lib $ver > $lib/go.mod
printf 'package %s\n\nimport "rsc.io/sampler"\n\nfunc Greeting() string { return "%s: " + sampler.Hello() }\n' $lib $lib > $lib/$lib.go
done
cat liba/go.mod libb/go.mod
cd app
go mod init example.com/app 2>/dev/null
cat > main.go <<'SRC'
package main
import (
"fmt"
"example.com/liba"
"example.com/libb"
)
func main() {
fmt.Println(liba.Greeting())
fmt.Println(libb.Greeting())
}
SRC
go mod edit -require=example.com/liba@v1.0.0 -replace=example.com/liba=../liba \
-require=example.com/libb@v1.0.0 -replace=example.com/libb=../libb
go mod tidy
go mod graph | grep sampler
go list -m rsc.io/sampler
go run .module example.com/liba
go 1.27
require rsc.io/sampler v1.3.0
module example.com/libb
go 1.27
require rsc.io/sampler v1.3.1
go: downloading rsc.io/sampler v1.3.1
go: downloading golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
example.com/app rsc.io/sampler@v1.3.1
example.com/liba@v1.0.0 rsc.io/sampler@v1.3.0
example.com/libb@v1.0.0 rsc.io/sampler@v1.3.1
rsc.io/sampler@v1.3.1 golang.org/x/text@v0.0.0-20170915032832-14c0d48ead0c
rsc.io/sampler v1.3.1
liba: Ahoy, world!
libb: Ahoy, world!
Both libraries get sampler v1.3.1. liba asked for "at least v1.3.0" and v1.3.1 satisfies that. Only v1.3.1 was downloaded in full; for v1.3.0 the go command read just its go.mod. tidy also wrote rsc.io/sampler v1.3.1 // indirect into the app's go.mod, so the app records the selected version itself.
Upgrading and downgrading
Now the app raises the minimum to v1.99.99, then goes back down to v1.3.0:
mkdir -p mvs/liba mvs/libb mvs/app && cd mvs
for lib in liba libb; do
ver=v1.3.0; [ $lib = libb ] && ver=v1.3.1
printf 'module example.com/%s\n\ngo 1.27\n\nrequire rsc.io/sampler %s\n' $lib $ver > $lib/go.mod
printf 'package %s\n\nimport "rsc.io/sampler"\n\nfunc Greeting() string { return "%s: " + sampler.Hello() }\n' $lib $lib > $lib/$lib.go
done
cd app
go mod init example.com/app 2>/dev/null
printf 'package main\n\nimport (\n\t"fmt"\n\n\t"example.com/liba"\n\t"example.com/libb"\n)\n\nfunc main() { fmt.Println(liba.Greeting()); fmt.Println(libb.Greeting()) }\n' > main.go
go mod edit -require=example.com/liba@v1.0.0 -replace=example.com/liba=../liba \
-require=example.com/libb@v1.0.0 -replace=example.com/libb=../libb
go mod tidy 2>/dev/null
go get rsc.io/sampler@v1.99.99
go run .
go get rsc.io/sampler@v1.3.0
go list -m all
go build .go: downloading rsc.io/sampler v1.99.99
go: upgraded rsc.io/sampler v1.3.1 => v1.99.99
liba: 99 bottles of beer on the wall, 99 bottles of beer, ...
libb: 99 bottles of beer on the wall, 99 bottles of beer, ...
go: downloading rsc.io/sampler v1.3.0
go: removed example.com/libb v1.0.0
go: downgraded rsc.io/sampler v1.99.99 => v1.3.0
example.com/app
example.com/liba v1.0.0 => ../liba
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
rsc.io/sampler v1.3.0
main.go:7:2: module example.com/libb provides package example.com/libb and is replaced but not required; to add it:
go get example.com/libb
The upgrade is easy: one more requirement, and v1.99.99 becomes the highest. The downgrade is the interesting one. libb v1.0.0 requires sampler v1.3.1, so as long as it's in the graph, sampler can't go below v1.3.1. The go command looks for an older libb that is happy with v1.3.0, finds none, and removes libb to honour your request. The last line shows the price: main.go still imports libb, so the build now fails. go get does what you asked and reports removed; deciding whether that is acceptable is your job (here: it isn't, so go back to v1.3.1).
A model of MVS
The algorithm is a graph walk: start at the main module, follow every requirement of every reached version, and keep the highest version seen per module path. This model runs it on the same graph and gets the same answers as the sessions:
graph = {
("app", ""): [("liba", "v1.0.0"), ("libb", "v1.0.0")],
("liba", "v1.0.0"): [("sampler", "v1.3.0")],
("libb", "v1.0.0"): [("sampler", "v1.3.1")],
("sampler", "v1.3.0"): [("text", "v0.0.0-2017")],
("sampler", "v1.3.1"): [("text", "v0.0.0-2017")],
("sampler", "v1.99.99"): [("text", "v0.0.0-2017")],
("text", "v0.0.0-2017"): [],
}
def vkey(v):
"""v1.3.1 -> (1, 3, 1, 1); a pseudo-version v0.0.0-… sorts before v0.0.0."""
core, _, pre = v.lstrip("v").partition("-")
return tuple(int(x) for x in core.split(".")) + ((0,) if pre else (1,))
def mvs(root_reqs):
selected, todo = {}, list(root_reqs)
seen = set()
while todo:
mod, ver = todo.pop()
if (mod, ver) in seen:
continue
seen.add((mod, ver))
if mod not in selected or vkey(ver) > vkey(selected[mod]):
selected[mod] = ver
todo.extend(graph[(mod, ver)]) # every reached version's requirements count
return dict(sorted(selected.items()))
app = graph[("app", "")]
print("build list: ", mvs(app))
print("upgrade sampler: ", mvs(app + [("sampler", "v1.99.99")]))
def downgrade(reqs, mod, target):
"""Drop every requirement whose own graph needs mod above target (no older versions exist here)."""
kept = [r for r in reqs if r[0] == mod or vkey(mvs([r]).get(mod, "v0.0.0")) <= vkey(target)]
removed = [r for r in reqs if r not in kept]
kept = [r for r in kept if r[0] != mod] + [(mod, target)]
return removed, mvs(kept)
removed, result = downgrade(app + [("sampler", "v1.99.99")], "sampler", "v1.3.0")
print("downgrade removes:", removed)
print("downgraded list: ", result)build list: {'liba': 'v1.0.0', 'libb': 'v1.0.0', 'sampler': 'v1.3.1', 'text': 'v0.0.0-2017'}
upgrade sampler: {'liba': 'v1.0.0', 'libb': 'v1.0.0', 'sampler': 'v1.99.99', 'text': 'v0.0.0-2017'}
downgrade removes: [('libb', 'v1.0.0')]
downgraded list: {'liba': 'v1.0.0', 'sampler': 'v1.3.0', 'text': 'v0.0.0-2017'}
The real downgrade would first try older versions of libb; there are none in this graph, so removing it is the only option, exactly as go get did.
Why this design
| Property | Solver + lockfile (npm, Cargo, pip) | MVS (Go) |
|---|---|---|
| Result depends on | the ranges and what is published right now | only the go.mod files in the graph |
| Repeatable without a lockfile | no | yes |
| New upstream release changes your build | yes, until locked | never, until someone requires it |
| Can fail to find an answer | yes (conflicting ranges) | no: "highest minimum" always exists within one major version |
| You get bug fixes automatically | yes, within ranges | no: upgrade explicitly (go get -u, Dependabot/Renovate) |
The cost is in the last row: MVS gives you the oldest versions that work, so security fixes arrive only when you or a dependency upgrade. That's what govulncheck and update bots are for. Incompatible versions can't conflict either, because a breaking change must get a new major version, which is a different module path (Semantic Import Versioning).
Test yourself
liba requires sampler v1.3.0, libb requires v1.3.1, and v1.99.99 exists on the proxy. Which version does MVS select?
- v1.3.0
- v1.3.1
- v1.99.99
- It fails: two different versions are required
Why can Go build reproducibly without a lockfile?
- go.sum stores the selected versions
- The selected versions depend only on the requirements in go.mod files, not on what is newest on the proxy
- The proxy never publishes new versions
- go build always uses the oldest version
You run go get rsc.io/sampler@v1.3.0, but libb (with no older version) requires v1.3.1. What happens?
- sampler stays at v1.3.1
- The command fails with a conflict
- libb is removed from the requirements so sampler can be v1.3.0
- Both versions are linked