Skip to content

Commit 98b3e3a

Browse files
committed
cmd/go, cmd/compile: match tool versions
This change passes runtime.Version from the go tool to the compiler. If the versions do not match, the compilation fails. The result is a go tool from one GOROOT will complain loudly if it is invoked with a different GOROOT value. Only release versions are checked, so that when developing Go you can still use "go install cmd/go" and "go install cmd/compile" separately. Fixes #19064 Change-Id: I17e184d07d3c1092b1d9af53ba55ed3ecf67791d Reviewed-on: https://go-review.googlesource.com/42595 Run-TryBot: David Crawshaw <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 9dd7059 commit 98b3e3a

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

src/cmd/compile/internal/gc/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ func Main(archInit func(*Arch)) {
222222
flag.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to `file`")
223223
flag.StringVar(&memprofile, "memprofile", "", "write memory profile to `file`")
224224
flag.Int64Var(&memprofilerate, "memprofilerate", 0, "set runtime.MemProfileRate to `rate`")
225+
var goversion string
226+
flag.StringVar(&goversion, "goversion", "", "required version of the runtime")
225227
flag.StringVar(&traceprofile, "traceprofile", "", "write an execution trace to `file`")
226228
flag.StringVar(&blockprofile, "blockprofile", "", "write block profile to `file`")
227229
flag.StringVar(&mutexprofile, "mutexprofile", "", "write mutex profile to `file`")
@@ -242,6 +244,11 @@ func Main(archInit func(*Arch)) {
242244
usage()
243245
}
244246

247+
if goversion != "" && goversion != runtime.Version() {
248+
fmt.Printf("compile: version %q does not match go tool version %q\n", runtime.Version(), goversion)
249+
Exit(2)
250+
}
251+
245252
thearch.LinkArch.Init(Ctxt)
246253

247254
if outfile == "" {

src/cmd/go/go_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4013,3 +4013,14 @@ func TestExecutableGOROOT(t *testing.T) {
40134013
t.Fatalf("%s env GOROOT = %q, want %q", symGoTool, got, want)
40144014
}
40154015
}
4016+
4017+
func TestNeedVersion(t *testing.T) {
4018+
tg := testgo(t)
4019+
defer tg.cleanup()
4020+
tg.parallel()
4021+
tg.tempFile("goversion.go", `package main; func main() {}`)
4022+
path := tg.path("goversion.go")
4023+
tg.setenv("TESTGO_VERSION", "go1.testgo")
4024+
tg.runFail("run", path)
4025+
tg.grepStderr("compile", "does not match go tool version")
4026+
}

src/cmd/go/internal/work/build.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,13 @@ func BuildModeInit() {
392392
cfg.BuildContext.InstallSuffix += codegenArg[1:]
393393
}
394394
}
395+
if strings.HasPrefix(runtimeVersion, "go1") {
396+
buildGcflags = append(buildGcflags, "-goversion", runtimeVersion)
397+
}
395398
}
396399

400+
var runtimeVersion = runtime.Version()
401+
397402
func runBuild(cmd *base.Command, args []string) {
398403
InstrumentInit()
399404
BuildModeInit()

src/cmd/go/internal/work/testgo.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// This file contains extra hooks for testing the go command.
6+
7+
// +build testgo
8+
9+
package work
10+
11+
import "os"
12+
13+
func init() {
14+
if v := os.Getenv("TESTGO_VERSION"); v != "" {
15+
runtimeVersion = v
16+
}
17+
}

0 commit comments

Comments
 (0)