]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/modload: in updateLazyRoots, do not require the main module explicitly
authorBryan C. Mills <bcmills@google.com>
Tue, 11 May 2021 14:15:57 +0000 (10:15 -0400)
committerBryan C. Mills <bcmills@google.com>
Wed, 12 May 2021 20:20:11 +0000 (20:20 +0000)
Fixes #46078

Change-Id: I8044dac717459f1eeae1d8381a6503f22f9f51ff
Reviewed-on: https://go-review.googlesource.com/c/go/+/319009
Trust: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/cmd/go/internal/modload/buildlist.go
src/cmd/go/testdata/script/mod_tidy_lazy_self.txt [new file with mode: 0644]

index 7a0cea405e3bf83e03b748b4fda9c9f83fba0b7d..e5db41c74860177aaf11e2addd06defcd59a30ea 100644 (file)
@@ -815,7 +815,8 @@ func updateLazyRoots(ctx context.Context, direct map[string]bool, rs *Requiremen
 
                roots = make([]module.Version, 0, len(rs.rootModules))
                rootsUpgraded = false
-               inRootPaths := make(map[string]bool, len(rs.rootModules))
+               inRootPaths := make(map[string]bool, len(rs.rootModules)+1)
+               inRootPaths[Target.Path] = true
                for _, m := range rs.rootModules {
                        if inRootPaths[m.Path] {
                                // This root specifies a redundant path. We already retained the
diff --git a/src/cmd/go/testdata/script/mod_tidy_lazy_self.txt b/src/cmd/go/testdata/script/mod_tidy_lazy_self.txt
new file mode 100644 (file)
index 0000000..ffcea18
--- /dev/null
@@ -0,0 +1,71 @@
+# Regression test for https://golang.org/issue/46078:
+# 'go mod tidy' should not panic if the main module initially
+# requires an older version of itself.
+
+
+# A module that explicitly requires an older version of itself should be
+# rejected as inconsistent: we enforce that every explicit requirement is the
+# selected version of its module path, but the selected version of the main
+# module is always itself — not some explicit version.
+
+! go list -m all
+stderr '^go: updates to go\.mod needed; to update it:\n\tgo mod tidy$'
+
+
+# The suggested 'go mod tidy' command should succeed (not crash).
+
+go mod tidy
+
+
+# We prune out redundant roots very early on in module loading, and at that
+# point the indirect requirement on example.net/x v0.1.0 appears to be
+# irrelevant. It should be pruned out; when the import of "example.net/x" is
+# later resolved, it should resolve at the latest version (v0.2.0), not the
+# version implied by the (former) misleading requirement on the older version of
+# the main module.
+
+cmp go.mod go.mod.tidy
+
+
+-- go.mod --
+module golang.org/issue/46078
+
+go 1.17
+
+replace (
+       example.net/x v0.1.0 => ./x
+       example.net/x v0.2.0 => ./x
+       golang.org/issue/46078 v0.1.0 => ./old
+)
+
+require golang.org/issue/46078 v0.1.0
+-- go.mod.tidy --
+module golang.org/issue/46078
+
+go 1.17
+
+replace (
+       example.net/x v0.1.0 => ./x
+       example.net/x v0.2.0 => ./x
+       golang.org/issue/46078 v0.1.0 => ./old
+)
+
+require example.net/x v0.2.0
+-- issue46078/issue.go --
+package issue46078
+
+import _ "example.net/x"
+
+-- old/go.mod --
+module golang.org/issue/46078
+
+go 1.17
+
+require example.net/x v0.1.0
+
+-- x/go.mod --
+module example.net/x
+
+go 1.17
+-- x/x.go --
+package x