Skip to content

Commit 9cc0209

Browse files
[release-branch.go1.11] cmd/go: don't fail if requested Go version is later than current one
This is a partial backport of CL 147278 from tip to the Go 1.11 branch. Change the behavior when the go.mod file requests a Go version that is later than the current one. Previously cmd/go would give a fatal error in this situation. With this change it attempts the compilation, and if (and only if) the compilation fails it adds a note saying that the requested Go version is newer than the known version. This is as described in https://golang.org/issue/28221. Updates #28221 Change-Id: Iea03ca574b6b1a046655f2bb2e554126f877fb66 Reviewed-on: https://go-review.googlesource.com/c/151358 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 6fa0ace commit 9cc0209

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/cmd/go/internal/work/exec.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,6 @@ func (b *Builder) build(a *Action) (err error) {
434434
return fmt.Errorf("missing or invalid binary-only package; expected file %q", a.Package.Target)
435435
}
436436

437-
if p.Module != nil && !allowedVersion(p.Module.GoVersion) {
438-
return fmt.Errorf("module requires Go %s", p.Module.GoVersion)
439-
}
440-
441437
if err := b.Mkdir(a.Objdir); err != nil {
442438
return err
443439
}
@@ -638,12 +634,19 @@ func (b *Builder) build(a *Action) (err error) {
638634
objpkg := objdir + "_pkg_.a"
639635
ofile, out, err := BuildToolchain.gc(b, a, objpkg, icfg.Bytes(), len(sfiles) > 0, gofiles)
640636
if len(out) > 0 {
641-
b.showOutput(a, a.Package.Dir, a.Package.Desc(), b.processOutput(out))
637+
output := b.processOutput(out)
638+
if p.Module != nil && !allowedVersion(p.Module.GoVersion) {
639+
output += "note: module requires Go " + p.Module.GoVersion
640+
}
641+
b.showOutput(a, a.Package.Dir, a.Package.Desc(), output)
642642
if err != nil {
643643
return errPrintedOutput
644644
}
645645
}
646646
if err != nil {
647+
if p.Module != nil && !allowedVersion(p.Module.GoVersion) {
648+
b.showOutput(a, a.Package.Dir, a.Package.Desc(), "note: module requires Go "+p.Module.GoVersion)
649+
}
647650
return err
648651
}
649652
if ofile != objpkg {

src/cmd/go/testdata/script/mod_go_version.txt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
env GO111MODULE=on
44

55
go list
6-
! go build
7-
stderr 'module requires Go 1.999'
6+
go build
87
go build sub.1
8+
go build subver.1
9+
! stderr 'module requires'
910
! go build badsub.1
1011
stderr 'module requires Go 1.11111'
1112

@@ -19,11 +20,13 @@ module m
1920
go 1.999
2021
require (
2122
sub.1 v1.0.0
23+
subver.1 v1.0.0
2224
badsub.1 v1.0.0
2325
versioned.1 v1.0.0
2426
)
2527
replace (
2628
sub.1 => ./sub
29+
subver.1 => ./subver
2730
badsub.1 => ./badsub
2831
versioned.1 v1.0.0 => ./versioned1
2932
versioned.1 v1.1.0 => ./versioned2
@@ -39,12 +42,20 @@ go 1.11
3942
-- sub/x.go --
4043
package x
4144

45+
-- subver/go.mod --
46+
module m
47+
go 1.11111
48+
49+
-- subver/x.go --
50+
package x
51+
4252
-- badsub/go.mod --
4353
module m
4454
go 1.11111
4555

4656
-- badsub/x.go --
4757
package x
58+
invalid syntax
4859

4960
-- versioned1/go.mod --
5061
module versioned
@@ -59,3 +70,4 @@ go 1.99999
5970

6071
-- versioned2/x.go --
6172
package x
73+
invalid syntax

0 commit comments

Comments
 (0)