]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: make go line declare minimum required Go version
authorRuss Cox <rsc@golang.org>
Mon, 13 Mar 2023 17:22:34 +0000 (13:22 -0400)
committerGopher Robot <gobot@golang.org>
Fri, 14 Apr 2023 16:08:34 +0000 (16:08 +0000)
For #57001, enforce the go line as declaring the minimum required
version of Go that can compile a module.

Modules that maintain compatibility with old versions of Go
but want to make use of new features in //go:build-constrained files
will be able to do so: the //go:build constraint will be interpreted
as changing the minimum Go version for that file and will unlock
the Go features allowed in that version.

Change-Id: Ibeeb7c93ce7ea2e5187d78af0757cbfac19484a0
Reviewed-on: https://go-review.googlesource.com/c/go/+/476279
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Russ Cox <rsc@golang.org>
Auto-Submit: Russ Cox <rsc@golang.org>

src/cmd/go/internal/work/exec.go
src/cmd/go/testdata/script/mod_go_version.txt

index c42b9a126e22a9a34ea6135dc183d29f5569f5c7..05734c5e987bb320177c7ac394927a6fa9f74aa4 100644 (file)
@@ -532,6 +532,10 @@ func (b *Builder) build(ctx context.Context, a *Action) (err error) {
                return errors.New("binary-only packages are no longer supported")
        }
 
+       if p.Module != nil && !allowedVersion(p.Module.GoVersion) {
+               return errors.New("module requires Go " + p.Module.GoVersion + " or later")
+       }
+
        if err := b.checkDirectives(a); err != nil {
                return err
        }
@@ -853,10 +857,6 @@ OverlayLoop:
        ofile, out, err := BuildToolchain.gc(b, a, objpkg, icfg.Bytes(), embedcfg, symabis, len(sfiles) > 0, gofiles)
        if len(out) > 0 {
                output := b.processOutput(out)
-               if p.Module != nil && !allowedVersion(p.Module.GoVersion) {
-                       output += "note: module requires Go " + p.Module.GoVersion + "\n"
-               }
-
                if err != nil {
                        return formatOutput(b.WorkDir, p.Dir, p.ImportPath, p.Desc(), output)
                } else {
@@ -864,9 +864,6 @@ OverlayLoop:
                }
        }
        if err != nil {
-               if p.Module != nil && !allowedVersion(p.Module.GoVersion) {
-                       b.showOutput(a, p.Dir, p.Desc(), "note: module requires Go "+p.Module.GoVersion+"\n")
-               }
                return err
        }
        if ofile != objpkg {
index 97d9975e6806060fa1f1a4d3f94454b37c06bedc..005c43ca6e23896b291cc80d4b3d901e4db0ed94 100644 (file)
@@ -3,40 +3,17 @@
 env GO111MODULE=on
 
 go list
-go build
-go build sub.1
-go build subver.1
-! stderr 'module requires'
-! go build badsub.1
-stderr '^note: module requires Go 1.11111$'
-
-go build versioned.1
-go mod edit -require versioned.1@v1.1.0
-! go build versioned.1
-stderr '^note: module requires Go 1.99999$'
-
-[short] stop
-
-# The message should be printed even if the compiler emits no output.
-go build -o $WORK/nooutput.exe nooutput.go
-! go build -toolexec=$WORK/nooutput.exe versioned.1
-stderr '^# versioned.1\nnote: module requires Go 1.99999$'
+! go build sub
+stderr '^sub: module requires Go 1.999 or later$'
 
 -- go.mod --
 module m
-go 1.999
+go 1.1
 require (
-       sub.1 v1.0.0
-       subver.1 v1.0.0
-       badsub.1 v1.0.0
-       versioned.1 v1.0.0
+       sub v1.0.0
 )
 replace (
-       sub.1 => ./sub
-       subver.1 => ./subver
-       badsub.1 => ./badsub
-       versioned.1 v1.0.0 => ./versioned1
-       versioned.1 v1.1.0 => ./versioned2
+       sub => ./sub
 )
 
 -- x.go --
@@ -44,67 +21,7 @@ package x
 
 -- sub/go.mod --
 module m
-go 1.11
+go 1.999
 
 -- sub/x.go --
 package x
-
--- subver/go.mod --
-module m
-go 1.11111
-
--- subver/x.go --
-package x
-
--- badsub/go.mod --
-module m
-go 1.11111
-
--- badsub/x.go --
-package x
-invalid syntax
-
--- versioned1/go.mod --
-module versioned
-go 1.0
-
--- versioned1/x.go --
-package x
-
--- versioned2/go.mod --
-module versioned
-go 1.99999
-
--- versioned2/x.go --
-package x
-invalid syntax
-
--- nooutput.go --
-// +build ignore
-
-package main
-
-import (
-       "bytes"
-       "os"
-       "os/exec"
-       "strings"
-)
-
-func main() {
-       stderr := new(bytes.Buffer)
-       stdout := new(bytes.Buffer)
-
-       cmd := exec.Command(os.Args[1], os.Args[2:]...)
-       cmd.Stderr = stderr
-       cmd.Stdout = stdout
-
-       err := cmd.Run()
-       if strings.HasPrefix(os.Args[2], "-V") {
-               os.Stderr.Write(stderr.Bytes())
-               os.Stdout.Write(stdout.Bytes())
-       }
-       if err != nil {
-               os.Exit(1)
-       }
-}