Skip to content

Commit 701f6a2

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/modcmd: use replaced paths to break cycles in 'go mod tidy'
Fixes golang#30166 Change-Id: I4704b57ed48197f512cd1b818e1f7d2fffc0d9ce Reviewed-on: https://go-review.googlesource.com/c/161898 Run-TryBot: Bryan C. Mills <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent e1b49ad commit 701f6a2

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/cmd/go/internal/modcmd/tidy.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func modTidyGoSum() {
7575
// we only have to tell modfetch what needs keeping.
7676
reqs := modload.Reqs()
7777
keep := make(map[module.Version]bool)
78+
replaced := make(map[module.Version]bool)
7879
var walk func(module.Version)
7980
walk = func(m module.Version) {
8081
// If we build using a replacement module, keep the sum for the replacement,
@@ -87,10 +88,11 @@ func modTidyGoSum() {
8788
keep[m] = true
8889
} else {
8990
keep[r] = true
91+
replaced[m] = true
9092
}
9193
list, _ := reqs.Required(m)
9294
for _, r := range list {
93-
if !keep[r] {
95+
if !keep[r] && !replaced[r] {
9496
walk(r)
9597
}
9698
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
env GO111MODULE=on
22

3+
# golang.org/issue/30166: 'go mod tidy' should not crash if a replaced module is
4+
# involved in a cycle.
5+
cd cycle
6+
env GOTRACEBACK=off
7+
go mod tidy
8+
cd ..
9+
310
# From inside the module, 'go list -m all' should NOT include transitive
411
# requirements of modules that have been replaced.
512
go list -m all
@@ -69,3 +76,35 @@ import (
6976
_ "rsc.io/sampler"
7077
_ "golang.org/x/text/language"
7178
)
79+
80+
-- cycle/go.mod --
81+
module golang.org/issue/30166
82+
83+
require (
84+
golang.org/issue/30166/a v0.0.0
85+
golang.org/issue/30166/b v0.0.0
86+
)
87+
88+
replace (
89+
golang.org/issue/30166/a => ./a
90+
golang.org/issue/30166/b => ./b
91+
)
92+
-- cycle/cycle.go --
93+
package cycle
94+
95+
import (
96+
_ "golang.org/issue/30166/a"
97+
_ "golang.org/issue/30166/b"
98+
)
99+
-- cycle/a/a.go --
100+
package a
101+
-- cycle/a/go.mod --
102+
module golang.org/issue/30166/a
103+
104+
require golang.org/issue/30166/b v0.0.0
105+
-- cycle/b/b.go --
106+
package b
107+
-- cycle/b/go.mod --
108+
module golang.org/issue/30166/b
109+
110+
require golang.org/issue/30166/a v0.0.0

0 commit comments

Comments
 (0)