Skip to content

Commit caa1e98

Browse files
rogpeppemvdan
authored andcommitted
internal/cueversion: construct pseudoversion when possible
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 <[email protected]> Change-Id: I508011561c0dd3f3b212c777919f64f67411ad35 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194091 Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Daniel Martí <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 2f90f54 commit caa1e98

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

internal/cueversion/version.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"runtime/debug"
99
"strings"
1010
"sync"
11+
"time"
12+
13+
"golang.org/x/mod/module"
1114
)
1215

1316
// LanguageVersion returns the CUE language version.
@@ -40,7 +43,32 @@ var moduleVersionOnce = sync.OnceValue(func() string {
4043
// module name; it also happens when running the cue tests.
4144
return "(no-cue-module)"
4245
}
43-
return cueMod.Version
46+
version := cueMod.Version
47+
if version != "(devel)" {
48+
return version
49+
}
50+
// A specific version was not provided by the buildInfo
51+
// so attempt to make our own.
52+
var vcsTime time.Time
53+
var vcsRevision string
54+
for _, s := range bi.Settings {
55+
switch s.Key {
56+
case "vcs.time":
57+
// If the format is invalid, we'll print a zero timestamp.
58+
vcsTime, _ = time.Parse(time.RFC3339Nano, s.Value)
59+
case "vcs.revision":
60+
vcsRevision = s.Value
61+
// module.PseudoVersion recommends the revision to be a 12-byte
62+
// commit hash prefix, which is what cmd/go uses as well.
63+
if len(vcsRevision) > 12 {
64+
vcsRevision = vcsRevision[:12]
65+
}
66+
}
67+
}
68+
if vcsRevision != "" {
69+
version = module.PseudoVersion("", "", vcsTime, vcsRevision)
70+
}
71+
return version
4472
})
4573

4674
func findCUEModule(bi *debug.BuildInfo) *debug.Module {

0 commit comments

Comments
 (0)