[go] cmd/go: error out of 'go mod tidy' if the go version is newer than supported

96 views
Skip to first unread message

Bryan C. Mills (Gerrit)

unread,
May 13, 2021, 9:49:29 AM5/13/21
to goph...@pubsubhelper.golang.org, Bryan C. Mills, golang-co...@googlegroups.com

Bryan C. Mills has uploaded this change for review.

View Change

cmd/go: error out of 'go mod tidy' if the go version is newer than supported

Fixes #46142

Change-Id: Ib7a0a159e53cbe476be6aa9a050add10cc750dec
---
M src/cmd/go/internal/modload/load.go
A src/cmd/go/testdata/script/mod_tidy_too_new.txt
2 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/src/cmd/go/internal/modload/load.go b/src/cmd/go/internal/modload/load.go
index f30ac6e..83fc7c0 100644
--- a/src/cmd/go/internal/modload/load.go
+++ b/src/cmd/go/internal/modload/load.go
@@ -922,7 +922,8 @@
}

if params.GoVersion != "" {
- if semver.Compare("v"+params.GoVersion, narrowAllVersionV) < 0 && !ld.UseVendorAll {
+ goVersionV := "v" + params.GoVersion
+ if semver.Compare(goVersionV, narrowAllVersionV) < 0 && !ld.UseVendorAll {
// The module's go version explicitly predates the change in "all" for lazy
// loading, so continue to use the older interpretation.
// (If params.GoVersion is empty, we are probably not in any module at all
@@ -930,6 +931,11 @@
ld.allClosesOverTests = true
}

+ if ld.Tidy && semver.Compare(goVersionV, "v"+latestGoVersion()) > 0 {
+ ld.errorf("go mod tidy: go.mod file indicates go %s, but maximum supported version is %s\n", params.GoVersion, latestGoVersion())
+ base.ExitIfErrors()
+ }
+
var err error
ld.requirements, err = convertDepth(ctx, ld.requirements, modDepthFromGoVersion(params.GoVersion))
if err != nil {
diff --git a/src/cmd/go/testdata/script/mod_tidy_too_new.txt b/src/cmd/go/testdata/script/mod_tidy_too_new.txt
new file mode 100644
index 0000000..b9c53b5
--- /dev/null
+++ b/src/cmd/go/testdata/script/mod_tidy_too_new.txt
@@ -0,0 +1,57 @@
+# 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.
+
+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 mod tidy: go.mod file indicates go 2000.0, but maximum supported version 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 mod tidy: go.mod file indicates go 2000.0, but maximum supported version 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

To view, visit change 319669. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ib7a0a159e53cbe476be6aa9a050add10cc750dec
Gerrit-Change-Number: 319669
Gerrit-PatchSet: 1
Gerrit-Owner: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-MessageType: newchange

Bryan C. Mills (Gerrit)

unread,
May 13, 2021, 10:08:43 AM5/13/21
to Bryan C. Mills, goph...@pubsubhelper.golang.org, Jay Conrod, Michael Matloob, golang-co...@googlegroups.com

Attention is currently required from: Jay Conrod, Michael Matloob.

Patch set 1:Run-TryBot +1

View Change

1 comment:

To view, visit change 319669. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ib7a0a159e53cbe476be6aa9a050add10cc750dec
Gerrit-Change-Number: 319669
Gerrit-PatchSet: 1
Gerrit-Owner: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-Attention: Jay Conrod <jayc...@google.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Thu, 13 May 2021 14:08:38 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

Bryan C. Mills (Gerrit)

unread,
May 13, 2021, 10:18:30 AM5/13/21
to goph...@pubsubhelper.golang.org, Bryan C. Mills, golang-co...@googlegroups.com

Bryan C. Mills has uploaded this change for review.

View Change

[release-branch.go1.16] cmd/go: error out of 'go mod tidy' if the go version is newer than supported

This backports the test from CL 319669, but — because of extensive
changes to the module loader during the Go 1.17 cycle — the
implementation is entirely different. (This implementation is based on
the addGoStmt function present in init.go in the 1.16 branch.)

Fixes #46144
Updates #46142

Change-Id: Ib7a0a159e53cbe476be6aa9a050add10cc750dec
---
M src/cmd/go/internal/modcmd/tidy.go
M src/cmd/go/internal/modload/buildlist.go
A src/cmd/go/testdata/script/mod_tidy_too_new.txt
3 files changed, 80 insertions(+), 0 deletions(-)

diff --git a/src/cmd/go/internal/modcmd/tidy.go b/src/cmd/go/internal/modcmd/tidy.go
index 8bc9ed5..67f90b1 100644
--- a/src/cmd/go/internal/modcmd/tidy.go
+++ b/src/cmd/go/internal/modcmd/tidy.go
@@ -61,6 +61,8 @@
modload.ForceUseModules = true
modload.RootMode = modload.NeedRoot

+ modload.CheckTidyVersion(ctx, tidyE)
+
modload.LoadPackages(ctx, modload.PackageOpts{
Tags: imports.AnyTags(),
ResolveMissingImports: true,
diff --git a/src/cmd/go/internal/modload/buildlist.go b/src/cmd/go/internal/modload/buildlist.go
index 45f220a..0ed9853 100644
--- a/src/cmd/go/internal/modload/buildlist.go
+++ b/src/cmd/go/internal/modload/buildlist.go
@@ -11,10 +11,13 @@
"cmd/go/internal/mvs"
"context"
"fmt"
+ "go/build"
"os"
"strings"

+ "golang.org/x/mod/modfile"
"golang.org/x/mod/module"
+ "golang.org/x/mod/semver"
)

// buildList is the list of modules to use for building packages.
@@ -226,6 +229,33 @@
return capVersionSlice(buildList)
}

+// CheckTidyVersion reports an error to stderr if the Go version indicated by
+// the go.mod file is not supported by this version of the 'go' command.
+//
+// If allowError is false, such an error terminates the program.
+func CheckTidyVersion(ctx context.Context, allowError bool) {
+ LoadModFile(ctx)
+ if index.goVersionV == "" {
+ return
+ }
+
+ tags := build.Default.ReleaseTags
+ maxGo := tags[len(tags)-1]
+ if !strings.HasPrefix(maxGo, "go") || !modfile.GoVersionRE.MatchString(maxGo[2:]) {
+ base.Fatalf("go: unrecognized go version %q", maxGo)
+ }
+ max := maxGo[2:]
+
+ if semver.Compare(index.goVersionV, "v"+max) > 0 {
+ have := index.goVersionV[1:]
+ if allowError {
+ fmt.Fprintf(os.Stderr, "go mod tidy: go.mod file indicates go %s, but maximum supported version is %s\n", have, max)
+ } else {
+ base.Fatalf("go mod tidy: go.mod file indicates go %s, but maximum supported version is %s\n", have, max)
+ }
+ }
+}
+
// TidyBuildList trims the build list to the minimal requirements needed to
// retain the same versions of all packages from the preceding call to
// LoadPackages.

diff --git a/src/cmd/go/testdata/script/mod_tidy_too_new.txt b/src/cmd/go/testdata/script/mod_tidy_too_new.txt
new file mode 100644
index 0000000..ca3163e
--- /dev/null
+++ b/src/cmd/go/testdata/script/mod_tidy_too_new.txt
@@ -0,0 +1,48 @@

+# 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.
+
+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 mod tidy: go.mod file indicates go 2000.0, but maximum supported version 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 mod tidy: go.mod file indicates go 2000.0, but maximum supported version is '$goversion'$'
+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

To view, visit change 319671. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: release-branch.go1.16
Gerrit-Change-Id: Ib7a0a159e53cbe476be6aa9a050add10cc750dec
Gerrit-Change-Number: 319671
Gerrit-PatchSet: 1
Gerrit-Owner: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-MessageType: newchange

Bryan C. Mills (Gerrit)

unread,
May 13, 2021, 10:19:37 AM5/13/21
to Bryan C. Mills, goph...@pubsubhelper.golang.org, Jay Conrod, Michael Matloob, golang-co...@googlegroups.com

Attention is currently required from: Jay Conrod, Michael Matloob.

Patch set 1:Run-TryBot +1

View Change

1 comment:

To view, visit change 319671. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: release-branch.go1.16
Gerrit-Change-Id: Ib7a0a159e53cbe476be6aa9a050add10cc750dec
Gerrit-Change-Number: 319671
Gerrit-PatchSet: 1
Gerrit-Owner: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-Attention: Jay Conrod <jayc...@google.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Thu, 13 May 2021 14:19:32 +0000

Bryan C. Mills (Gerrit)

unread,
May 13, 2021, 10:40:05 AM5/13/21
to goph...@pubsubhelper.golang.org, Bryan C. Mills, golang-co...@googlegroups.com

Bryan C. Mills has uploaded this change for review.

View Change

[release-branch.go1.15] cmd/go: error out of 'go mod tidy' if the go version is newer than supported


This backports the test from CL 319669, but — because of extensive
changes to the module loader during the Go 1.16 and 1.17 cycles — the

implementation is entirely different. (This implementation is based on
the addGoStmt function already present in init.go.)

Fixes #46143

Updates #46142

Change-Id: Ib7a0a159e53cbe476be6aa9a050add10cc750dec
---
M src/cmd/go/internal/modcmd/tidy.go
M src/cmd/go/internal/modload/load.go
A src/cmd/go/testdata/script/mod_tidy_too_new.txt
3 files changed, 59 insertions(+), 0 deletions(-)

diff --git a/src/cmd/go/internal/modcmd/tidy.go b/src/cmd/go/internal/modcmd/tidy.go
index 5ff8474..71e1eda 100644
--- a/src/cmd/go/internal/modcmd/tidy.go
+++ b/src/cmd/go/internal/modcmd/tidy.go
@@ -43,6 +43,7 @@
}

modload.SilenceMissingStdImports = true
+ modload.CheckTidyVersion()
modload.LoadALL()
modload.TidyBuildList()
modTidyGoSum() // updates memory copy; WriteGoMod on next line flushes it out
diff --git a/src/cmd/go/internal/modload/load.go b/src/cmd/go/internal/modload/load.go
index e5ea1a6..0dea6ee 100644
--- a/src/cmd/go/internal/modload/load.go
+++ b/src/cmd/go/internal/modload/load.go
@@ -24,7 +24,9 @@
"sort"

"strings"

+ "golang.org/x/mod/modfile"
"golang.org/x/mod/module"
+ "golang.org/x/mod/semver"
)

// buildList is the list of modules to use for building packages.
@@ -478,6 +480,31 @@
buildList = append([]module.Version{}, list...)

}

+// CheckTidyVersion reports an error to stderr if the Go version indicated by
+// the go.mod file is not supported by this version of the 'go' command.
+//
+// If allowError is false, such an error terminates the program.
+func CheckTidyVersion() {
+ InitMod()
+ mf := ModFile()
+ if mf.Go == nil || mf.Go.Version == "" {
+ return
+ }
+ goVersionV := "v" + mf.Go.Version

+
+ tags := build.Default.ReleaseTags
+ maxGo := tags[len(tags)-1]
+ if !strings.HasPrefix(maxGo, "go") || !modfile.GoVersionRE.MatchString(maxGo[2:]) {
+ base.Fatalf("go: unrecognized go version %q", maxGo)
+ }
+ max := maxGo[2:]
+
+	if semver.Compare(goVersionV, "v"+max) > 0 {
+ have := goVersionV[1:]

+ base.Fatalf("go mod tidy: go.mod file indicates go %s, but maximum supported version is %s\n", have, max)
+ }
+}
+
 // TidyBuildList trims the build list to the minimal requirements needed to
 // retain the same versions of all packages from the preceding Load* or
// ImportPaths* call.

diff --git a/src/cmd/go/testdata/script/mod_tidy_too_new.txt b/src/cmd/go/testdata/script/mod_tidy_too_new.txt
new file mode 100644
index 0000000..8a73c28
--- /dev/null
+++ b/src/cmd/go/testdata/script/mod_tidy_too_new.txt
@@ -0,0 +1,31 @@

+# 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.
+
+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 mod tidy: go.mod file indicates go 2000.0, but maximum supported version is '$goversion'$'
+cmp go.mod go.mod.orig
+
+
+-- go.mod --
+module example.net/from/the/future
+
+go 2000.0
+
+replace example.net/m v0.0.0 => ./m
+-- x.go --
+package x
+
+import "example.net/m"
+-- m/go.mod --
+module example.net/m
+
+go 1.17
+-- m/m.go --
+package m

To view, visit change 319710. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: release-branch.go1.15
Gerrit-Change-Id: Ib7a0a159e53cbe476be6aa9a050add10cc750dec
Gerrit-Change-Number: 319710
Gerrit-PatchSet: 1
Gerrit-Owner: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-MessageType: newchange

Bryan C. Mills (Gerrit)

unread,
May 13, 2021, 11:01:56 AM5/13/21
to Bryan C. Mills, goph...@pubsubhelper.golang.org, Go Bot, Jay Conrod, Michael Matloob, golang-co...@googlegroups.com

Attention is currently required from: Jay Conrod, Michael Matloob.

Bryan C. Mills removed a vote from this change.

View Change

Removed TryBot-Result-1 by Go Bot <go...@golang.org>

To view, visit change 319710. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: release-branch.go1.15
Gerrit-Change-Id: Ib7a0a159e53cbe476be6aa9a050add10cc750dec
Gerrit-Change-Number: 319710
Gerrit-PatchSet: 1
Gerrit-Owner: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-Attention: Jay Conrod <jayc...@google.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-MessageType: deleteVote

Bryan C. Mills (Gerrit)

unread,
May 13, 2021, 11:01:56 AM5/13/21
to Bryan C. Mills, goph...@pubsubhelper.golang.org, Go Bot, Jay Conrod, Michael Matloob, golang-co...@googlegroups.com

Attention is currently required from: Jay Conrod, Michael Matloob.

View Change

1 comment:

To view, visit change 319710. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: release-branch.go1.15
Gerrit-Change-Id: Ib7a0a159e53cbe476be6aa9a050add10cc750dec
Gerrit-Change-Number: 319710
Gerrit-PatchSet: 1
Gerrit-Owner: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-Attention: Jay Conrod <jayc...@google.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Thu, 13 May 2021 15:01:50 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Go Bot <go...@golang.org>
Gerrit-MessageType: comment

Jay Conrod (Gerrit)

unread,
May 13, 2021, 11:31:59 AM5/13/21
to Bryan C. Mills, goph...@pubsubhelper.golang.org, Go Bot, Michael Matloob, golang-co...@googlegroups.com

Attention is currently required from: Bryan C. Mills, Michael Matloob.

Patch set 1:Code-Review +2

View Change

    To view, visit change 319669. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ib7a0a159e53cbe476be6aa9a050add10cc750dec
    Gerrit-Change-Number: 319669
    Gerrit-PatchSet: 1
    Gerrit-Owner: Bryan C. Mills <bcm...@google.com>
    Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
    Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
    Gerrit-Attention: Bryan C. Mills <bcm...@google.com>
    Gerrit-Attention: Michael Matloob <mat...@golang.org>
    Gerrit-Comment-Date: Thu, 13 May 2021 15:31:54 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Jay Conrod (Gerrit)

    unread,
    May 13, 2021, 11:33:31 AM5/13/21
    to Bryan C. Mills, goph...@pubsubhelper.golang.org, Go Bot, Michael Matloob, golang-co...@googlegroups.com

    Attention is currently required from: Bryan C. Mills, Michael Matloob.

    Patch set 1:Code-Review +2

    View Change

      To view, visit change 319671. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: release-branch.go1.16
      Gerrit-Change-Id: Ib7a0a159e53cbe476be6aa9a050add10cc750dec
      Gerrit-Change-Number: 319671
      Gerrit-PatchSet: 1
      Gerrit-Owner: Bryan C. Mills <bcm...@google.com>
      Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
      Gerrit-Reviewer: Go Bot <go...@golang.org>
      Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
      Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
      Gerrit-Attention: Bryan C. Mills <bcm...@google.com>
      Gerrit-Attention: Michael Matloob <mat...@golang.org>
      Gerrit-Comment-Date: Thu, 13 May 2021 15:33:26 +0000

      Jay Conrod (Gerrit)

      unread,
      May 13, 2021, 11:34:06 AM5/13/21
      to Bryan C. Mills, goph...@pubsubhelper.golang.org, Go Bot, Michael Matloob, golang-co...@googlegroups.com

      Attention is currently required from: Bryan C. Mills, Michael Matloob.

      Patch set 1:Code-Review +2

      View Change

        To view, visit change 319710. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: go
        Gerrit-Branch: release-branch.go1.15
        Gerrit-Change-Id: Ib7a0a159e53cbe476be6aa9a050add10cc750dec
        Gerrit-Change-Number: 319710
        Gerrit-PatchSet: 1
        Gerrit-Owner: Bryan C. Mills <bcm...@google.com>
        Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
        Gerrit-Reviewer: Go Bot <go...@golang.org>
        Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
        Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
        Gerrit-Attention: Bryan C. Mills <bcm...@google.com>
        Gerrit-Attention: Michael Matloob <mat...@golang.org>
        Gerrit-Comment-Date: Thu, 13 May 2021 15:34:02 +0000

        Bryan C. Mills (Gerrit)

        unread,
        May 14, 2021, 10:39:13 PM5/14/21
        to Bryan C. Mills, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Jay Conrod, Go Bot, Michael Matloob, golang-co...@googlegroups.com

        Bryan C. Mills submitted this change.

        View Change

        Approvals: Jay Conrod: Looks good to me, approved Bryan C. Mills: Trusted; Run TryBots Go Bot: TryBots succeeded
        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 <bcm...@google.com>
        Run-TryBot: Bryan C. Mills <bcm...@google.com>
        TryBot-Result: Go Bot <go...@golang.org>
        Reviewed-by: Jay Conrod <jayc...@google.com>

        ---
        M src/cmd/go/internal/modload/load.go
        A src/cmd/go/testdata/script/mod_tidy_too_new.txt
        2 files changed, 64 insertions(+), 1 deletion(-)

        diff --git a/src/cmd/go/internal/modload/load.go b/src/cmd/go/internal/modload/load.go
        index f30ac6e..83fc7c0 100644
        --- a/src/cmd/go/internal/modload/load.go
        +++ b/src/cmd/go/internal/modload/load.go

        @@ -922,7 +922,8 @@
        }

        if params.GoVersion != "" {
        - if semver.Compare("v"+params.GoVersion, narrowAllVersionV) < 0 && !ld.UseVendorAll {
        + goVersionV := "v" + params.GoVersion
        + if semver.Compare(goVersionV, narrowAllVersionV) < 0 && !ld.UseVendorAll {
        // The module's go version explicitly predates the change in "all" for lazy
        // loading, so continue to use the older interpretation.
        // (If params.GoVersion is empty, we are probably not in any module at all
        @@ -930,6 +931,11 @@
        ld.allClosesOverTests = true
        }

        + if ld.Tidy && semver.Compare(goVersionV, "v"+latestGoVersion()) > 0 {
        + ld.errorf("go mod tidy: go.mod file indicates go %s, but maximum supported version is %s\n", params.GoVersion, latestGoVersion())
        + base.ExitIfErrors()
        + }
        +
        var err error
        ld.requirements, err = convertDepth(ctx, ld.requirements, modDepthFromGoVersion(params.GoVersion))
        if err != nil {
        diff --git a/src/cmd/go/testdata/script/mod_tidy_too_new.txt b/src/cmd/go/testdata/script/mod_tidy_too_new.txt
        new file mode 100644
        index 0000000..b9c53b5
        --- /dev/null
        +++ b/src/cmd/go/testdata/script/mod_tidy_too_new.txt
        @@ -0,0 +1,57 @@

        +# 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.
        +
        +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 mod tidy: go.mod file indicates go 2000.0, but maximum supported version 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 mod tidy: go.mod file indicates go 2000.0, but maximum supported version 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

        To view, visit change 319669. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: Ib7a0a159e53cbe476be6aa9a050add10cc750dec
        Gerrit-Change-Number: 319669
        Gerrit-PatchSet: 2
        Gerrit-Owner: Bryan C. Mills <bcm...@google.com>
        Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
        Gerrit-Reviewer: Go Bot <go...@golang.org>
        Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
        Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
        Gerrit-MessageType: merged

        Carlos Amedee (Gerrit)

        unread,
        Jun 2, 2021, 3:00:40 PM6/2/21
        to Bryan C. Mills, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Jay Conrod, Go Bot, Michael Matloob, golang-co...@googlegroups.com

        Carlos Amedee submitted this change.

        View Change

        Approvals: Jay Conrod: Looks good to me, approved Bryan C. Mills: Trusted; Run TryBots Go Bot: TryBots succeeded
        [release-branch.go1.16] cmd/go: error out of 'go mod tidy' if the go version is newer than supported


        This backports the test from CL 319669, but — because of extensive
        changes to the module loader during the Go 1.17 cycle — the

        implementation is entirely different. (This implementation is based on
        the addGoStmt function present in init.go in the 1.16 branch.)

        Fixes #46144
        Updates #46142

        Change-Id: Ib7a0a159e53cbe476be6aa9a050add10cc750dec
        Reviewed-on: https://go-review.googlesource.com/c/go/+/319671

        Trust: Bryan C. Mills <bcm...@google.com>
        Run-TryBot: Bryan C. Mills <bcm...@google.com>
        TryBot-Result: Go Bot <go...@golang.org>
        Reviewed-by: Jay Conrod <jayc...@google.com>
        ---
        M src/cmd/go/internal/modcmd/tidy.go
        M src/cmd/go/internal/modload/buildlist.go
        A src/cmd/go/testdata/script/mod_tidy_too_new.txt
        3 files changed, 80 insertions(+), 0 deletions(-)

        diff --git a/src/cmd/go/internal/modcmd/tidy.go b/src/cmd/go/internal/modcmd/tidy.go
        index 8bc9ed5..67f90b1 100644
        --- a/src/cmd/go/internal/modcmd/tidy.go
        +++ b/src/cmd/go/internal/modcmd/tidy.go

        @@ -61,6 +61,8 @@
        modload.ForceUseModules = true
        modload.RootMode = modload.NeedRoot

        + modload.CheckTidyVersion(ctx, tidyE)
        +
        modload.LoadPackages(ctx, modload.PackageOpts{
        Tags: imports.AnyTags(),
        ResolveMissingImports: true,
        diff --git a/src/cmd/go/internal/modload/buildlist.go b/src/cmd/go/internal/modload/buildlist.go
        index 45f220a..0ed9853 100644
        --- a/src/cmd/go/internal/modload/buildlist.go
        +++ b/src/cmd/go/internal/modload/buildlist.go
        @@ -11,10 +11,13 @@
        "cmd/go/internal/mvs"
        "context"
        "fmt"
        + "go/build"
        "os"
         	"strings"

        + "golang.org/x/mod/modfile"
        "golang.org/x/mod/module"
        + "golang.org/x/mod/semver"
        )

        // buildList is the list of modules to use for building packages.
        @@ -226,6 +229,33 @@
        return capVersionSlice(buildList)
        }

        +// CheckTidyVersion reports an error to stderr if the Go version indicated by
        +// the go.mod file is not supported by this version of the 'go' command.
        +//
        +// If allowError is false, such an error terminates the program.
        +func CheckTidyVersion(ctx context.Context, allowError bool) {
        + LoadModFile(ctx)
        +	if index.goVersionV == "" {
        + return
        + }
        +

        + tags := build.Default.ReleaseTags
        + maxGo := tags[len(tags)-1]
        + if !strings.HasPrefix(maxGo, "go") || !modfile.GoVersionRE.MatchString(maxGo[2:]) {
        + base.Fatalf("go: unrecognized go version %q", maxGo)
        + }
        + max := maxGo[2:]
        +
        +	if semver.Compare(index.goVersionV, "v"+max) > 0 {
        + have := index.goVersionV[1:]
        + if allowError {
        + fmt.Fprintf(os.Stderr, "go mod tidy: go.mod file indicates go %s, but maximum supported version is %s\n", have, max)
        + } else {
        +			base.Fatalf("go mod tidy: go.mod file indicates go %s, but maximum supported version is %s\n", have, max)
        + }
        + }
        +}
        +
        // TidyBuildList trims the build list to the minimal requirements needed to
         // retain the same versions of all packages from the preceding call to
        // LoadPackages.

        diff --git a/src/cmd/go/testdata/script/mod_tidy_too_new.txt b/src/cmd/go/testdata/script/mod_tidy_too_new.txt
        new file mode 100644
        index 0000000..ca3163e
        --- /dev/null
        +++ b/src/cmd/go/testdata/script/mod_tidy_too_new.txt
        @@ -0,0 +1,48 @@

        +# 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.
        +
        +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 mod tidy: go.mod file indicates go 2000.0, but maximum supported version 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 mod tidy: go.mod file indicates go 2000.0, but maximum supported version is '$goversion'$'
        +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

        To view, visit change 319671. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: go
        Gerrit-Branch: release-branch.go1.16
        Gerrit-Change-Id: Ib7a0a159e53cbe476be6aa9a050add10cc750dec
        Gerrit-Change-Number: 319671
        Gerrit-PatchSet: 2
        Gerrit-Owner: Bryan C. Mills <bcm...@google.com>
        Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
        Gerrit-Reviewer: Carlos Amedee <car...@golang.org>

        Carlos Amedee (Gerrit)

        unread,
        Jun 2, 2021, 3:01:34 PM6/2/21
        to Bryan C. Mills, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Jay Conrod, Go Bot, Michael Matloob, golang-co...@googlegroups.com

        Carlos Amedee submitted this change.

        View Change

        Approvals: Jay Conrod: Looks good to me, approved Bryan C. Mills: Trusted; Run TryBots Go Bot: TryBots succeeded
        [release-branch.go1.15] cmd/go: error out of 'go mod tidy' if the go version is newer than supported


        This backports the test from CL 319669, but — because of extensive
        changes to the module loader during the Go 1.16 and 1.17 cycles — the

        implementation is entirely different. (This implementation is based on
        the addGoStmt function already present in init.go.)

        Fixes #46143
        Updates #46142

        Change-Id: Ib7a0a159e53cbe476be6aa9a050add10cc750dec
        Reviewed-on: https://go-review.googlesource.com/c/go/+/319710

        Trust: Bryan C. Mills <bcm...@google.com>
        Run-TryBot: Bryan C. Mills <bcm...@google.com>
        Reviewed-by: Jay Conrod <jayc...@google.com>
        TryBot-Result: Go Bot <go...@golang.org>

        ---
        M src/cmd/go/internal/modcmd/tidy.go
        M src/cmd/go/internal/modload/load.go
        A src/cmd/go/testdata/script/mod_tidy_too_new.txt
        3 files changed, 59 insertions(+), 0 deletions(-)

        diff --git a/src/cmd/go/internal/modcmd/tidy.go b/src/cmd/go/internal/modcmd/tidy.go
        index 5ff8474..71e1eda 100644
        --- a/src/cmd/go/internal/modcmd/tidy.go
        +++ b/src/cmd/go/internal/modcmd/tidy.go

        @@ -43,6 +43,7 @@
        }

        modload.SilenceMissingStdImports = true
        + modload.CheckTidyVersion()
        modload.LoadALL()
        modload.TidyBuildList()
        modTidyGoSum() // updates memory copy; WriteGoMod on next line flushes it out
        diff --git a/src/cmd/go/internal/modload/load.go b/src/cmd/go/internal/modload/load.go
        index e5ea1a6..0dea6ee 100644
        --- a/src/cmd/go/internal/modload/load.go
        +++ b/src/cmd/go/internal/modload/load.go
        @@ -24,7 +24,9 @@
        "sort"

        "strings"

        + "golang.org/x/mod/modfile"
        "golang.org/x/mod/module"
        + "golang.org/x/mod/semver"
        )

        // buildList is the list of modules to use for building packages.
        @@ -478,6 +480,31 @@
        buildList = append([]module.Version{}, list...)
        }

        +// CheckTidyVersion reports an error to stderr if the Go version indicated by
        +// the go.mod file is not supported by this version of the 'go' command.
        +//
        +// If allowError is false, such an error terminates the program.
        +func CheckTidyVersion() {
        + InitMod()
        + mf := ModFile()
        +	if mf.Go == nil || mf.Go.Version == "" {
        + return
        + }

        + goVersionV := "v" + mf.Go.Version
        +
        + tags := build.Default.ReleaseTags
        + maxGo := tags[len(tags)-1]
        + if !strings.HasPrefix(maxGo, "go") || !modfile.GoVersionRE.MatchString(maxGo[2:]) {
        + base.Fatalf("go: unrecognized go version %q", maxGo)
        + }
        + max := maxGo[2:]
        +
        +	if semver.Compare(goVersionV, "v"+max) > 0 {
        + have := goVersionV[1:]

        + base.Fatalf("go mod tidy: go.mod file indicates go %s, but maximum supported version is %s\n", have, max)
        + }
        +}
        +
         // TidyBuildList trims the build list to the minimal requirements needed to
         // retain the same versions of all packages from the preceding Load* or
        // ImportPaths* call.
        diff --git a/src/cmd/go/testdata/script/mod_tidy_too_new.txt b/src/cmd/go/testdata/script/mod_tidy_too_new.txt
        new file mode 100644
        index 0000000..8a73c28
        --- /dev/null
        +++ b/src/cmd/go/testdata/script/mod_tidy_too_new.txt
        @@ -0,0 +1,31 @@

        +# 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.
        +
        +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 mod tidy: go.mod file indicates go 2000.0, but maximum supported version is '$goversion'$'
        +cmp go.mod go.mod.orig
        +
        +
        +-- go.mod --
        +module example.net/from/the/future
        +
        +go 2000.0
        +
        +replace example.net/m v0.0.0 => ./m
        +-- x.go --
        +package x
        +
        +import "example.net/m"
        +-- m/go.mod --
        +module example.net/m
        +
        +go 1.17
        +-- m/m.go --
        +package m

        To view, visit change 319710. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: go
        Gerrit-Branch: release-branch.go1.15
        Gerrit-Change-Id: Ib7a0a159e53cbe476be6aa9a050add10cc750dec
        Gerrit-Change-Number: 319710
        Reply all
        Reply to author
        Forward
        0 new messages