Skip to content

Commit

Permalink
internal/cueversion: new package
Browse files Browse the repository at this point in the history
This CL factors out the version-determination logic from cmd/cue. This
will enable it to be used by other packages, including putting inside
the User-Agent header when making HTTP requests.

This does make one of the existing cmd/cue tests not possible to write,
but the logic is simple enough (and it doesn't take any additional code
paths) that this seems like it should be OK.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: Ic2accbe29d64bf9cc62cdca413c66edd1ce19e4b
Dispatch-Trailer: {"type":"trybot","CL":1178030,"patchset":2,"ref":"refs/changes/30/1178030/2","targetBranch":"master"}
  • Loading branch information
rogpeppe authored and cueckoo committed Mar 11, 2024
1 parent 6533933 commit 042b275
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 60 deletions.
3 changes: 1 addition & 2 deletions cmd/cue/cmd/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ func backport(mod, cwd string) error {
}

func versionForModFile() string {
bi, _ := readBuildInfo()
version := cueVersion(bi)
version := cueVersion()
if gomodule.IsPseudoVersion(version) {
// If we have a version like v0.7.1-0.20240130142347-7855e15cb701
// we want it to turn into the base version (v0.7.0 in that example).
Expand Down
32 changes: 0 additions & 32 deletions cmd/cue/cmd/testdata/script/modtidy_with_vcsinfo.txtar

This file was deleted.

36 changes: 10 additions & 26 deletions cmd/cue/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"runtime/debug"

"github.com/spf13/cobra"

"cuelang.org/go/internal/cueversion"
)

func newVersionCmd(c *Command) *cobra.Command {
Expand All @@ -44,15 +46,6 @@ func newVersionCmd(c *Command) *cobra.Command {
// considered legacy.
var version string

// fallbackVersion is used as-is when the -ldflags above isn't used
// and when there isn't a recorded main module version,
// for example when building via `go install ./cmd/cue`.
// It should reflect the last release in the current branch.
//
// TODO: remove once Go stamps local builds with a main module version
// derived from the local VCS information per https://go.dev/issue/50603.
const fallbackVersion = "v0.8.0-alpha.5"

func runVersion(cmd *Command, args []string) error {
w := cmd.OutOrStdout()

Expand All @@ -62,7 +55,7 @@ func runVersion(cmd *Command, args []string) error {
// shouldn't happen
return errors.New("unknown error reading build-info")
}
fmt.Fprintf(w, "cue version %s\n\n", cueVersion(bi))
fmt.Fprintf(w, "cue version %s\n\n", cueVersion())
fmt.Fprintf(w, "go version %s\n", runtime.Version())
for _, s := range bi.Settings {
if s.Value == "" {
Expand All @@ -83,26 +76,17 @@ func runVersion(cmd *Command, args []string) error {
// cueVersion returns the version of the CUE module as much
// as can reasonably be determined. If no version can be
// determined, it returns the empty string.
func cueVersion(bi *debug.BuildInfo) string {
if v := os.Getenv("CUE_VERSION_OVERRIDE"); v != "" && inTest {
return v
func cueVersion() string {
if inTest {
if v := os.Getenv("CUE_VERSION_OVERRIDE"); v != "" {
return v
}
}
v := version
if v != "" {
if v := version; v != "" {
// The global version variable has been configured via ldflags.
return v
}
if bi == nil {
return fallbackVersion
}
switch bi.Main.Version {
case "": // missing version
case "(devel)": // local build
case "v0.0.0-00010101000000-000000000000": // build via a directory replace directive
default:
return bi.Main.Version
}
return fallbackVersion
return cueversion.Version()
}

func readBuildInfo() (*debug.BuildInfo, bool) {
Expand Down
39 changes: 39 additions & 0 deletions internal/cueversion/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Package cueversion provides access to the version of the
// cuelang.org/go module.
package cueversion

import (
"runtime/debug"
"sync"
)

// fallbackVersion is used when there isn't a recorded main module
// version, for example when building via `go install ./cmd/cue`. It
// should reflect the last release in the current branch.
//
// TODO: remove once Go stamps local builds with a main module version
// derived from the local VCS information per
// https://go.dev/issue/50603.
const fallbackVersion = "v0.8.0-alpha.5"

// Version returns the version of the cuelang.org/go module as best
// as can reasonably be determined. The result is always a valid Go
// semver version.
func Version() string {
return versionOnce()
}

var versionOnce = sync.OnceValue(func() string {
bi, ok := debug.ReadBuildInfo()
if !ok {
return fallbackVersion
}
switch bi.Main.Version {
case "": // missing version
case "(devel)": // local build
case "v0.0.0-00010101000000-000000000000": // build via a directory replace directive
default:
return bi.Main.Version
}
return fallbackVersion
})
19 changes: 19 additions & 0 deletions internal/cueversion/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cueversion

import (
"testing"

"github.com/go-quicktest/qt"
"golang.org/x/mod/semver"
)

func TestVersion(t *testing.T) {
// This is just a smoke test to make sure that things
// are wired up OK. It would be possible to unit
// test the logic inside Version, but it's simple
// enough that that would amount to creating invariants
// that just match the code, not providing any more
// assurance of correctness.
vers := Version()
qt.Assert(t, qt.Satisfies(vers, semver.IsValid))
}

0 comments on commit 042b275

Please sign in to comment.