Skip to content

Commit

Permalink
internal/cueversion: construct pseudoversion when possible
Browse files Browse the repository at this point in the history
This change copies some old logic that was previously deleted
(in https://cuelang.org/cl/1176194) to construct a pseudoversion
when one isn't directly present.

It's a little tricky to test but has been manually verified to work.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I508011561c0dd3f3b212c777919f64f67411ad35
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194091
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
rogpeppe authored and mvdan committed Apr 30, 2024
1 parent 2f90f54 commit caa1e98
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion internal/cueversion/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"runtime/debug"
"strings"
"sync"
"time"

"golang.org/x/mod/module"
)

// LanguageVersion returns the CUE language version.
Expand Down Expand Up @@ -40,7 +43,32 @@ var moduleVersionOnce = sync.OnceValue(func() string {
// module name; it also happens when running the cue tests.
return "(no-cue-module)"
}
return cueMod.Version
version := cueMod.Version
if version != "(devel)" {
return version
}
// A specific version was not provided by the buildInfo
// so 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 != "" {
version = module.PseudoVersion("", "", vcsTime, vcsRevision)
}
return version
})

func findCUEModule(bi *debug.BuildInfo) *debug.Module {
Expand Down

0 comments on commit caa1e98

Please sign in to comment.