Skip to content

Commit ce92a20

Browse files
author
Bryan C. Mills
committed
cmd/go: error out of 'go mod tidy' if the go version is newer than supported
Fixes #46142 Change-Id: Ib7a0a159e53cbe476be6aa9a050add10cc750dec Reviewed-on: https://go-review.googlesource.com/c/go/+/319669 Trust: Bryan C. Mills <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 02699f8 commit ce92a20

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/cmd/go/internal/modload/load.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,14 +922,20 @@ func loadFromRoots(ctx context.Context, params loaderParams) *loader {
922922
}
923923

924924
if params.GoVersion != "" {
925-
if semver.Compare("v"+params.GoVersion, narrowAllVersionV) < 0 && !ld.UseVendorAll {
925+
goVersionV := "v" + params.GoVersion
926+
if semver.Compare(goVersionV, narrowAllVersionV) < 0 && !ld.UseVendorAll {
926927
// The module's go version explicitly predates the change in "all" for lazy
927928
// loading, so continue to use the older interpretation.
928929
// (If params.GoVersion is empty, we are probably not in any module at all
929930
// and should use the latest semantics.)
930931
ld.allClosesOverTests = true
931932
}
932933

934+
if ld.Tidy && semver.Compare(goVersionV, "v"+latestGoVersion()) > 0 {
935+
ld.errorf("go mod tidy: go.mod file indicates go %s, but maximum supported version is %s\n", params.GoVersion, latestGoVersion())
936+
base.ExitIfErrors()
937+
}
938+
933939
var err error
934940
ld.requirements, err = convertDepth(ctx, ld.requirements, modDepthFromGoVersion(params.GoVersion))
935941
if err != nil {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# https://golang.org/issue/46142: 'go mod tidy' should error out if the version
2+
# in the go.mod file is newer than the most recent supported version.
3+
4+
cp go.mod go.mod.orig
5+
6+
7+
# If the go.mod file specifies an unsupported Go version, 'go mod tidy' should
8+
# refuse to edit it: we don't know what a tidy go.mod file for that version
9+
# would look like.
10+
11+
! go mod tidy
12+
stderr 'go mod tidy: go.mod file indicates go 2000.0, but maximum supported version is '$goversion'$'
13+
cmp go.mod go.mod.orig
14+
15+
16+
# The -e flag should push past the error and edit the file anyway,
17+
# but preserve the too-high version.
18+
19+
cp go.mod.orig go.mod
20+
go mod tidy -e
21+
stderr 'go mod tidy: go.mod file indicates go 2000.0, but maximum supported version is '$goversion'$'
22+
cmp go.mod go.mod.tidy
23+
24+
25+
# Explicitly switching to a supported version should suppress the error completely.
26+
27+
cp go.mod.orig go.mod
28+
go mod tidy -go=1.17
29+
! stderr 'maximum supported version'
30+
go mod edit -go=1.17 go.mod.tidy
31+
cmp go.mod go.mod.tidy
32+
33+
34+
-- go.mod --
35+
module example.net/from/the/future
36+
37+
go 2000.0
38+
39+
replace example.net/m v0.0.0 => ./m
40+
-- go.mod.tidy --
41+
module example.net/from/the/future
42+
43+
go 2000.0
44+
45+
replace example.net/m v0.0.0 => ./m
46+
47+
require example.net/m v0.0.0
48+
-- x.go --
49+
package x
50+
51+
import "example.net/m"
52+
-- m/go.mod --
53+
module example.net/m
54+
55+
go 1.17
56+
-- m/m.go --
57+
package m

0 commit comments

Comments
 (0)