Skip to content

Commit ea726d8

Browse files
oioojBryan C. Mills
authored and
Bryan C. Mills
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent d29c14f commit ea726d8

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

src/cmd/go/internal/modload/load.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,17 @@ func ImportPathsQuiet(patterns []string, tags map[string]bool) []*search.Match {
211211

212212
// One last pass to finalize wildcards.
213213
updateMatches(matches, false)
214+
checkMultiplePaths()
215+
WriteGoMod()
216+
217+
return matches
218+
}
214219

215-
// A given module path may be used as itself or as a replacement for another
216-
// module, but not both at the same time. Otherwise, the aliasing behavior is
217-
// too subtle (see https://golang.org/issue/26607), and we don't want to
218-
// commit to a specific behavior at this point.
220+
// checkMultiplePaths verifies that a given module path is used as itself
221+
// or as a replacement for another module, but not both at the same time.
222+
//
223+
// (See https://golang.org/issue/26607 and https://golang.org/issue/34650.)
224+
func checkMultiplePaths() {
219225
firstPath := make(map[module.Version]string, len(buildList))
220226
for _, mod := range buildList {
221227
src := mod
@@ -229,9 +235,6 @@ func ImportPathsQuiet(patterns []string, tags map[string]bool) []*search.Match {
229235
}
230236
}
231237
base.ExitIfErrors()
232-
WriteGoMod()
233-
234-
return matches
235238
}
236239

237240
// pathInModuleCache returns the import path of the directory dir,
@@ -383,6 +386,7 @@ func loadAll(testAll bool) []string {
383386
}
384387
all := TargetPackages("...")
385388
loaded.load(func() []string { return all })
389+
checkMultiplePaths()
386390
WriteGoMod()
387391

388392
var paths []string

src/cmd/go/testdata/script/mod_tidy_replace.txt

+26
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ grep 'rsc.io/sampler v1.2.0' go.mod
4747
cd outside
4848
go list -m all
4949
stdout 'rsc.io/sampler v1.3.0'
50+
cd ..
51+
52+
# The same module can't be used as two different paths.
53+
cd multiple-paths
54+
! go mod tidy
55+
stderr 'rsc.io/quote/[email protected] used for two different module paths \(not-rsc.io/quote/v3 and rsc.io/quote/v3\)'
5056

5157
-- go.mod --
5258
module example.com/tidy
@@ -109,3 +115,23 @@ package b
109115
module golang.org/issue/30166/b
110116

111117
require golang.org/issue/30166/a v0.0.0
118+
-- multiple-paths/main.go --
119+
package main
120+
121+
import (
122+
"fmt"
123+
"rsc.io/quote/v3"
124+
)
125+
126+
func main() {
127+
fmt.Println(quote.GoV3())
128+
}
129+
-- multiple-paths/go.mod --
130+
module quoter
131+
132+
require (
133+
rsc.io/quote/v3 v3.0.0
134+
not-rsc.io/quote/v3 v3.0.0
135+
)
136+
137+
replace not-rsc.io/quote/v3 => rsc.io/quote/v3 v3.0.0

src/cmd/go/testdata/script/mod_vendor_replace.txt

+22
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ stdout '.*[/\\]vendor[/\\]rsc.io[/\\]quote[/\\]v3'
2121
! stderr 'finding'
2222
! stderr 'lookup disabled'
2323

24+
# The same module can't be used as two different paths.
25+
cd multiple-paths
26+
! go mod vendor
27+
stderr 'rsc.io/quote/[email protected] used for two different module paths \(not-rsc.io/quote/v3 and rsc.io/quote/v3\)'
28+
2429
-- go.mod --
2530
module example.com/replace
2631

@@ -37,3 +42,20 @@ module not-rsc.io/quote/v3
3742

3843
-- local/not-rsc.io/quote/v3/quote.go --
3944
package quote
45+
46+
-- multiple-paths/main.go --
47+
package main
48+
import (
49+
"fmt"
50+
"rsc.io/quote/v3"
51+
)
52+
func main() {
53+
fmt.Println(quote.GoV3())
54+
}
55+
-- multiple-paths/go.mod --
56+
module quoter
57+
require (
58+
rsc.io/quote/v3 v3.0.0
59+
not-rsc.io/quote/v3 v3.0.0
60+
)
61+
replace not-rsc.io/quote/v3 => rsc.io/quote/v3 v3.0.0

0 commit comments

Comments
 (0)