moby/internal/mod/mod.go
CrazyMax 5400366b90
Set BuildKit version using buildinfo
This sets BuildKit version from the build information embedded
in running binary so we are aligned with the expected vendoring.

We iterate over all dependencies and find the BuildKit one
and set the right version. We also check if the module is
replaced and use it this case.

There is also additional checks if a pseudo version is
detected. See comments in code for more info.

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
2023-04-25 12:30:52 +02:00

65 lines
1.7 KiB
Go

package mod
import (
"runtime/debug"
"sync"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"
)
var (
buildInfoOnce sync.Once
buildInfo *debug.BuildInfo
)
func Version(name string) (modVersion string) {
return moduleVersion(name, readBuildInfo())
}
func moduleVersion(name string, bi *debug.BuildInfo) (modVersion string) {
if bi == nil {
return
}
// iterate over all dependencies and find buildkit
for _, dep := range bi.Deps {
if dep.Path != name {
continue
}
// get the version of buildkit dependency
modVersion = dep.Version
if dep.Replace != nil {
// if the version is replaced, get the replaced version
modVersion = dep.Replace.Version
}
if !module.IsPseudoVersion(modVersion) {
return
}
// if the version is a pseudo version, get the base version
// e.g. v0.10.7-0.20230306143919-70f2ad56d3e5 => v0.10.6
if base, err := module.PseudoVersionBase(modVersion); err == nil && base != "" {
// set canonical version of the base version (removes +incompatible suffix)
// e.g. v2.1.2+incompatible => v2.1.2
base = semver.Canonical(base)
// if the version is a pseudo version, get the revision
// e.g. v0.10.7-0.20230306143919-70f2ad56d3e5 => 70f2ad56d3e5
if rev, err := module.PseudoVersionRev(modVersion); err == nil && rev != "" {
// append the revision to the version
// e.g. v0.10.7-0.20230306143919-70f2ad56d3e5 => v0.10.6+70f2ad56d3e5
modVersion = base + "+" + rev
} else {
// if the revision is not available, use the base version
modVersion = base
}
}
break
}
return
}
func readBuildInfo() *debug.BuildInfo {
buildInfoOnce.Do(func() {
buildInfo, _ = debug.ReadBuildInfo()
})
return buildInfo
}