]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/modcmd: error out if one module with two different paths
authorBaokun Lee <nototon@gmail.com>
Mon, 7 Oct 2019 14:33:57 +0000 (22:33 +0800)
committerBryan C. Mills <bcmills@google.com>
Tue, 22 Oct 2019 14:26:04 +0000 (14:26 +0000)
If a single module is imported via two different paths, go mod tidy
should have reported this error instead of deferring it until go build.

Fixes #34650.

Change-Id: I9d09df1551b3e2083ed9f0bc77f2989073057717
Reviewed-on: https://go-review.googlesource.com/c/go/+/199598
Run-TryBot: Baokun Lee <nototon@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/go/internal/modload/load.go
src/cmd/go/testdata/script/mod_tidy_replace.txt
src/cmd/go/testdata/script/mod_vendor_replace.txt

index 5f6fd672ba892d86010ddf47ef9d351c34a105c6..a9f711733c2fee6546d50f0cb4def8948dc4a3bb 100644 (file)
@@ -211,11 +211,17 @@ func ImportPathsQuiet(patterns []string, tags map[string]bool) []*search.Match {
 
        // One last pass to finalize wildcards.
        updateMatches(matches, false)
+       checkMultiplePaths()
+       WriteGoMod()
+
+       return matches
+}
 
-       // A given module path may be used as itself or as a replacement for another
-       // module, but not both at the same time. Otherwise, the aliasing behavior is
-       // too subtle (see https://golang.org/issue/26607), and we don't want to
-       // commit to a specific behavior at this point.
+// checkMultiplePaths verifies that a given module path is used as itself
+// or as a replacement for another module, but not both at the same time.
+//
+// (See https://golang.org/issue/26607 and https://golang.org/issue/34650.)
+func checkMultiplePaths() {
        firstPath := make(map[module.Version]string, len(buildList))
        for _, mod := range buildList {
                src := mod
@@ -229,9 +235,6 @@ func ImportPathsQuiet(patterns []string, tags map[string]bool) []*search.Match {
                }
        }
        base.ExitIfErrors()
-       WriteGoMod()
-
-       return matches
 }
 
 // pathInModuleCache returns the import path of the directory dir,
@@ -383,6 +386,7 @@ func loadAll(testAll bool) []string {
        }
        all := TargetPackages("...")
        loaded.load(func() []string { return all })
+       checkMultiplePaths()
        WriteGoMod()
 
        var paths []string
index d5c22530944df3fd90337c4914b3d131eb3acd63..c3158f8610e3499250bb10ab5e97998b37062106 100644 (file)
@@ -47,6 +47,12 @@ grep 'rsc.io/sampler v1.2.0' go.mod
 cd outside
 go list -m all
 stdout 'rsc.io/sampler v1.3.0'
+cd ..
+
+# The same module can't be used as two different paths.
+cd multiple-paths
+! go mod tidy
+stderr 'rsc.io/quote/v3@v3.0.0 used for two different module paths \(not-rsc.io/quote/v3 and rsc.io/quote/v3\)'
 
 -- go.mod --
 module example.com/tidy
@@ -109,3 +115,23 @@ package b
 module golang.org/issue/30166/b
 
 require golang.org/issue/30166/a v0.0.0
+-- multiple-paths/main.go --
+package main
+
+import (
+       "fmt"
+       "rsc.io/quote/v3"
+)
+
+func main() {
+       fmt.Println(quote.GoV3())
+}
+-- multiple-paths/go.mod --
+module quoter
+
+require (
+       rsc.io/quote/v3 v3.0.0
+       not-rsc.io/quote/v3 v3.0.0
+)
+
+replace not-rsc.io/quote/v3 => rsc.io/quote/v3 v3.0.0
index a251daa6c1450660853680c12bdc4e278e437f12..900b36a07240cc048a9a15a78b18321883671e05 100644 (file)
@@ -21,6 +21,11 @@ stdout '.*[/\\]vendor[/\\]rsc.io[/\\]quote[/\\]v3'
 ! stderr 'finding'
 ! stderr 'lookup disabled'
 
+# The same module can't be used as two different paths.
+cd multiple-paths
+! go mod vendor
+stderr 'rsc.io/quote/v3@v3.0.0 used for two different module paths \(not-rsc.io/quote/v3 and rsc.io/quote/v3\)'
+
 -- go.mod --
 module example.com/replace
 
@@ -37,3 +42,20 @@ module not-rsc.io/quote/v3
 
 -- local/not-rsc.io/quote/v3/quote.go --
 package quote
+
+-- multiple-paths/main.go --
+package main
+import (
+       "fmt"
+       "rsc.io/quote/v3"
+)
+func main() {
+       fmt.Println(quote.GoV3())
+}
+-- multiple-paths/go.mod --
+module quoter
+require (
+       rsc.io/quote/v3 v3.0.0
+       not-rsc.io/quote/v3 v3.0.0
+)
+replace not-rsc.io/quote/v3 => rsc.io/quote/v3 v3.0.0
\ No newline at end of file