cmd/link/internal/ld: make runtime.buildVersion with experiments valid

Specifically if there are experiments but no nonstandard toolchain
suffix such as "-devel", the go version will not be valid according to
go/version.IsValid. To fix that, always put the X: part into the suffix,
resulting in, for example, go1.25.0-X:foo.

Fixes #75953

Change-Id: I6a6a696468f3ba9b82b6a410fb88831428e93b58
Reviewed-on: https://go-review.googlesource.com/c/go/+/719701
Reviewed-by: Michael Matloob <matloob@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
matloob@golang.org 2025-11-11 13:54:52 -05:00 committed by Michael Matloob
parent d50a571ddf
commit 9daaab305c
2 changed files with 8 additions and 1 deletions

View file

@ -51,6 +51,9 @@ func stripExperiment(version string) string {
if i := strings.Index(version, " X:"); i >= 0 { if i := strings.Index(version, " X:"); i >= 0 {
return version[:i] return version[:i]
} }
if i := strings.Index(version, "-X:"); i >= 0 {
return version[:i]
}
return version return version
} }

View file

@ -188,7 +188,11 @@ func Main(arch *sys.Arch, theArch Arch) {
buildVersion := buildcfg.Version buildVersion := buildcfg.Version
if goexperiment := buildcfg.Experiment.String(); goexperiment != "" { if goexperiment := buildcfg.Experiment.String(); goexperiment != "" {
buildVersion += " X:" + goexperiment sep := " "
if !strings.Contains(buildVersion, "-") { // See go.dev/issue/75953.
sep = "-"
}
buildVersion += sep + "X:" + goexperiment
} }
addstrdata1(ctxt, "runtime.buildVersion="+buildVersion) addstrdata1(ctxt, "runtime.buildVersion="+buildVersion)