1
0
mirror of https://github.com/golang/go synced 2024-09-29 03:24:29 -06:00

cmd/go: refuse to run when the main module or workspace needs a newer Go

We already refuse to build code in modules are too new (CL 476279).
This is a more comprehensive check: refuse to do anything at all with
modules or workspaces that are too new.

Since the module or workspace is new, it may have semantics we don't
understand and misinterpret well before we get to the actual building of code.
For example when we switched from // +build to //go:build that changed
the decision about which files go into a package, which affects the way
the overall load phase runs and which errors it reports. Waiting until the
building of code would miss earlier changes like that one.

Leaving the test from CL 476279 alone, but it's not load-bearing anymore.

For #57001.

Change-Id: I8c39943db1d7ddbcb9b5cae68d80459fddd68151
Reviewed-on: https://go-review.googlesource.com/c/go/+/497435
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Auto-Submit: Russ Cox <rsc@golang.org>
This commit is contained in:
Russ Cox 2023-05-23 12:09:09 -04:00
parent 9dd0c7fc78
commit 1a22008f2f
5 changed files with 58 additions and 78 deletions

View File

@ -698,6 +698,9 @@ func LoadModFile(ctx context.Context) *Requirements {
if err != nil {
base.Fatalf("reading go.work: %v", err)
}
if gover.Compare(workFileGoVersion, gover.Local()) > 0 {
base.Fatalf("go: %s requires go %v (running go %v)", base.ShortPath(workFilePath), workFileGoVersion, gover.Local())
}
for _, modRoot := range modRoots {
sumFile := strings.TrimSuffix(modFilePath(modRoot), ".mod") + ".sum"
modfetch.WorkspaceGoSumFiles = append(modfetch.WorkspaceGoSumFiles, sumFile)
@ -760,6 +763,9 @@ func LoadModFile(ctx context.Context) *Requirements {
base.Fatalf("go: %v", err)
}
}
if f.Go != nil && gover.Compare(f.Go.Version, gover.Local()) > 0 {
base.Fatalf("go: %s requires go %v (running go %v)", base.ShortPath(gomod), f.Go.Version, gover.Local())
}
modFiles = append(modFiles, f)
mainModule := f.Module.Mod

View File

@ -43,7 +43,8 @@ func readVendorList(mainModule module.Version) {
vendorPkgModule = make(map[string]module.Version)
vendorVersion = make(map[string]string)
vendorMeta = make(map[module.Version]vendorMetadata)
data, err := os.ReadFile(filepath.Join(MainModules.ModRoot(mainModule), "vendor/modules.txt"))
vendorFile := filepath.Join(MainModules.ModRoot(mainModule), "vendor/modules.txt")
data, err := os.ReadFile(vendorFile)
if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
base.Fatalf("go: %s", err)
@ -110,6 +111,9 @@ func readVendorList(mainModule module.Version) {
if goVersion, ok := strings.CutPrefix(entry, "go "); ok {
meta.GoVersion = goVersion
rawGoVersion.Store(mod, meta.GoVersion)
if gover.Compare(goVersion, gover.Local()) > 0 {
base.Fatalf("go: %s in %s requires go %v (running go %v)", mod.Path, base.ShortPath(vendorFile), goVersion, gover.Local())
}
}
// All other tokens are reserved for future use.
}

View File

@ -0,0 +1,47 @@
# Go should refuse to build code that is too new according to go.mod.
# go.mod too new
env GOTOOLCHAIN=local
! go build .
stderr '^go: go.mod requires go 1.99999 \(running go 1\..+\)$'
# go.mod referenced from go.work too new
cp go.work.old go.work
! go build .
stderr '^go: go.mod requires go 1.99999 \(running go 1\..+\)$'
# go.work too new
cp go.work.new go.work
cp go.mod.old go.mod
! go build .
stderr '^go: go.work requires go 1.99999 \(running go 1\..+\)$'
# vendor too new
rm go.work
mv notvendor vendor
! go build -mod=vendor .
stderr '^go: golang.org/x/text in vendor'${/}'modules.txt requires go 1.99999 \(running go 1\..+\)$'
-- go.mod --
module example
go 1.99999
-- p.go --
package p
-- go.mod.old --
module example
go 1.10
-- go.work.new --
go 1.99999
use .
-- go.work.old --
go 1.10
use .
-- notvendor/modules.txt --
# golang.org/x/text v0.9.0
## explicit; go 1.99999
golang.org/x/text/internal/language

View File

@ -1,18 +0,0 @@
# Go should indicate the version the module requires when a standard library
# import is missing. See golang.org/issue/48966.
env GOTOOLCHAIN=local
! go build .
stderr '^main.go:3:8: package nonexistent is not in std \(.*\)$'
stderr '^note: imported by a module that requires go 1.99999$'
-- go.mod --
module example
go 1.99999
-- main.go --
package main
import _ "nonexistent"
func main() {}

View File

@ -1,59 +0,0 @@
# https://golang.org/issue/46142: 'go mod tidy' should error out if the version
# in the go.mod file is newer than the most recent supported version.
env GOTOOLCHAIN=local
cp go.mod go.mod.orig
# If the go.mod file specifies an unsupported Go version, 'go mod tidy' should
# refuse to edit it: we don't know what a tidy go.mod file for that version
# would look like.
! go mod tidy
stderr 'go: go.mod file indicates go 2000.0, but maximum version supported by tidy is '$goversion'$'
cmp go.mod go.mod.orig
# The -e flag should push past the error and edit the file anyway,
# but preserve the too-high version.
cp go.mod.orig go.mod
go mod tidy -e
stderr 'go: go.mod file indicates go 2000.0, but maximum version supported by tidy is '$goversion'$'
cmp go.mod go.mod.tidy
# Explicitly switching to a supported version should suppress the error completely.
cp go.mod.orig go.mod
go mod tidy -go=1.17
! stderr 'maximum supported version'
go mod edit -go=1.17 go.mod.tidy
cmp go.mod go.mod.tidy
-- go.mod --
module example.net/from/the/future
go 2000.0
replace example.net/m v0.0.0 => ./m
-- go.mod.tidy --
module example.net/from/the/future
go 2000.0
replace example.net/m v0.0.0 => ./m
require example.net/m v0.0.0
-- x.go --
package x
import "example.net/m"
-- m/go.mod --
module example.net/m
go 1.17
-- m/m.go --
package m