Skip to content

Commit

Permalink
cmd/cue: hard-code a fallback CUE semver version
Browse files Browse the repository at this point in the history
As described in https://cuelang.org/issue/2908, the behavior now is:

1) Users explicity setting a version at build time via
   -ldflags='-X cuelang.org/go/cmd/cue/cmd.version=<version>'
   would use that version as-is, just like today
2) Users building via GOPROXY would get the module version
   reported by the proxy as-is, just like today
3) Other users (i.e. those using `go install ./cmd/cue`) would now get
   the fallback version added by this patch and updated manually,
   even if they are currently a few commits after that last tag,
   since `cue version` will still print the stamped VCS information

That is, up until now and in master, we had

    $ go install ./cmd/cue
    $ cue version
    cue version v0.0.0-20240307083748-cdcb6fa19bf3

    go version devel go1.23-58052fe8e7 2024-03-07 05:27:04 +0000
          -buildmode exe
           -compiler gc
      DefaultGODEBUG httplaxcontentlength=1,httpmuxgo121=1,tls10server=1,tlsrsakex=1,tlsunsafeekm=1,winreadlinkvolume=0,winsymlink=0
         CGO_ENABLED 1
              GOARCH amd64
                GOOS linux
             GOAMD64 v3
                 vcs git
        vcs.revision cdcb6fa
            vcs.time 2024-03-07T08:37:48Z
        vcs.modified false

Whereas now, we have

    $ go install ./cmd/cue
    $ cue version
    cue version v0.8.0-alpha.5

    go version devel go1.23-58052fe8e7 2024-03-07 05:27:04 +0000
          -buildmode exe
           -compiler gc
      DefaultGODEBUG httplaxcontentlength=1,httpmuxgo121=1,tls10server=1,tlsrsakex=1,tlsunsafeekm=1,winreadlinkvolume=0,winsymlink=0
         CGO_ENABLED 1
              GOARCH amd64
                GOOS linux
             GOAMD64 v3
                 vcs git
        vcs.revision 9d9b786f0996468bf527a9412d1389a7a17a0562
            vcs.time 2024-03-07T15:13:21Z
        vcs.modified false

Such a local build will also now be able to add language.version
to cue.mod/module.cue via `cue mod init` and `cue mod tidy`,
as up until now a local cmd/cue build did not know its own
semver version, from which we derive the language version.

Add a TODO to this added fallback version, as once upstream Go
implements the proposal at https://go.dev/issue/50603,
we can remove this manually updated version and keep semver versions
in local builds via the Go toolchain automatically.

Fixes #2908.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I9e6189fcdaff5b493c9065e5bb74f4dd9a023800
  • Loading branch information
mvdan committed Mar 7, 2024
1 parent 9da49fd commit 977f0fb
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 40 deletions.
3 changes: 3 additions & 0 deletions cmd/cue/cmd/testdata/script/modinit_majorversion.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ exists cue.mod/pkg

-- want-module.cue --
module: "foo.com/bar@v1"
language: {
version: "v0.8.0-alpha.5"
}
3 changes: 3 additions & 0 deletions cmd/cue/cmd/testdata/script/modinit_noargs.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ exists cue.mod/pkg

-- want-module.cue --
module: ""
language: {
version: "v0.8.0-alpha.5"
}
3 changes: 3 additions & 0 deletions cmd/cue/cmd/testdata/script/modinit_nomajorversion.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ exists cue.mod/pkg

-- want-module.cue --
module: "foo.com/bar@v0"
language: {
version: "v0.8.0-alpha.5"
}
3 changes: 3 additions & 0 deletions cmd/cue/cmd/testdata/script/modtidy_initial.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ exec cue export .
cmp stdout want-stdout
-- want-module --
module: "main.org@v0"
language: {
version: "v0.8.0-alpha.5"
}
deps: {
"bar.com@v0": {
v: "v0.5.0"
Expand Down
3 changes: 3 additions & 0 deletions cmd/cue/cmd/testdata/script/modtidy_non_root.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ cmp cue.mod/module.cue want-module

-- want-module --
module: "main.org@v0"
language: {
version: "v0.8.0-alpha.5"
}
-- cue.mod/module.cue --
module: "main.org@v0"
-- x/y/z.cue --
Expand Down
3 changes: 3 additions & 0 deletions cmd/cue/cmd/testdata/script/modtidy_with_generated_code.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ exec cue export .
cmp stdout want-stdout
-- want-module --
module: "main.org@v0"
language: {
version: "v0.8.0-alpha.5"
}
deps: {
"example.com@v0": {
v: "v0.0.1"
Expand Down
3 changes: 3 additions & 0 deletions cmd/cue/cmd/testdata/script/modtidy_with_vcsinfo.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ cmp stdout want-stdout
}
-- want-module --
module: "main.org@v0"
language: {
version: "v0.8.0-alpha.5"
}
-- cue.mod/module.cue --
module: "main.org@v0"

Expand Down
8 changes: 3 additions & 5 deletions cmd/cue/cmd/testdata/script/version.txtar
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# Version output without VCS information.
#
# TODO: this test may fail if someone runs the tests via GOPROXY,
# as then the module version won't be "devel".
exec cue version
stdout -count=1 'cue version \(devel\)'
stdout -count=1 '^cue version v0.[1-9]'
stdout -count=1 '^go version (devel )?go1.'

# Version output with VCS information.
env CUE_VERSION_TEST_CFG='[{"Key":"vcs","Value":"git"},{"Key":"vcs.revision","Value":"47b7032385cb490fab7d47b89fca36835cf13d39"},{"Key":"vcs.time","Value":"2022-05-10T04:58:46Z"},{"Key":"vcs.modified","Value":"true"}]'
exec cue version
stdout -count=1 '^cue version v0.0.0-20220510045846-47b7032385cb$'
stdout -count=1 '^cue version v0.[1-9]'
stdout -count=1 '^go version (devel )?go1.'
stdout -count=1 'vcs git'
stdout -count=1 'vcs\.revision 47b7032385cb490fab7d47b89fca36835cf13d39'
Expand Down
49 changes: 14 additions & 35 deletions cmd/cue/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ import (
"os"
"runtime"
"runtime/debug"
"time"

"github.com/spf13/cobra"
"golang.org/x/mod/module"
)

func newVersionCmd(c *Command) *cobra.Command {
Expand All @@ -37,8 +35,6 @@ func newVersionCmd(c *Command) *cobra.Command {
return cmd
}

const defaultVersion = "(devel)"

// version can be set by a builder using
// -ldflags='-X cuelang.org/go/cmd/cue/cmd.version=<version>'.
// However, people should prefer building via a mechanism which
Expand All @@ -48,6 +44,15 @@ const defaultVersion = "(devel)"
// 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 @@ -57,11 +62,7 @@ func runVersion(cmd *Command, args []string) error {
// shouldn't happen
return errors.New("unknown error reading build-info")
}
version := cueVersion(bi)
if version == "" {
version = defaultVersion
}
fmt.Fprintf(w, "cue version %s\n\n", version)
fmt.Fprintf(w, "cue version %s\n\n", cueVersion(bi))
fmt.Fprintf(w, "go version %s\n", runtime.Version())
for _, s := range bi.Settings {
if s.Value == "" {
Expand All @@ -88,41 +89,19 @@ func cueVersion(bi *debug.BuildInfo) string {
}
v := version
if v != "" {
// The global version variable has been
// configured via ldflags.
// The global version variable has been configured via ldflags.
return v
}
if bi == nil {
return ""
return fallbackVersion
}
if bi.Main.Version != "" && bi.Main.Version != defaultVersion {
if bi.Main.Version != "" && bi.Main.Version != "(devel)" {
v = bi.Main.Version
}
if v != "" {
return v
}
// a specific version was not provided by ldflags or buildInfo
// attempt to make our own
var vcsTime time.Time
var vcsRevision string
for _, s := range bi.Settings {
switch s.Key {
case "vcs.time":
// If the format is invalid, we'll print a zero timestamp.
vcsTime, _ = time.Parse(time.RFC3339Nano, s.Value)
case "vcs.revision":
vcsRevision = s.Value
// module.PseudoVersion recommends the revision to be a 12-byte
// commit hash prefix, which is what cmd/go uses as well.
if len(vcsRevision) > 12 {
vcsRevision = vcsRevision[:12]
}
}
}
if vcsRevision != "" {
return module.PseudoVersion("", "", vcsTime, vcsRevision)
}
return ""
return fallbackVersion
}

func readBuildInfo() (*debug.BuildInfo, bool) {
Expand Down

0 comments on commit 977f0fb

Please sign in to comment.