Commit ea726d8d authored by Baokun Lee's avatar Baokun Lee Committed by Bryan C. Mills

cmd/go/internal/modcmd: error out if one module with two different paths

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: default avatarBryan C. Mills <bcmills@google.com>
parent d29c14f3
...@@ -211,11 +211,17 @@ func ImportPathsQuiet(patterns []string, tags map[string]bool) []*search.Match { ...@@ -211,11 +211,17 @@ func ImportPathsQuiet(patterns []string, tags map[string]bool) []*search.Match {
// One last pass to finalize wildcards. // One last pass to finalize wildcards.
updateMatches(matches, false) updateMatches(matches, false)
checkMultiplePaths()
WriteGoMod()
return matches
}
// A given module path may be used as itself or as a replacement for another // checkMultiplePaths verifies that a given module path is used as itself
// module, but not both at the same time. Otherwise, the aliasing behavior is // or as a replacement for another module, but not both at the same time.
// too subtle (see https://golang.org/issue/26607), and we don't want to //
// commit to a specific behavior at this point. // (See https://golang.org/issue/26607 and https://golang.org/issue/34650.)
func checkMultiplePaths() {
firstPath := make(map[module.Version]string, len(buildList)) firstPath := make(map[module.Version]string, len(buildList))
for _, mod := range buildList { for _, mod := range buildList {
src := mod src := mod
...@@ -229,9 +235,6 @@ func ImportPathsQuiet(patterns []string, tags map[string]bool) []*search.Match { ...@@ -229,9 +235,6 @@ func ImportPathsQuiet(patterns []string, tags map[string]bool) []*search.Match {
} }
} }
base.ExitIfErrors() base.ExitIfErrors()
WriteGoMod()
return matches
} }
// pathInModuleCache returns the import path of the directory dir, // pathInModuleCache returns the import path of the directory dir,
...@@ -383,6 +386,7 @@ func loadAll(testAll bool) []string { ...@@ -383,6 +386,7 @@ func loadAll(testAll bool) []string {
} }
all := TargetPackages("...") all := TargetPackages("...")
loaded.load(func() []string { return all }) loaded.load(func() []string { return all })
checkMultiplePaths()
WriteGoMod() WriteGoMod()
var paths []string var paths []string
......
...@@ -47,6 +47,12 @@ grep 'rsc.io/sampler v1.2.0' go.mod ...@@ -47,6 +47,12 @@ grep 'rsc.io/sampler v1.2.0' go.mod
cd outside cd outside
go list -m all go list -m all
stdout 'rsc.io/sampler v1.3.0' 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 -- -- go.mod --
module example.com/tidy module example.com/tidy
...@@ -109,3 +115,23 @@ package b ...@@ -109,3 +115,23 @@ package b
module golang.org/issue/30166/b module golang.org/issue/30166/b
require golang.org/issue/30166/a v0.0.0 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
...@@ -21,6 +21,11 @@ stdout '.*[/\\]vendor[/\\]rsc.io[/\\]quote[/\\]v3' ...@@ -21,6 +21,11 @@ stdout '.*[/\\]vendor[/\\]rsc.io[/\\]quote[/\\]v3'
! stderr 'finding' ! stderr 'finding'
! stderr 'lookup disabled' ! 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 -- -- go.mod --
module example.com/replace module example.com/replace
...@@ -37,3 +42,20 @@ module not-rsc.io/quote/v3 ...@@ -37,3 +42,20 @@ module not-rsc.io/quote/v3
-- local/not-rsc.io/quote/v3/quote.go -- -- local/not-rsc.io/quote/v3/quote.go --
package quote 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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment