runtime,cmd/link: include GOEXPERIMENTs in runtime.Version(), "go version X"
This adds the set of GOEXPERIMENTs to the build version if it differs
from the default set of experiments. This exposes the experiment
settings via runtime.Version() and "go version <binary>".
Change-Id: I143dbbc50f66a4cf175469199974e18848075af6
Reviewed-on: https://go-review.googlesource.com/c/go/+/307820
Trust: Austin Clements <austin@google.com>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
diff --git a/src/cmd/dist/buildruntime.go b/src/cmd/dist/buildruntime.go
index e0a101a..3ef551e 100644
--- a/src/cmd/dist/buildruntime.go
+++ b/src/cmd/dist/buildruntime.go
@@ -19,7 +19,6 @@
//
// package sys
//
-// const TheVersion = <version>
// const StackGuardMultiplier = <multiplier value>
//
func mkzversion(dir, file string) {
@@ -28,7 +27,6 @@
fmt.Fprintln(&buf)
fmt.Fprintf(&buf, "package sys\n")
fmt.Fprintln(&buf)
- fmt.Fprintf(&buf, "const TheVersion = `%s`\n", findgoversion())
fmt.Fprintf(&buf, "const StackGuardMultiplierDefault = %d\n", stackGuardMultiplierDefault())
writefile(buf.String(), file, writeSkipSame)
diff --git a/src/cmd/go/testdata/script/version_goexperiment.txt b/src/cmd/go/testdata/script/version_goexperiment.txt
new file mode 100644
index 0000000..4b165eb
--- /dev/null
+++ b/src/cmd/go/testdata/script/version_goexperiment.txt
@@ -0,0 +1,16 @@
+# Test that experiments appear in "go version <binary>"
+
+# This test requires rebuilding the runtime, which takes a while.
+[short] skip
+
+env GOEXPERIMENT=fieldtrack
+go build -o main$GOEXE version.go
+go version main$GOEXE
+stdout 'X:fieldtrack$'
+exec ./main$GOEXE
+stderr 'X:fieldtrack$'
+
+-- version.go --
+package main
+import "runtime"
+func main() { println(runtime.Version()) }
diff --git a/src/cmd/link/internal/ld/main.go b/src/cmd/link/internal/ld/main.go
index 95c89f8..8631cf2 100644
--- a/src/cmd/link/internal/ld/main.go
+++ b/src/cmd/link/internal/ld/main.go
@@ -118,6 +118,12 @@
addstrdata1(ctxt, "runtime.defaultGOROOT="+final)
addstrdata1(ctxt, "cmd/internal/objabi.defaultGOROOT="+final)
+ buildVersion := objabi.Version
+ if goexperiment := objabi.GOEXPERIMENT(); goexperiment != "" {
+ buildVersion += " X:" + goexperiment
+ }
+ addstrdata1(ctxt, "runtime.buildVersion="+buildVersion)
+
// TODO(matloob): define these above and then check flag values here
if ctxt.Arch.Family == sys.AMD64 && objabi.GOOS == "plan9" {
flag.BoolVar(&flag8, "8", false, "use 64-bit addresses in symbol table")
diff --git a/src/internal/goexperiment/flags.go b/src/internal/goexperiment/flags.go
index 1c513d5..4803fab 100644
--- a/src/internal/goexperiment/flags.go
+++ b/src/internal/goexperiment/flags.go
@@ -26,6 +26,9 @@
// In the toolchain, the set of experiments enabled for the current
// build should be accessed via objabi.Experiment.
//
+// The set of experiments is included in the output of runtime.Version()
+// and "go version <binary>" if it differs from the default experiments.
+//
// For the set of experiments supported by the current toolchain, see
// go doc internal/experiment.Flags.
package goexperiment
diff --git a/src/runtime/extern.go b/src/runtime/extern.go
index b73d684..48e1e66 100644
--- a/src/runtime/extern.go
+++ b/src/runtime/extern.go
@@ -240,11 +240,21 @@
return defaultGOROOT
}
+// buildVersion is the Go tree's version string at build time.
+//
+// If any GOEXPERIMENTs are set to non-default values, it will include
+// "X:<GOEXPERIMENT>".
+//
+// This is set by the linker.
+//
+// This is accessed by "go version <binary>".
+var buildVersion string
+
// Version returns the Go tree's version string.
// It is either the commit hash and date at the time of the build or,
// when possible, a release tag like "go1.3".
func Version() string {
- return sys.TheVersion
+ return buildVersion
}
// GOOS is the running program's operating system target:
diff --git a/src/runtime/proc.go b/src/runtime/proc.go
index 35a3f9c..d545a14 100644
--- a/src/runtime/proc.go
+++ b/src/runtime/proc.go
@@ -12,8 +12,6 @@
"unsafe"
)
-var buildVersion = sys.TheVersion
-
// set using cmd/go/internal/modload.ModInfoProg
var modinfo string