From ea726d8dc6f3fb6f02ff816d8bfafed96fc7788a Mon Sep 17 00:00:00 2001 From: Baokun Lee Date: Mon, 7 Oct 2019 22:33:57 +0800 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Bryan C. Mills --- src/cmd/go/internal/modload/load.go | 18 ++++++++----- .../go/testdata/script/mod_tidy_replace.txt | 26 +++++++++++++++++++ .../go/testdata/script/mod_vendor_replace.txt | 22 ++++++++++++++++ 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/cmd/go/internal/modload/load.go b/src/cmd/go/internal/modload/load.go index 5f6fd672ba8..a9f711733c2 100644 --- a/src/cmd/go/internal/modload/load.go +++ b/src/cmd/go/internal/modload/load.go @@ -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() - // 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. + return matches +} + +// 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 diff --git a/src/cmd/go/testdata/script/mod_tidy_replace.txt b/src/cmd/go/testdata/script/mod_tidy_replace.txt index d5c22530944..c3158f8610e 100644 --- a/src/cmd/go/testdata/script/mod_tidy_replace.txt +++ b/src/cmd/go/testdata/script/mod_tidy_replace.txt @@ -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 diff --git a/src/cmd/go/testdata/script/mod_vendor_replace.txt b/src/cmd/go/testdata/script/mod_vendor_replace.txt index a251daa6c14..900b36a0724 100644 --- a/src/cmd/go/testdata/script/mod_vendor_replace.txt +++ b/src/cmd/go/testdata/script/mod_vendor_replace.txt @@ -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